我有以下数据:
Year_Month Country Type Data
2019_01 France IT 20
2019_02 France IT 30
2019_03 France IT 40
2019_01 France AT 10
2019_02 France AT 15
2019_03 France AT 20我想对Year_Month "2019_04“的组合法国和IT & AT分别进行预测。
因此,例如,我应该得到如下结果:
(法国、信息技术)预测:
Year_Month Country Type Data
2019_04 France IT 50(法国、AT)的预测:
Year_Month Country Type Data
2019_04 France AT 25如何设计循环,使具有模型的函数能够一次为每个组合运行并保存输出?
发布于 2020-01-22 09:56:09
谢谢!为我工作的是comboList=list(地图‘国家’,地图‘类型’)
comboList
对于i,组合体枚举(ComboList):打印(组合体)subset=data[(数据‘’country‘=组合框)&(Data’‘type’=combo1)] subset=subset["Data"]
x_train_ts, y_train_ts, x_test_ts, y_test_ts = data(subset,10, 1)
trials = Trials()
best = fmin(create_model_hypopt,
space=search_space,
algo=tpe.suggest,
max_evals=1,
trials=trials)
loss=trials.losses()
loss.append(loss)发布于 2020-01-21 15:58:35
虽然你的问题还有很多问题(你想用哪种模型来预测?))您可以从使用scikit的sklearn.linear_model开始-学习并计算每种类型的预测:
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
# Generate data from the example
df = pd.DataFrame({
'Year_Month': {0: '2019_01',1: '2019_02',2: '2019_03',3: '2019_01',4: '2019_02',5: '2019_03'},
'Country': { 0: 'France', 1: 'France', 2: 'France', 3: 'France', 4: 'France', 5: 'France'},
'Type': {0: 'IT', 1: 'IT', 2: 'IT', 3: 'AT', 4: 'AT', 5: 'AT'},
'Data': {0: 20, 1: 30, 2: 40, 3: 10, 4: 15, 5: 20}})
# Generate our empty regressor to fit the trend.
regressor = LinearRegression()
result = {}
# loop on every type
for t in df['Type'].unique():
# slice
df_slice = df[df['Type'] == t]
# train the regressor
regressor.fit(X=df_slice['Year_Month'].to_numpy().reshape(-1, 1), y=df_slice['Data'])
# predict new values
result[t] = {'predicted_value': regressor.predict(np.array([201904]).reshape(-1, 1))}
# build dataframe with all your results
final_df = pd.DataFrame(result)
# IT AT
# predicted_value [50.0] [25.0]https://stackoverflow.com/questions/59843432
复制相似问题