情绪分析逻辑回归的错误输入形状
Bad input shape on sentiment analysis logistic regression
我想用逻辑回归预测情感分析模型的准确性,但得到错误:错误的输入形状(使用输入编辑)
数据框:
df
sentence | polarity_label
new release! | positive
buy | neutral
least good-looking | negative
代码:
from sklearn.preprocessing import OneHotEncoder
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer,
ENGLISH_STOP_WORDS
# Define the set of stop words
my_stop_words = ENGLISH_STOP_WORDS
vect = CountVectorizer(max_features=5000,stop_words=my_stop_words)
vect.fit(df.sentence)
X = vect.transform(df.sentence)
y = df.polarity_label
encoder = OneHotEncoder()
encoder.fit_transform(y)
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=123)
LogisticRegression(penalty='l2',C=1.0)
log_reg = LogisticRegression().fit(X_train, y_train)
错误信息
ValueError: Expected 2D array, got 1D array instead:
array=['Neutral' 'Positive' 'Positive' ... 'Neutral' 'Neutral' 'Neutral'].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.```
How can I fix this?
像这样调整您的代码,例如:
y = df.polarity_label
当前您正在尝试使用您的 CountVectorizer 将 y 转换为一个向量,它是在句子数据上训练的。
所以 CountVectorizer 有这个词汇表(你可以用 vect.get_feature_names()
得到它):
['buy', 'good', 'looking', 'new', 'release']
并将包含这些词的一些文本转换为向量。
但是当你在只有单词 positive, neutral, negative
的 y 上使用它时,它找不到任何它的“已知”单词,因此你的 y 是空的。
如果在转换后检查 y,您还可以看到它是空的:
<3x5 sparse matrix of type '<class 'numpy.int64'>'
with 0 stored elements in Compressed Sparse Row format>
我认为您需要将 y 标签转换为 One hot 编码,
现在你的标签向量可能是这样的 [0,1,0,0,1,0],
但是对于逻辑回归,你需要将它们转换成这种形式 [[0,1],[1,0],[0,1],[0,1]],因为在逻辑回归中我们倾向于计算 probability/likelihood 对于所有 类.
您可以使用 sklearn onehotencoder,
from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder()
encoder.fit_transform(y)
我想用逻辑回归预测情感分析模型的准确性,但得到错误:错误的输入形状(使用输入编辑)
数据框:
df
sentence | polarity_label
new release! | positive
buy | neutral
least good-looking | negative
代码:
from sklearn.preprocessing import OneHotEncoder
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer,
ENGLISH_STOP_WORDS
# Define the set of stop words
my_stop_words = ENGLISH_STOP_WORDS
vect = CountVectorizer(max_features=5000,stop_words=my_stop_words)
vect.fit(df.sentence)
X = vect.transform(df.sentence)
y = df.polarity_label
encoder = OneHotEncoder()
encoder.fit_transform(y)
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=123)
LogisticRegression(penalty='l2',C=1.0)
log_reg = LogisticRegression().fit(X_train, y_train)
错误信息
ValueError: Expected 2D array, got 1D array instead:
array=['Neutral' 'Positive' 'Positive' ... 'Neutral' 'Neutral' 'Neutral'].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.```
How can I fix this?
像这样调整您的代码,例如:
y = df.polarity_label
当前您正在尝试使用您的 CountVectorizer 将 y 转换为一个向量,它是在句子数据上训练的。
所以 CountVectorizer 有这个词汇表(你可以用 vect.get_feature_names()
得到它):
['buy', 'good', 'looking', 'new', 'release']
并将包含这些词的一些文本转换为向量。
但是当你在只有单词 positive, neutral, negative
的 y 上使用它时,它找不到任何它的“已知”单词,因此你的 y 是空的。
如果在转换后检查 y,您还可以看到它是空的:
<3x5 sparse matrix of type '<class 'numpy.int64'>'
with 0 stored elements in Compressed Sparse Row format>
我认为您需要将 y 标签转换为 One hot 编码, 现在你的标签向量可能是这样的 [0,1,0,0,1,0], 但是对于逻辑回归,你需要将它们转换成这种形式 [[0,1],[1,0],[0,1],[0,1]],因为在逻辑回归中我们倾向于计算 probability/likelihood 对于所有 类.
您可以使用 sklearn onehotencoder,
from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder()
encoder.fit_transform(y)