简体   繁体   中英

ValueError: too many values to unpack (expected 4) Python

I created a function that find the best parameters for Random Forest Classifier, but I riceved this error "ValueError: too many values to unpack (expected 4)". I don't want to use RandomSearchCV or GridSearchCV. Could anyone help me? The code is the following:

depth_options= range(8,30)
sample_split_options= range(2, 200)
sample_leaf_options= range(1, 100)
estimator_options= range(10, 1000)

# for loop to iterate for each leaf size
for leaf_size,split_size,n_est,depth in sample_leaf_options, sample_split_options, depth_options, estimator_options  :
model = RandomForestClassifier(n_estimators = n_est, n_jobs = -1,random_state = 42, 
min_samples_leaf = leaf_size, min_samples_split=split_size, max_depth=depth)
model.fit(X4_train,y_train)
print(leaf_size, split_size, n_est, depth)
print ("AUC - ROC : ", roc_auc_score(y_test,model.predict_proba(X4_test)[:,1]))

You need separate for loops:

for leaf_size in sample_leaf_options:
    for split_size in sample_split_options:
        for n_est in depth_options:
            for depth in estimator_options:
                model = RandomForestClassifier(n_estimators = n_est, n_jobs = -1,random_state = 42, min_samples_leaf = leaf_size, min_samples_split=split_size, max_depth=depth)
                model.fit(X4_train,y_train)
                print(leaf_size, split_size, n_est, depth)
                print ("AUC - ROC : ", roc_auc_score(y_test,model.predict_proba(X4_test)[:,1]))

Edit: To save the highest score:

highest_score = 0
highest_leaf_size = 0
highest_split_size = 0
highest_n_est = 0
highest_depth = 0

# for loop to iterate for each leaf size
for leaf_size in sample_leaf_options:
    for split_size in sample_split_options:
        for n_est in depth_options:
            for depth in estimator_options:
                model = RandomForestClassifier(n_estimators = n_est, n_jobs = -1,random_state = 42, min_samples_leaf = leaf_size, min_samples_split=split_size, max_depth=depth)
                model.fit(X4_train,y_train)
                score = roc_auc_score(y_test,model.predict_proba(X4_test)[:,1])
                if score > highest_score:
                    highest_score = score
                    highest_leaf_size = leaf_size
                    highest_split_size = split_size
                    highest_n_est = n_est
                    highest_depth = depth
print(highest_leaf_size, highest_split_size, highest_n_est, highest_depth)
print ("AUC - ROC : ", highest_score)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM