Reputation:
i have used following code from machine Learning book
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import mglearn
X,y =make_moons(n_samples=100,noise=0.25,random_state=3)
X_train,X_test,y_train,y_test =train_test_split(X,y,stratify=y,random_state=42)
#sketch random forest
forest =RandomForestClassifier(n_estimators=5,random_state=2)
forest.fit(X_train,y_train)
#draw random forest
fix, axes =plt.subplots(2,3,figsize=(20,10))
for i,(ax,tree) in enumerate(list(zip(axes.ravel())),forest.estimators_):
ax.set_title("tree{}".format(i))
mglearn.plots.plot_tree_partition(X_train,y_train,tree,ax=ax)
mglearn.plots.plot_2d_separator(forest, X_train, fill=True, ax=axes[-1, -1],alpha=.4)
axes[-1, -1].set_title("Random Forest")
mglearn.discrete_scatter(X_train[:, 0], X_train[:, 1], y_train)
it says following error :
TypeError: 'list' object cannot be interpreted as an integer
i know that in python3 , for zip there is necessary list command, so in book originally it was written
for i, (ax, tree) in enumerate(zip(axes.ravel(), forest.estimators_)):
and i have added list command, but still it shows me this error, can you help me to clarify what is wrong?
Upvotes: 1
Views: 131
Reputation: 4481
In
enumerate(list(zip(axes.ravel())),forest.estimators_)
forest.estimators_
is outside your list(zip())
call and is treated as the second argument for enumerate
, which, from the docs, represents the start index. Since forest.estimators_
is a list, this will fail as an integer is required.
What you mean to write is:
enumerate(list(zip(axes.ravel(), forest.estimators_)))
Upvotes: 1