【问题标题】:How to predict custom value from trained model?如何从训练有素的模型中预测自定义值?
【发布时间】:2018-11-12 20:02:50
【问题描述】:

我对 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

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

【问题讨论】:

    标签: python pandas numpy dataframe machine-learning


    【解决方案1】:

    错误提示:

    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。您需要传递一个形状为(1, 2)np.array。 试试:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-30
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 2022-10-19
      • 2017-03-28
      • 2021-01-12
      • 2018-11-24
      相关资源
      最近更新 更多