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 
  • 2
    This is a purely Python error - you need to correctly copy the block inside the while - there should be an indent inside the block. It has nothing to do with machine-learning .. - MaxU
  • one
    @user21 I would recommend that you (before programming machine learning) familiarize yourself with the basics of the syntax of the language. - andy.37
  • one
    Where can I get the necessary minimum for this? Is the Python book for data anlysis right? - user21
  • one
    in my opinion, it makes sense to rename and update the question, because in the form of a comment it is almost not “readable” ... - MaxU
  • one
    @ user21 "Dive into Python" M. Pilgrim - andy.37

1 answer 1

IndentationError: expected an indented block = Indent error.

According to the PEP standards, it is customary to indent 4 spaces, but the interpreter will work even if you select 1 with a space or a tab.

The code must be formatted. Nesting should be visually displayed inside the loop.

 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 

The second case when this error occurs is the mixing of tabs and spaces. For example, this happens if you write code in a non-configured Sublime Text editor.

An example with a high level of nesting and how it should be displayed.

 product = 0 for product in products: print(product) for attribute in product.specifications: print(attribute) if attribute['size']: for size in attribute['size']: try: size * 2 except: size = 0 print("hello")