ageron / ageron/handson-ml2

Chapter 6 Exercise 8 Grow a forest 'continuous-multioutput'

未关闭
#292 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Jupyter Notebook
星标
30k
派生
13.1k
PR 合并指标
30 天内没有已合并 PR

描述

# Context: Exercise 7

**a**

from sklearn.datasets import make_moons

X, y = make_moons(n_samples=10000, noise=0.4, random_state=42)

**b**

from sklearn.model_selection import train_test_split


x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

**c**

from sklearn.model_selection import GridSearchCV


from sklearn.tree import DecisionTreeClassifier


params={"max_leaf_nodes":list(range(0,100)), "min_samples_split":[2,3,4,5]}

tree_clf=DecisionTreeClassifier(random_state=42)


model = GridSearchCV(tree_clf, params, cv=3, verbose=1)
model.fit(x_train, y_train)

# Code Exercise 8

**a**

from sklearn.model_selection import ShuffleSplit


n_trees = 1000
n_instances = 100


mini_sets = []


rs = ShuffleSplit(n_splits=n_trees, test_size=len(x_train) - n_instances, random_state=42)


for mini_train_index, mini_test_index in rs.split(x_train):


x_mini_train = x_train[mini_train_index]


y_mini_train = x_train[mini_train_index]


mini_sets.append((x_mini_train, y_mini_train))

**b**

from sklearn.base import clone


forest = [clone(model.best_estimator_) for _ in range(n_trees)]


accuracy_scores = []


for tree, (x_mini_train, y_mini_train) in zip(forest, mini_sets):


tree.fit(x_mini_train, y_mini_train)


y_pred = tree.predict(x_test)


accuracy_scores.append(accuracy_score(y_test, y_pred))


np.mean(accuracy_scores)

## Obtained error
*----> 8 tree.fit(x_mini_train, y_mini_train)*
...
**ValueError:** Unknown label type: **'continuous-multioutput'**

## How to fixe it and why does it happen?

Thanks!

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。