模型的要素数必须与输入

时间:2016-04-19 15:30:02

标签: python numpy scikit-learn sklearn-pandas

由于某种原因,此数据集的功能被解释为行,“模型n_features为16,输入n_features为18189”其中18189是行数,16是正确的要素列表。

可疑代码在这里:

for var in cat_cols:
    num = LabelEncoder()
    train[var] = num.fit_transform(train[var].astype('str'))
    train['output'] = num.fit_transform(train['output'].astype('str'))

for var in cat_cols:
    num = LabelEncoder()
    test[var] = num.fit_transform(test[var].astype('str'))
    test['output'] = num.fit_transform(test['output'].astype('str'))


clf = RandomForestClassifier(n_estimators = 10)

xTrain = train[list(features)].values
yTrain = train["output"].values

xTest = test[list(features)].values
xTest = test["output"].values 

clf.fit(xTrain,yTrain)
clfProbs = clf.predict(xTest)#Error happens here.

有人有任何想法吗?

示例培训日期csv

tr4,42,"JobCat4","divorced","tertiary","yes",2,"yes","no","unknown",5,"may",0,1,-1,0,"unknown","TypeA"

样本测试数据csv

tst2,47,"JobCat3","married","unknown","no",1506,"yes","no","unknown",5,"may",0,1,-1,0,"unknown",?

1 个答案:

答案 0 :(得分:0)

你有一个小错字 - 你创建了变量xTest,然后立即覆盖不正确的东西。将违规行更改为:

xTest = test[list(features)].values
yTest = test["output"].values