scikit-learn - 预测新输入的训练模型

橄榄球

我有一个如下所示的数据集:

| "Consignor Code" | "Consignee Code" | "Origin" | "Destination" | "Carrier Code" | 
|------------------|------------------|----------|---------------|----------------| 
| "6402106844"     | "66903717"       | "DKCPH"  | "CNPVG"       | "6402746387"   | 
| "6402106844"     | "66903717"       | "DKCPH"  | "CNPVG"       | "6402746387"   | 
| "6402106844"     | "6404814143"     | "DKCPH"  | "CNPVG"       | "6402746387"   | 
| "6402107662"     | "66974631"       | "DKCPH"  | "VNSGN"       | "6402746393"   | 
| "6402107662"     | "6404518090"     | "DKCPH"  | "THBKK"       | "6402746393"   | 
| "6402107662"     | "6404518090"     | "DKBLL"  | "THBKK"       | "6402746393"   | 
| "6408507648"     | "6403601344"     | "DKCPH"  | "USTPA"       | "66565231"     | 


我正在尝试在其上构建我的第一个 ML 模型。为此,我正在使用 scikit-learn。这是我的代码:

#Import the dependencies
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import make_scorer, accuracy_score
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.externals import joblib
from sklearn import preprocessing
import pandas as pd

#Import the dataset (A CSV file)
dataset = pd.read_csv('shipments.csv', header=0, skip_blank_lines=True)
#Drop any rows containing NaN values
dataset.dropna(subset=['Consignor Code', 'Consignee Code',
                       'Origin', 'Destination', 'Carrier Code'], inplace=True)

#Convert the numeric only cells to strings
dataset['Consignor Code'] = dataset['Consignor Code'].astype('int64')
dataset['Consignee Code'] = dataset['Consignee Code'].astype('int64')
dataset['Carrier Code'] = dataset['Carrier Code'].astype('int64')

#Define our target (What we want to be able to predict)
target = dataset.pop('Destination')

#Convert all our data to numeric values, so we can use the .fit function.
#For that, we use LabelEncoder
le = preprocessing.LabelEncoder()
target = le.fit_transform(list(target))
dataset['Origin'] = le.fit_transform(list(dataset['Origin']))
dataset['Consignor Code'] = le.fit_transform(list(dataset['Consignor Code']))
dataset['Consignee Code'] = le.fit_transform(list(dataset['Consignee Code']))
dataset['Carrier Code'] = le.fit_transform(list(dataset['Carrier Code']))

#Prepare the dataset.
X_train, X_test, y_train, y_test = train_test_split(
    dataset, target, test_size=0.3, random_state=0)


#Prepare the model and .fit it.
model = RandomForestClassifier()
model.fit(X_train, y_train)

#Make a prediction on the test set.
predictions = model.predict(X_test)

#Print the accuracy score.
print("Accuracy score: {}".format(accuracy_score(y_test, predictions)))

现在上面的代码返回:

Accuracy score: 0.7172413793103448

现在我的问题可能很愚蠢 - 但我如何使用我的model来实际向我展示它对新数据的预测?

考虑下面的新输入,我希望它预测Destination

"6408507648","6403601344","DKCPH","","66565231"

如何使用这些数据查询我的模型并获得预测Destination结果?

塞利乌斯·斯汀格

在这里,您有一个完整的工作示例,其中包含预测。最重要的部分是为每个特征定义不同的标签编码器,这样你就可以用相同的编码拟合新数据,否则你会遇到错误(现在可能会显示,但你会在计算准确度时注意到):

dataset = pd.DataFrame({'Consignor Code':["6402106844","6402106844","6402106844","6402107662","6402107662","6402107662","6408507648"],
                   'Consignee Code': ["66903717","66903717","6404814143","66974631","6404518090","6404518090","6403601344"],
                   'Origin':["DKCPH","DKCPH","DKCPH","DKCPH","DKCPH","DKBLL","DKCPH"],
                   'Destination':["CNPVG","CNPVG","CNPVG","VNSGN","THBKK","THBKK","USTPA"],
                   'Carrier Code':["6402746387","6402746387","6402746387","6402746393","6402746393","6402746393","66565231"]})

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import make_scorer, accuracy_score
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.externals import joblib
from sklearn import preprocessing
import pandas as pd

