模型的特征数必须与输入匹配。模型n_features为5,输入n_features为1

时间:2018-05-29 18:55:03

标签: python pandas numpy scikit-learn

**我正在预测机器学习算法的目标值。用户将提供基本功能,并且它们将是分类值。我为此创建了自己的数据集。所有用户值都在列表中;之后,它们被转换为数组并通过预测函数。那时它失败了"模型的功能数量必须与输入匹配。模型n_features为5,输入n_features为1"错误**

split_index = int(len(db)*.4)


x_train= db.iloc[:,:5][:-split_index]
x_test= db.iloc[:,:5][-split_index:]
y_train= db.iloc[:,5][:-split_index]
y_test= db.iloc[:,5][-split_index:]
# Create linear regression object
regr = RandomForestClassifier(max_depth=2,random_state=1)

**使用RandomForestClassification **

regr.fit(x_train,y_train)
print("PLEASE READ INSTRUCTION")
lista=[]
print("""
kindly enter 5 features in sequence
1.number of leaves: (seedling has upto 15 leaves)
2.height of seeding: (in centimeter upto 20cm)
3.color of seedling: (white 100, reddish 101, brown 102, green 103, faint green 104, yellow 105)
4.texture of seedling: (smooth 0 rough 1)
5.length of leaf: (in centimeter upto 20cm)""")
for x in range(0,5):
    a=int(input("enter feature "))
    lista.append(a)
#print(lista)
#import pandas as pd
import numpy as np
#rp= pd.DataFrame(lista)
#print(rp)

dc=np.array(lista)
dc=dc[:,np.newaxis]
#print(dc)
regr_pred = regr.predict(dc)
print(regr_pred)
#print(type(regr

1 个答案:

答案 0 :(得分:0)

您的{{1}}数组的形状为=(5,1)。所以我建议你转置它

{{1}}