4 回答

TA贡献1804条经验 获得超3个赞
max_reg = None #<--- add this to represent the regressor with maximum score
max_score = 0 #<--- add this to represent maximum score
t=() # <--- add this to tuple declaration
c_estimators = 100
for m in range(3,6) :
rf_reg = RandomForestRegressor(n_estimators =c_estimators , max_depth=m)
rf_reg = rf_reg.fit(X_train, Y_train)
rf_reg_score = rf_reg.score(X_test,Y_test)
t = (m,c_estimators,rf_reg.score) # tuple assignment
rf_reg_score = t[2]
print (t)
if rf_reg_score > max_score :
max_score = rf_reg_score
max_reg = rf_reg
t = (m,c_estimators) # tuple assignment
print (t)

TA贡献1877条经验 获得超1个赞
import sklearn.datasets as datasets
import sklearn.model_selection as model_selection
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
np.random.seed(100)
boston=datasets.load_boston()
X_train,X_test,Y_train,Y_test=train_test_split(boston.data,boston.target,random_state=30)
print(X_train.shape)
print(X_test.shape)
rf_reg = RandomForestRegressor()
rf_reg = rf_reg.fit(X_train, Y_train)
print(rf_reg.score(X_train,Y_train))
print(rf_reg.score(X_test,Y_test))
print(rf_reg.predict(X_test[0:2]))
li=[]
nestimators=100
for maxdepth in range(3,6) :
rf_reg1 = RandomForestRegressor(max_depth=maxdepth,n_estimators=nestimators)
rf_reg1 = rf_reg1.fit(X_train, Y_train)
li.append(rf_reg1.score(X_test,Y_test))
maxValue=max(li)
maxIndex=li.index(maxValue)
a=(maxIndex+3,nestimators)
print(a)
#This code 100% works ,i tested and got exact output and cleared HandsOn
添加回答
举报