【问题标题】:What is the X axis of a binary text classification and why does my graph look so messed up?二进制文本分类的 X 轴是什么?为什么我的图表看起来如此混乱?
【发布时间】:2019-12-22 23:44:18
【问题描述】:

我正在尝试输入一个句子并将其分类为 1 或 0。我有两列数据,第一列是句子文本(例如“这是一个句子”),第二列是分类(例如 0 或 1)。

我已经预测了要解释的值,只是我似乎无法理解图表的 X 轴以及为什么我的回归线看起来像它的样子。

import nltk
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from os import listdir
from os.path import isfile, join
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import roc_auc_score, mean_squared_error, r2_score
from sklearn import linear_model

X_train, X_test, Y_train, Y_test = train_test_split(labor_data['text'],labor_data['label_one'],random_state=0)
vect = CountVectorizer(ngram_range=(1,1),min_df=0,max_df=.25).fit(X_train)
X_train_vectorized = vect.transform(X_train)
lr_model = linear_model.LinearRegression()

lr_model.fit(X_train_vectorized,Y_train)
lr_predictions = lr_model.predict(vect.transform(X_test))

plt.scatter(X_test, Y_test,  color='black')
plt.plot(X_test, lr_predictions, color='blue', linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()

我了解 Y 轴是值,但不了解 X 轴或我的回归线。我知道我的 lr_predictions 是 0 到 1 之间的值,图中的所有值也是如此。但是这条线不应该是一条向下倾斜的直线吗?

图表 https://imgur.com/a/k9JUKC9

【问题讨论】:

  • 你能看一下你的“vect”变量上有什么吗?
  • 只是一个CountVectorizer CountVectorizer(analyzer='word', binary=False, decode_error='strict', dtype=<class 'numpy.int64'>, encoding='utf-8', input='content', lowercase=True, max_df=0.25, max_features=None, min_df=0, ngram_range=(1, 1), preprocessor=None, stop_words=None, strip_accents=None, token_pattern='(?u)\\b\\w\\w+\\b', tokenizer=None, vocabulary=None)
  • 输出是什么样的?打印 X_train_vectorized。

标签: python machine-learning scikit-learn nlp


【解决方案1】:

您示例中的蓝线不是回归线,它是连接所有预测值的线

import nltk
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import roc_auc_score, mean_squared_error, r2_score
from sklearn import linear_model
%matplotlib inline


df = pd.DataFrame(columns = ['text','label_one'])
df = df.append({'text': 'The American Cocker Spaniel is a breed of sporting dog.', 'label_one':0}, ignore_index=True)
df = df.append({'text': 'The Kooikerhondje is a small spaniel-type breed of dog of Dutch ancestry that was originally used as a working dog', 'label_one':0}, ignore_index=True)
df = df.append({'text': 'the dog is running outside.', 'label_one':0}, ignore_index=True)
df = df.append({'text': 'Poodles are a group of formal dog breeds, the Standard Poodle, Miniature Poodle and Toy Poodle.', 'label_one':0}, ignore_index=True)
df = df.append({'text': 'The Chihuahua is the smallest breed of dog, and is named after the Mexican state of Chihuahua. ', 'label_one':0}, ignore_index=True)
df = df.append({'text': 'The Maine Coon is the largest domesticated cat breed.', 'label_one':1}, ignore_index=True)
df = df.append({'text': 'The Persian cat is a long-haired breed of cat characterized by its round face and short muzzle. ', 'label_one':1}, ignore_index=True)
df = df.append({'text': 'The cat (Felis catus) is a small carnivorous mammal', 'label_one':1}, ignore_index=True)
df = df.append({'text': 'The cat is sleeping on the rug', 'label_one':1}, ignore_index=True)



X_train, X_test, Y_train, Y_test = train_test_split(list(df['text']),list(df['label_one']))
vect = CountVectorizer(ngram_range=(1,1),min_df=1,max_df=1.0).fit(X_train)
X_train_vectorized = vect.transform(X_train)
lr_model = linear_model.LinearRegression()

lr_model.fit(X_train_vectorized,Y_train)
lr_predictions = lr_model.predict(vect.transform(X_test))

plt.scatter(range(len(X_test)), Y_test,  color='black')
plt.plot(range(len(X_test)), lr_predictions, color='blue', linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()
for s in X_test:
    print(s)


输出:

一些备注:

1) 在您的示例中,您使用的是线性回归(不是逻辑回归),因此您的预测值不一定介于 0 和 1 之间。例如,上图中的第二句话具有负预测值。

2) 这是多元线性回归,因为有多个解释变量。

查看 X_train_vectorized 告诉我们它是一个:

所以在上面的示例数据中有 36 个解释变量(或“输入变量”)。

我们也可以看看模型系数:

print(len(lr_model.coef_))

36

创建的线性模型确实有 36 个系数(除了模型截距)

print(lr_model.intercept_)

0.3333333333333333

【讨论】:

    猜你喜欢
    • 2021-05-09
    • 2013-08-07
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多