Python 类型错误:'<';在';int';和';str';,don';我不理解这个错误
我正在创建一个机器学习算法,用于情绪分析,但一直遇到这个错误Python 类型错误:'<';在';int';和';str';,don';我不理解这个错误,python,machine-learning,scikit-learn,naivebayes,Python,Machine Learning,Scikit Learn,Naivebayes,我正在创建一个机器学习算法,用于情绪分析,但一直遇到这个错误 TypeError:“您正在使用CountVectorizer对数据进行矢量化,然后将该结果用于TfIdfVectorizer。不能为TFIDFvectorier提供整数数据。如果要使用TfIdfVectorizer,请直接在文本上使用它CountVectorizer和TfIdfVectorizer是两种不同的文本数据矢量化方法,可以将文本数据输入到模型中。我建议你仔细阅读这些内容,了解它的作用 希望有帮助 我正在使用pandas.r
TypeError:“您正在使用
CountVectorizer
对数据进行矢量化,然后将该结果用于TfIdfVectorizer
。不能为TFIDFvectorier
提供整数数据。如果要使用TfIdfVectorizer
,请直接在文本上使用它CountVectorizer
和TfIdfVectorizer
是两种不同的文本数据矢量化方法,可以将文本数据输入到模型中。我建议你仔细阅读这些内容,了解它的作用
希望有帮助 我正在使用pandas.read_csv()方法来读取它,我对它不熟悉,不知道它会返回什么我已经尝试放置TFIDFvectorier,但我遇到了另一个错误错误错误是:类型错误:不支持类型转换:(dtype('
train_data = "C:/Users/User/Abhinav/TrumpStuff/trumpwords.csv"
Xwords = pd.read_csv(train_data, usecols=[2], header=None)
ywords_pos = pd.read_csv(train_data, usecols=[3], header=None)
ywords_neg = pd.read_csv(train_data, usecols=[4], header=None)
ywords_bad = pd.read_csv(train_data, usecols=[5], header=None)
count_vect = CountVectorizer()
Xtrain_counts = count_vect.fit_transform(getStringArray(Xwords))
tfidf_transformer = TfidfTransformer()
Xtrain_tfidf = tfidf_transformer.fit_transform(Xtrain_counts)
clf_positive = MultinomialNB().fit(Xtrain_tfidf, ywords_pos)
clf_negative = MultinomialNB().fit(Xtrain_tfidf, ywords_neg)
clf_bad = MultinomialNB().fit(Xtrain_tfidf, ywords_bad)
"""
My data is from https://data.world/lovesdata/trump-tweets-5-4-09-12-5-16/workspace/file?filename=trumpwords.xlsx
"""
D:\WPy-3702\python-3.7.0\lib\site-packages\sklearn\utils\validation.py:578:
DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-5775276c3452> in <module>()
----> 1 clf_positive = MultinomialNB().fit(Xtrain_tfidf, ywords_pos)
2 clf_negative = MultinomialNB().fit(Xtrain_tfidf, ywords_neg)
3 clf_bad = MultinomialNB().fit(Xtrain_tfidf, ywords_bad)
D:\WPy-3702\python-3.7.0\lib\site-packages\sklearn\naive_bayes.py in fit(self, X, y, sample_weight)
581
582 labelbin = LabelBinarizer()
--> 583 Y = labelbin.fit_transform(y)
584 self.classes_ = labelbin.classes_
585 if Y.shape[1] == 1:
D:\WPy-3702\python-3.7.0\lib\site-packages\sklearn\preprocessing\label.py in fit_transform(self, y)
305 Shape will be [n_samples, 1] for binary problems.
306 """
--> 307 return self.fit(y).transform(y)
308
309 def transform(self, y):
D:\WPy-3702\python-3.7.0\lib\site-packages\sklearn\preprocessing\label.py in fit(self, y)
274 self : returns an instance of self.
275 """
--> 276 self.y_type_ = type_of_target(y)
277 if 'multioutput' in self.y_type_:
278 raise ValueError("Multioutput target data is not supported with "
D:\WPy-3702\python-3.7.0\lib\site-packages\sklearn\utils\multiclass.py in type_of_target(y)
285 return 'continuous' + suffix
286
--> 287 if (len(np.unique(y)) > 2) or (y.ndim >= 2 and len(y[0]) > 1):
288 return 'multiclass' + suffix # [1, 2, 3] or [[1., 2., 3]] or [[1, 2]]
289 else:
D:\WPy-3702\python-3.7.0\lib\site-packages\numpy\lib\arraysetops.py in unique(ar, return_index, return_inverse, return_counts, axis)
221 ar = np.asanyarray(ar)
222 if axis is None:
--> 223 return _unique1d(ar, return_index, return_inverse, return_counts)
224 if not (-ar.ndim <= axis < ar.ndim):
225 raise ValueError('Invalid axis kwarg specified for unique')
D:\WPy-3702\python-3.7.0\lib\site-packages\numpy\lib\arraysetops.py in _unique1d(ar, return_index, return_inverse, return_counts)
281 aux = ar[perm]
282 else:
--> 283 ar.sort()
284 aux = ar
285 flag = np.concatenate(([True], aux[1:] != aux[:-1]))
TypeError: '<' not supported between instances of 'int' and 'str'