我所理解decision tree的切入點是兩張圖:
這兩張圖表示的意思是一樣的,都是將已有的數據歸類。
預測使用的數據:
Position,Level,Salary
Business Analyst,1,45000
Junior Consultant,2,50000
Senior Consultant,3,60000
Manager,4,80000
Country Manager,5,110000
Region Manager,6,150000
Partner,7,200000
Senior Partner,8,300000
C-level,9,500000
CEO,10,1000000
預測python代碼:
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#Importing the dataset
dataset=pd.read_csv('Position_Salaries.csv')
x=dataset.iloc[:,1:2].values
y=dataset.iloc[:,2].values
#Fitting Decision Tree Regression to the dataset
from sklearn.tree import DecisionTreeRegressor
regressor=DecisionTreeRegressor(random_state=0)
regressor.fit(x,y)
#Predicting a new result
y_pred=regressor.predict(6.5)
# Visualising the Decision Tree Regression results(higher resolution)
x_grid=np.arange(min(x),max(x),0.1)
x_grid=x_grid.reshape(len(x_grid),1)
plt.scatter(x,y,color='red')
plt.plot(x_grid,regressor.predict(x_grid),color='blue')
plt.title('Truth or Bluff(Decision Tree Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
結果
困惑點:DecisionTreeRegressor(random_state=0)內部的是實現方式不清楚!三人行,必有我師焉,望能從大家的留言中得到進一步的答案,謝謝。
王家林老師人工智能AI第10課 老師微信13928463918