The task is to train a random forest with a different number of trees from 1 to 50 and, for each of the options, evaluate the quality of the resulting forest for cross-validation by 5 blocks. (sklearn.metrics.r2_score). I wrote a loop like this:
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 & lt; 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, no matter what cycle I write:
IndentationError: expected an indented block
How to fix it?
(I did the same for
loop with [P] indices, but the same error came out)
Update
True, I wrote the code in notepad and just pasted 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 from 1.
For some reason, instead of the expected result of a vector containing estimates for iterations. I got:
& lt; function r2_score at 0x0000023304775BF8 & gt;
& lt; function r2_score at 0x0000023304775BF8 & gt;
...
& lt; function r2_score at 0x0000023304775BF8 & gt;
& lt; function r2_score at 0x0000023304775BF8 & gt;
and with command
min (r2_score)
TypeError: 'function' object is not iterable
E = np.array (r2_score)
min (E)
TypeError: iteration over a 0-d array
Answer 1, authority 100%
IndentationError: expected an indented block = Indentation error.
By PEP standards, it is customary to indent 4 spaces, but the interpreter will work even if you select 1 space or 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 & lt; 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 mixing tabs and spaces. For example, this happens if you write code in an unconfigured 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")