[英]Scikitlearn - order of fit and predict inputs, does it matter?
刚开始使用这个库...使用RandomForestClassifiers有一些问题(我已经阅读过文档,但没有弄清楚)
我的问题非常简单,比方说我有一个火车数据集
ABC
1 2 3
其中A是自变量(y),BC是因变量(x)。 假设测试集看起来相同,但顺序是
BAC
1 2 3
当我调用forest.fit(train_data[0:,1:],train_data[0:,0])
然后我需要在运行之前重新排序测试集以匹配此顺序吗? (忽略我需要删除已经预测的y值(a)的事实,所以让我们说B和C乱序......)
是的,你需要重新排序它们。 想象一个更简单的案例,线性回归。 该算法将计算每个特征的权重,因此,例如,如果特征1不重要,则将为其分配接近0权重。
如果在预测时间顺序不同,则一个重要特征将乘以这几乎为零的权重,并且预测将完全关闭。
elyase是正确的。 scikit-learn
将以您给出的任何顺序简单地获取数据。 因此,您必须确保在训练和预测时间内数据的顺序相同。
这是一个简单的说明示例:
训练时间:
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier()
x = pd.DataFrame({
'feature_1': [0, 0, 1, 1],
'feature_2': [0, 1, 0, 1]
})
y = [0, 0, 1, 1]
model.fit(x, y)
# we now have a model that
# (i) predicts 0 when x = [0, 0] or [0, 1], and
# (ii) predicts 1 when x = [1, 0] or [1, 1]
预测时间:
# positive example
http_request_payload = {
'feature_1': 0,
'feature_2': 1
}
input_features = pd.DataFrame([http_request_payload])
model.predict(input_features) # this returns 0, as expected
# negative example
http_request_payload = {
'feature_2': 1, # notice that the order is jumbled up
'feature_1': 0
}
input_features = pd.DataFrame([http_request_payload])
model.predict(input_features) # this returns 1, when it should have returned 0.
# scikit-learn doesn't care about the key-value mapping of the features.
# it simply vectorizes the dataframe in whatever order it comes in.
这是我在训练期间缓存列顺序的方式,以便我可以在预测时间内使用它。
# training
x = pd.DataFrame([...])
column_order = x.columns
model = SomeModel().fit(x, y) # train model
# save the things that we need at prediction time. you can also use pickle if you don't want to pip install joblib
import joblib
joblib.dump(model, 'my_model.joblib')
joblib.dump(column_order, 'column_order.txt')
# load the artifacts from disk
model = joblib.load('linear_model.joblib')
column_order = joblib.load('column_order.txt')
# imaginary http request payload
request_payload = { 'feature_1': ..., 'feature_1': ... }
# create empty dataframe with the right shape and order (using column_order)
input_features = pd.DataFrame([], columns=column_order)
input_features = input_features.append(request_payload, ignore_index=True)
input_features = input_features.fillna(0) # handle any missing data however you like
model.predict(input_features.values.tolist())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.