如何使用预训练模型填充缺失值?
How to fill missing value using pre-trained model?
我有一个时间序列索引,其中变量和湿度读数很少。我已经训练了一个 ML 模型来预测基于 X、Y 和 Z 的湿度值。现在,当我使用 pickle 加载保存的模型时,我想使用 X、Y 和 Z 填充湿度缺失值。但是,它应该考虑 X、Y 和 Z 本身不应该丢失的事实。
Time X Y Z Humidity
1/2/2017 13:00 31 22 21 48
1/2/2017 14:00 NaN 12 NaN NaN
1/2/2017 15:00 25 55 33 NaN
在这个例子中,湿度的最后一行将使用模型填充。而第二行不应由模型预测,因为 X 和 Z 也缺失。
到目前为止我已经试过了:
with open('model_pickle','rb') as f:
mp = pickle.load(f)
for i, value in enumerate(df['Humidity'].values):
if np.isnan(value):
df['Humidity'][i] = mp.predict(df['X'][i],df['Y'][i],df['Z'][i])
这给了我一个错误 'predict() takes from 2 to 5 positional arguments but 6 were given' 而且我没有考虑 X、Y 和 Z 列值。下面是我用来训练模型并将其保存到文件中的代码:
df = df.dropna()
dfTest = df.loc['2017-01-01':'2019-02-28']
dfTrain = df.loc['2019-03-01':'2019-03-18']
features = [ 'X', 'Y', 'Z']
train_X = dfTrain[features]
train_y = dfTrain.Humidity
test_X = dfTest[features]
test_y = dfTest.Humidity
model = xgb.XGBRegressor(max_depth=10,learning_rate=0.07)
model.fit(train_X,train_y)
predXGB = model.predict(test_X)
mae = mean_absolute_error(predXGB,test_y)
import pickle
with open('model_pickle','wb') as f:
pickle.dump(model,f)
我在训练和保存模型的过程中没有出错。
可以报错吗?
无论如何,如果您有缺失值,您有不同的选择来处理它们。您可以完全丢弃数据点,也可以尝试使用选择的方法推断缺失的部分:均值、插值等。
Pandas 文档有一个关于如何处理它们的很好的指南:
https://pandas.pydata.org/pandas-docs/stable/user_guide/missing_data.html
尝试
df['Humidity'][i] = mp.predict(df[['X', 'Y', 'Z']][i])
通过这种方式,您可以将数据作为单个参数传递,正如函数所期望的那样。按照您编写的方式,您将数据拆分为 3 个参数。
对于预测,因为你想确保你拥有所有的 X、Y、Z 值,你可以这样做,
df = df.dropna(subset = ["X", "Y", "Z"])
现在您可以预测剩余有效示例的值,
# where features = ["X", "Y", "Z"]
df['Humidity'] = mp.predict(df[features])
mp.predict 将对所有行进行 return 预测,因此无需迭代预测。
编辑:。
对于推理,假设你有一个数据框 df
,你可以这样做,
# Get rows with missing Humidity where it can be predicted.
df_inference = df[df.Humidity.isnull()]
# remaining rows
df = df[df.Humidity.notnull()]
# This might still have rows with missing features.
# Since you cannot infer with missing features, Remove them too and add them to remaining rows
df = df.append(df_inference[df_inference[features].isnull().any(1)])
# and remove them from df_inference
df_inference = df_inference[~df_inference[features].isnull().any(1)]
#Now you can infer on these rows
df_inference['Humidity'] = mp.predict(df_inference[features])
# Now you can merge this back to the remaining rows to get the original number of rows and sort the rows by index
df = df.append(df_inference)
df.sort_index()
我有一个时间序列索引,其中变量和湿度读数很少。我已经训练了一个 ML 模型来预测基于 X、Y 和 Z 的湿度值。现在,当我使用 pickle 加载保存的模型时,我想使用 X、Y 和 Z 填充湿度缺失值。但是,它应该考虑 X、Y 和 Z 本身不应该丢失的事实。
Time X Y Z Humidity
1/2/2017 13:00 31 22 21 48
1/2/2017 14:00 NaN 12 NaN NaN
1/2/2017 15:00 25 55 33 NaN
在这个例子中,湿度的最后一行将使用模型填充。而第二行不应由模型预测,因为 X 和 Z 也缺失。
到目前为止我已经试过了:
with open('model_pickle','rb') as f:
mp = pickle.load(f)
for i, value in enumerate(df['Humidity'].values):
if np.isnan(value):
df['Humidity'][i] = mp.predict(df['X'][i],df['Y'][i],df['Z'][i])
这给了我一个错误 'predict() takes from 2 to 5 positional arguments but 6 were given' 而且我没有考虑 X、Y 和 Z 列值。下面是我用来训练模型并将其保存到文件中的代码:
df = df.dropna()
dfTest = df.loc['2017-01-01':'2019-02-28']
dfTrain = df.loc['2019-03-01':'2019-03-18']
features = [ 'X', 'Y', 'Z']
train_X = dfTrain[features]
train_y = dfTrain.Humidity
test_X = dfTest[features]
test_y = dfTest.Humidity
model = xgb.XGBRegressor(max_depth=10,learning_rate=0.07)
model.fit(train_X,train_y)
predXGB = model.predict(test_X)
mae = mean_absolute_error(predXGB,test_y)
import pickle
with open('model_pickle','wb') as f:
pickle.dump(model,f)
我在训练和保存模型的过程中没有出错。
可以报错吗?
无论如何,如果您有缺失值,您有不同的选择来处理它们。您可以完全丢弃数据点,也可以尝试使用选择的方法推断缺失的部分:均值、插值等。
Pandas 文档有一个关于如何处理它们的很好的指南: https://pandas.pydata.org/pandas-docs/stable/user_guide/missing_data.html
尝试
df['Humidity'][i] = mp.predict(df[['X', 'Y', 'Z']][i])
通过这种方式,您可以将数据作为单个参数传递,正如函数所期望的那样。按照您编写的方式,您将数据拆分为 3 个参数。
对于预测,因为你想确保你拥有所有的 X、Y、Z 值,你可以这样做,
df = df.dropna(subset = ["X", "Y", "Z"])
现在您可以预测剩余有效示例的值,
# where features = ["X", "Y", "Z"]
df['Humidity'] = mp.predict(df[features])
mp.predict 将对所有行进行 return 预测,因此无需迭代预测。
编辑:。
对于推理,假设你有一个数据框 df
,你可以这样做,
# Get rows with missing Humidity where it can be predicted.
df_inference = df[df.Humidity.isnull()]
# remaining rows
df = df[df.Humidity.notnull()]
# This might still have rows with missing features.
# Since you cannot infer with missing features, Remove them too and add them to remaining rows
df = df.append(df_inference[df_inference[features].isnull().any(1)])
# and remove them from df_inference
df_inference = df_inference[~df_inference[features].isnull().any(1)]
#Now you can infer on these rows
df_inference['Humidity'] = mp.predict(df_inference[features])
# Now you can merge this back to the remaining rows to get the original number of rows and sort the rows by index
df = df.append(df_inference)
df.sort_index()