我有一个twitter个人资料的CSV,包含:姓名,描述,追随者数量,追随者数量,bot(我想预测的类)
仅使用CountVectorizer值(xtrain)和Bot(ytrain)时,我成功地执行了一个分类模型。但我无法将此功能添加到我的其他功能集。你知道吗
vectorizer = CountVectorizer()
CountVecTest = vectorizer.fit_transform(training_data.description.values.astype('U'))
CountVecTest = CountVecTest.toarray()
arr = sparse.coo_matrix(CountVecTest)
training_data["NewCol"] = arr.toarray().tolist()
rf = RandomForestClassifier(criterion='entropy', min_samples_leaf=10, min_samples_split=20)
rf = rf.fit(training_data[["followers_count","friends_count","NewCol","bot"]], training_data.bot)
错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-54-7d67a6586592> in <module>()
1 rf = RandomForestClassifier(criterion='entropy', min_samples_leaf=10, min_samples_split=20)
----> 2 rf = rf.fit(training_data[["followers_count","friends_count","NewCol","bot"]], training_data.bot)
D:\0_MyFiles\0_Libraries\Documents\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py in fit(self, X, y, sample_weight)
245 """
246 # Validate or convert input data
--> 247 X = check_array(X, accept_sparse="csc", dtype=DTYPE)
248 y = check_array(y, accept_sparse='csc', ensure_2d=False, dtype=None)
249 if sample_weight is not None:
D:\0_MyFiles\0_Libraries\Documents\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
431 force_all_finite)
432 else:
--> 433 array = np.array(array, dtype=dtype, order=order, copy=copy)
434
435 if ensure_2d:
ValueError: setting an array element with a sequence.
我做了一些调试:
print(type(training_data.NewCol))
print(type(training_data.NewCol[0]))
>>> <class 'pandas.core.series.Series'>
>>> <class 'numpy.ndarray'>
任何帮助都将不胜感激。你知道吗
我会反过来做,把你的特征添加到你的矢量化中。以下是我用一个玩具例子的意思:
假设现在您在一个名为
df
的数据帧中拥有特性,在y_train
中拥有标签:您希望对列
c
执行文本向量化,并将特性a
和b
添加到向量化中。你知道吗这将返回:
但是
CountVecTest
现在是一个scipy稀疏矩阵。所以你需要做的就是把你的特征添加到这个矩阵中。像这样:这将如预期的那样返回:
然后你可以训练你的随机森林:
注意:在您提供的代码片段中,您将label info(“bot”列)传递给了training特性,显然您不应该这样做。你知道吗
相关问题 更多 >
编程相关推荐