Number of features of the model must match the input

旧时模样 提交于 2019-12-25 07:16:36

问题


For some reason the features of this dataset is being interpreted as rows, "Model n_features is 16 and input n_features is 18189" Where 18189 is the number of rows and 16 is the correct feature list.

The suspect code is here:

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.

Anyone got any ideas?

Sample training date csv

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

Sample test data csv

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

回答1:


You have a small typo - you created the variable xTest and then are immediately overwriting to something incorrect. Change the offending lines to:

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


来源:https://stackoverflow.com/questions/36722931/number-of-features-of-the-model-must-match-the-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!