>我需要创建一个模型,根据变量对记录进行准确分类。例如,如果一条记录具有预测因子A
或B
,我希望它被归类为具有预测值X
。实际数据采用以下形式:
Predicted Predictor
X A
X B
Y D
X A
对于我的解决方案,我执行以下操作: 1. 用于LabelEncoder
为Predicted
列创建数值 2. 预测变量有多个类别,我使用get_dummies
将它们解析为单独的列。
下面是数据帧的一个子部分,其中包含(虚拟(Predictor
和几个预测变量类别(请原谅错位(:
Predicted Predictor_A Predictor_B
9056 30 0 0
2482 74 1 0
3407 56 1 0
12882 15 0 0
7988 30 0 0
13032 12 0 0
9738 28 0 0
6739 40 0 0
373 131 0 0
3030 62 0 0
8964 30 0 0
691 125 0 0
6214 41 0 0
6438 41 1 0
5060 42 0 0
3703 49 0 0
12461 16 0 0
2235 75 0 0
5107 42 0 0
4464 46 0 0
7075 39 1 0
11891 16 0 0
9190 30 0 0
8312 30 0 0
10328 24 0 0
1602 97 0 0
8804 30 0 0
8286 30 0 0
6821 40 0 0
3953 46 1
如上所示将数据重塑为 datframe 后,我尝试使用sklearn
中的MultinomialNB
。这样做时,我遇到的错误是:
ValueError: Found input variables with inconsistent numbers of samples: [1, 8158]
我在尝试使用只有 2 列的数据帧时遇到了错误 ->Predicted
和Predictor_A
我的问题是:
- 我需要做什么来解决错误?
- 我的方法正确吗?
-
要拟合
MultinomialNB
模型,您需要训练样本及其特征及其相应的标签(目标值(。 -
在您的情况下,
Predicted
是target
变量,Predictor_A and Predictor_B
是features
变量(预测变量(。
示例 1:
from sklearn.naive_bayes import MultinomialNB
import pandas as pd
from sklearn.model_selection import train_test_split
df = pd.read_csv("dt.csv", delim_whitespace=True)
# X is the features
X = df[['Predictor_A','Predictor_B']]
#y is the labels or targets or classes
y = df['Predicted']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
clf = MultinomialNB()
clf.fit(X_train, y_train)
clf.predict(X_test)
#array([30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30])
#this result makes sense if you look at X_test. all the samples are similar
print(X_test)
Predictor_A Predictor_B
8286 0 0
12461 0 0
6214 0 0
9190 0 0
373 0 0
3030 0 0
11891 0 0
9056 0 0
8804 0 0
6438 1 0
#get the probabilities
clf.predict_proba(X_test)
注2:我使用的数据可以在这里找到
编辑
如果使用某些具有 4 个标记(预测因子(的文档来训练模型,则要预测的新文档也应具有相同数量的标记。
示例 2:
clf.fit(X, y)
在这里,X
是一个[29, 2]
数组。所以我们有29
训练样本(文档(,它有2
标签(预测因子(
clf.predict(X_new)
在这里,X_new
可能是[n, 2]
.因此,我们可以预测n
新文档上的类,但这些新文档也应该具有恰好2
标签(预测因子(。