如何从训练有素的模型预测定制价值?

时间:2018-06-03 08:04:01

标签: python pandas numpy dataframe machine-learning

我对python理解ML架构非常陌生。

我设计了一个场景,训练了我的模型,我的测试结果按预期工作。 我的测试数据中有大约5行 我的问题是..如果我想测试单个记录并获得预测我该怎么做? 当我用单一记录进行测试时,我得到以下错误

以下是我的代码示例和错误。请帮忙

import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier

dataset = pd.read_csv('MLData2.csv')
# Data: 
#A  1   1
#A  1   1
#A  1   1
#A  2   1
#A  2   1
#B  3   3
#B  3   3
#B  3   3
#B  4   3
#B  5   3
#C  4   4
#C  1   4
#C  2   4
#C  3   4

X = dataset.iloc[:, :-1].values

y = dataset.iloc[:,2].values


#Encode data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:,0] = labelencoder_X.fit_transform(X[:,0])
X[:,1] = labelencoder_X.fit_transform(X[:,1])
#onehotencoder = OneHotEncoder(categorical_features = "all")
#X = onehotencoder.fit_transform(X).toarray()

#labelencoder_Y = LabelEncoder()
#y = labelencoder_Y.fit_transform(y)
#onehotencoder_y = OneHotEncoder(categorical_features = "all")
#y = np.reshape(y, (-1, 1))
#y = onehotencoder_y.fit_transform(y).toarray()

from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 
0.2, random_state = 42)

from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion="entropy", random_state 
= 42)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
sTest = np.reshape([0,3], (-1,1))

#I want to test model for inputs C 3, which should return 4. How to 
test that?
y_pred1 = classifier.predict(sTest)

以下是收到的错误

ValueError: Number of features of the model must match the input. Model n_features is 2 and input n_features is 1

简而言之,我的输出应始终为1表示A,3表示B,4表示C(在我的场景中)

1 个答案:

答案 0 :(得分:0)

错误说:

ValueError: Number of features of the model must match the input. Model n_features is 2 and input n_features is 1

这意味着您对模型的输入形状与预期的不同。

您的模型需要2D样本,即具有(n_samples, n_features)的形状n_features=2的numpy数组。在您的情况下,您想要测试单个样本,所以n_samples=1。您需要传递np.array形状(1, 2)。 尝试:

sTest = np.reshape([0,3], (1, -1))