mirror of
https://github.com/apachecn/ailearning.git
synced 2026-06-30 10:16:12 +08:00
更新决策树的内容,做ppt
This commit is contained in:
50
src/python/09.RegTrees/RTSklearn.py
Normal file
50
src/python/09.RegTrees/RTSklearn.py
Normal file
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/python
|
||||
# coding:utf8
|
||||
|
||||
'''
|
||||
Created on 2017-03-10
|
||||
Update on 2017-03-10
|
||||
author: jiangzhonglian
|
||||
content: 回归树
|
||||
'''
|
||||
|
||||
print(__doc__)
|
||||
|
||||
|
||||
# Import the necessary modules and libraries
|
||||
import numpy as np
|
||||
from sklearn.tree import DecisionTreeRegressor
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
# Create a random dataset
|
||||
rng = np.random.RandomState(1)
|
||||
X = np.sort(5 * rng.rand(80, 1), axis=0)
|
||||
y = np.sin(X).ravel()
|
||||
print X, '\n\n\n-----------\n\n\n', y
|
||||
y[::5] += 3 * (0.5 - rng.rand(16))
|
||||
|
||||
|
||||
# Fit regression model
|
||||
regr_1 = DecisionTreeRegressor(max_depth=2, min_samples_leaf=5)
|
||||
regr_2 = DecisionTreeRegressor(max_depth=5, min_samples_leaf=5)
|
||||
regr_1.fit(X, y)
|
||||
regr_2.fit(X, y)
|
||||
|
||||
|
||||
# Predict
|
||||
X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis]
|
||||
y_1 = regr_1.predict(X_test)
|
||||
y_2 = regr_2.predict(X_test)
|
||||
|
||||
|
||||
# Plot the results
|
||||
plt.figure()
|
||||
plt.scatter(X, y, c="darkorange", label="data")
|
||||
plt.plot(X_test, y_1, color="cornflowerblue", label="max_depth=2", linewidth=2)
|
||||
plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2)
|
||||
plt.xlabel("data")
|
||||
plt.ylabel("target")
|
||||
plt.title("Decision Tree Regression")
|
||||
plt.legend()
|
||||
plt.show()
|
||||
@@ -7,14 +7,14 @@ Update on 2017-03-08
|
||||
Tree-Based Regression Methods Source Code for Machine Learning in Action Ch. 9
|
||||
@author: jiangzhonglian
|
||||
'''
|
||||
import regTrees
|
||||
from Tkinter import *
|
||||
from numpy import *
|
||||
import regTrees
|
||||
|
||||
import matplotlib
|
||||
matplotlib.use('TkAgg')
|
||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||
from matplotlib.figure import Figure
|
||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||
matplotlib.use('TkAgg')
|
||||
|
||||
|
||||
def test_widget_text(root):
|
||||
|
||||
Reference in New Issue
Block a user