#Import the dataset (A CSV file)
#Drop any rows containing NaN values
dataset.dropna(subset=['Consignor Code', 'Consignee Code',
                       'Origin', 'Destination', 'Carrier Code'], inplace=True)


#Define our target (What we want to be able to predict)
target = dataset.pop('Destination')

#Convert all our data to numeric values, so we can use the .fit function.
#For that, we use LabelEncoder
le_origin = preprocessing.LabelEncoder()
le_consignor = preprocessing.LabelEncoder()
le_consignee = preprocessing.LabelEncoder()
le_carrier = preprocessing.LabelEncoder()
le_target = preprocessing.LabelEncoder()
target = le_target.fit_transform(list(target))
dataset['Origin'] = le_origin.fit_transform(list(dataset['Origin']))
dataset['Consignor Code'] = le_consignor.fit_transform(list(dataset['Consignor Code']))
dataset['Consignee Code'] = le_consignee.fit_transform(list(dataset['Consignee Code']))
dataset['Carrier Code'] = le_carrier.fit_transform(list(dataset['Carrier Code']))

#Prepare the dataset.
X_train, X_test, y_train, y_test = train_test_split(
    dataset, target, test_size=0.3, random_state=42)


#Prepare the model and .fit it.
model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)

#Make a prediction on the test set.
predictions = model.predict(X_test)

#Print the accuracy score.
print("Accuracy score: {}".format(accuracy_score(y_test, predictions)))

new_input = ["6408507648","6403601344","DKCPH","66565231"]
fitted_new_input = np.array([le_consignor.transform([new_input[0]])[0],
                                le_consignee.transform([new_input[1]])[0],
                                le_origin.transform([new_input[2]])[0],
                                le_carrier.transform([new_input[3]])[0]])
new_predictions = model.predict(fitted_new_input.reshape(1,-1))

print(le_target.inverse_transform(new_predictions))

最后,您的树预测:

['THBKK']

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 scikit learn 训练机器学习模型以进行时间序列预测

使用 scikit-learn 训练线性回归模型后,如何对原始数据集中不存在的新数据点进行预测?

如何使用scikit-learn加载先前保存的模型并使用新的训练数据扩展模型

Scikit-Learn / Pandas:根据用户输入使用保存的模型进行预测

从 scikit-learn 模型列表中迭代预测

在使用scikit_learn和pandas训练模型后,如何预测未来的数据(以我的情况为例)?

使用scikit-learn训练NER的NLP对数线性模型

如何使用scikit-learn训练XOR模型?

Scikit-Learn:使用DBSCAN预测新点

如何使用 SciKit-Learn 预测新的房价?

总结 Scikit-Learn 的线性回归的预测

如何使用 scikit learn 预测目标标签

Scikit Learn中用于预测的扩展功能

Scikit-learn - 我在预测什么?

从scikit-learn管道获取模型属性

scikit-learn进行回归模型评估

继承自scikit-learn的LassoCV模型

Scikit-Learn Imputer未输入

带有 SVR 的 Scikit-learn BaggingRegressor 训练速度快但预测速度慢

scikit-learn:如果在进行一次热编码后其功能少于训练/测试集,则如何预测新数据

使用scikit-learn预测单个值会导致ValueError

使用scikit-learn预测有趣的文章

如何在scikit-learn中预测时间序列?

如何使用scikit-learn评估预测的置信度得分

使用scikit-learn的Imputer模块预测缺失值

scikit-learn:如何缩减“ y”预测结果

使用scikit-learn预测电影评论

Scikit-Learn:如何检索KFold CV的预测概率?

Scikit-Learn决策树:预测的概率是a还是b?