如何创建机器学习模型列表?

时间:2020-08-12 13:59:21

标签: python pandas list numpy machine-learning

我正在针对30个不同的数据集(具有相同维度)训练一系列机器学习模型。我想将这些模型存储在列表中。所有模型都是相同的,即DecisionTreeRegressor(),但每个模型都在不同的数据集上训练。

model_list = []
for i in range(30):
    model = DecisionTreeRegressor(max_depth= None, criterion ='mse')
    model.fit(x[i], y[i])
    model_list.append(model)

我可以使用上面的代码,但是它将为每个模型(即“模型”)以相同的名称存储所有模型。我想用不同的名称存储每个名称,例如['model_1','model_2','model_3'等。 ]

以下是数据x和y的尺寸。供你参考。请帮忙。

x.shape = 30y.shape = 30

x[0].shape = (500, 5) and y[0].shape = (500) 

1 个答案:

答案 0 :(得分:1)

test_model = 'x[0].shape = (500, 5) and y[0].shape = (500)'

您可以按照以下步骤进行操作,也可以在for循环中添加名称

model_df = pd.DataFrame()

for i in range(30):
    model_df = model_df.append([test_model])
model_df=model_df.reset_index(drop=True)
model_df['model_name'] = ['model no: ']+model_df.index.astype(str)
print(model_df)
                                               0    model_name
0   x[0].shape = (500, 5) and y[0].shape = (500)   model no: 0
1   x[0].shape = (500, 5) and y[0].shape = (500)   model no: 1
2   x[0].shape = (500, 5) and y[0].shape = (500)   model no: 2
3   x[0].shape = (500, 5) and y[0].shape = (500)   model no: 3
4   x[0].shape = (500, 5) and y[0].shape = (500)   model no: 4
5   x[0].shape = (500, 5) and y[0].shape = (500)   model no: 5
6   x[0].shape = (500, 5) and y[0].shape = (500)   model no: 6
7   x[0].shape = (500, 5) and y[0].shape = (500)   model no: 7
8   x[0].shape = (500, 5) and y[0].shape = (500)   model no: 8
9   x[0].shape = (500, 5) and y[0].shape = (500)   model no: 9
10  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 10
11  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 11
12  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 12
13  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 13
14  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 14
15  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 15
16  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 16
17  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 17
18  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 18
19  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 19
20  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 20
21  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 21
22  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 22
23  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 23
24  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 24
25  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 25
26  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 26
27  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 27
28  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 28
29  x[0].shape = (500, 5) and y[0].shape = (500)  model no: 29