The task is to train a random forest with a different number of trees from 1 to 50 and for each of the options to evaluate the quality of the forest obtained for cross-validation in 5 blocks. (sklearn.metrics.r2_score). I wrote this cycle:
from sklearn.ensemble import RandomForestRegressor from sklearn.cross_validation import KFold from sklearn.metrics import r2_score P_scores = [] p = np.linspace(1.0, 50.0, num=50) p1 = np.array(p) kf = KFold(4176, n_folds=5, random_state=1, shuffle=True) P = 0 while P < len(p1): regressor = RandomForestRegressor(n_estimators=P, random_state=1) regressor.fit(X, Y) predictions = clf.predict(X) r2_score(Y, predictions) P_scores.append(r2_score) print(P_scores) P += 1 But the error is always the same, whatever cycle I wrote:
IndentationError: expected an indented block How to fix it?
(I did the same for loop with indices [P], but the same error came out)
Update
True, I wrote the code in a notebook and just inserted it into the console without indentation. The code worked, because the error was different:
ValueError: n_estimators must be greater than zero, got 0.
But here you just need to start with 1.
Why then instead of the expected result of the vector containing the estimates for the iterations. I got:
<function r2_score at 0x0000023304775BF8> <function r2_score at 0x0000023304775BF8> ... <function r2_score at 0x0000023304775BF8> <function r2_score at 0x0000023304775BF8> and with the team
min(r2_score) TypeError: 'function' object is not iterable E = np.array(r2_score) min(E) TypeError: iteration over a 0-d array
while- there should be an indent inside the block. It has nothing to do with machine-learning .. - MaxU