Issue
I have twelve different datasets which aim at creating twelve different random forest models. I want to create a for-loop but I don't know how to store each model and name each model according to the correlated datasets (codes are depicted below). I am in the learning phase of learning python and machine learning using Scikit learn. Any comments or suggestions are welcome.
data = [df_AFC,df_AF,df_ESF,df_EXF,df_F,df_GF,df_KRFC,df_KRF,df_MF,df_PF,df_SFC,df_SF]
name = ['AFC','AF','ESF','EXF','F','GF','KRFC','KRF','MF','PF','SFC','SF']
#fix_random_state=42
result = []
for i,j in zip(data,name):
x = i.drop('class', axis=1)
y = i['class']
# rus = RandomUnderSampler(sampling_strategy="not minority") # String
rus = RandomUnderSampler(sampling_strategy=1, random_state=41) # Numerical value
x_res, y_res = rus.fit_resample(x, y)
#Remove low variance features
#replace with x_res, y_res from now on
remove_low_variance(x_res, threshold=0.1)
#Data splitting
x_train, x_test, y_train, y_test = train_test_split(x_res, y_res, test_size=0.2, random_state=42)
x_train.shape, x_test.shape
#Model building
model = RandomForestClassifier(n_estimators=500, random_state=42)
model.fit(x_train, y_train)
result.append(model_i)
expected outputs
y_train_pred_AFC = model_AFC.predict(unknown)
y_train_pred_AF = model_AF.predict(unknown)
...
Solution
I'd recommend storing the models in a dictionary like this:
data_sets = [df_AFC,df_AF,df_ESF,df_EXF,df_F,df_GF,df_KRFC,df_KRF,df_MF,df_PF,df_SFC,df_SF]
names = ['AFC','AF','ESF','EXF','F','GF','KRFC','KRF','MF','PF','SFC','SF']
#fix_random_state=42
models = {}
for data, name in zip(data_sets, names):
# <<< REMOVED CODE >>>
#Model building
model = RandomForestClassifier(n_estimators=500, random_state=42)
model.fit(x_train, y_train)
models[name] = model
Then you can call the trained model to make predictions in another loop like this:
predictions = {}
for name, model in models.items():
predictions[name] = model.predict(test_data)
Or one by one:
y_train_pred_AFC = models['AFC'].predict(test_data)
#...
Answered By - TC Arlen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.