【Python学习系列十】Python机器学习库scikit-learn实现Decision Trees案例
生活随笔
收集整理的這篇文章主要介紹了
【Python学习系列十】Python机器学习库scikit-learn实现Decision Trees案例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
學(xué)習(xí)網(wǎng)址:http://scikit-learn.org/stable/modules/tree.html
scikit-learn這個官網(wǎng)很好,里面有算法案例也有算法原理說明。
案例代碼:
# -*- coding: utf-8 -*- __author__ = 'Jason.F'#http://scikit-learn.org/stable/modules/tree.htmlimport numpy as np import matplotlib.pyplot as plt from sklearn.tree import DecisionTreeRegressor# Create a random dataset rng = np.random.RandomState(1) X = np.sort(200 * rng.rand(100, 1) - 100, axis=0) y = np.array([np.pi * np.sin(X).ravel(), np.pi * np.cos(X).ravel()]).T y[::5, :] += (0.5 - rng.rand(20, 2))# Fit regression model regr_1 = DecisionTreeRegressor(max_depth=2) regr_2 = DecisionTreeRegressor(max_depth=5) regr_3 = DecisionTreeRegressor(max_depth=8) regr_1.fit(X, y) regr_2.fit(X, y) regr_3.fit(X, y)# Predict X_test = np.arange(-100.0, 100.0, 0.01)[:, np.newaxis] y_1 = regr_1.predict(X_test) y_2 = regr_2.predict(X_test) y_3 = regr_3.predict(X_test)# Plot the results plt.figure() s = 50 plt.scatter(y[:, 0], y[:, 1], c="navy", s=s, label="data") plt.scatter(y_1[:, 0], y_1[:, 1], c="cornflowerblue", s=s, label="max_depth=2") plt.scatter(y_2[:, 0], y_2[:, 1], c="c", s=s, label="max_depth=5") plt.scatter(y_3[:, 0], y_3[:, 1], c="orange", s=s, label="max_depth=8") plt.xlim([-6, 6]) plt.ylim([-6, 6]) plt.xlabel("target 1") plt.ylabel("target 2") plt.title("Multi-output Decision Tree Regression") plt.legend() plt.show()結(jié)果:
對scikit-learn庫大體上拿決策樹和支持向量機來了解,后面就是要具體應(yīng)用。
總結(jié)
以上是生活随笔為你收集整理的【Python学习系列十】Python机器学习库scikit-learn实现Decision Trees案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python学习系列九】Python机
- 下一篇: 【正一专栏】轮回-从坚信去年骑士会逆转到