- 데이터셋 : make_moons from sklearn.datasets
In [1]:
#-*- coding:utf-8 -*-
# 랜덤 포레스트 분석
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
import mglearn
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
# make_moons : 샘플 데이터 셋
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)
forest = RandomForestClassifier(n_estimators = 5, random_state = 2)
forest.fit(x_train, y_train)
Out[1]:
In [2]:
# 랜덤 포레스트 영역 출력
fig, axes = plt.subplots(2, 3, figsize = (20, 10))
for i, (ax, tree) in enumerate(zip(axes.ravel(), forest.estimators_)):
ax.set_title("Tree {}".format(i))
mglearn.plots.plot_tree_partition(x, y, tree, ax = ax)
mglearn.plots.plot_2d_separator(forest, x, fill = True, ax = axes[-1, -1], alpha = .4)
axes[-1, -1].set_title("Random Forest")
mglearn.discrete_scatter(x[:,0], x[:, 1], y)
Out[2]:
In [3]:
print("훈련 세트 정확도 : {:.3f}".format(forest.score(x_train, y_train)))
print("테스트 세트 정확도 : {:.3f}".format(forest.score(x_test, y_test)))
'Python > 빅데이터분석' 카테고리의 다른 글
SVC - Support Vector Classification (0) | 2019.06.23 |
---|---|
Regression, Tree (0) | 2019.06.23 |
LinearRegression, DecisionTreeRegression (0) | 2019.06.23 |
LinearRegression, Ridge, Lasso 알고리즘 (0) | 2019.05.27 |
붓꽃데이터를 이용한 KNN 분류모델 사용 (0) | 2019.04.20 |