使用的數據還是上一篇博客https://blog.csdn.net/HereIcome/article/details/80435395中的數據,只是訓練的模型使用random forest regression模型。
from sklearn.ensemble import RandomForestRegressor
regressor=RandomForestRegressor(n_estimators=100,random_state=0) //n_estimators是可以調節的參數。
regressor.fit(x,y) //x,y分別是level、salary
運行結果圖:
理解RandomForestRegressor:
參考博客鏈接:https://blog.csdn.net/qq_16633405/article/details/61200502,鏈接作者:春雨裏de太陽
RandomForestRegressor模型建立在DecisionTreeRegressor之上的,若干個DecisionTree組成RandomForest,他的計算過程如下:
step1:Pick at random K data points from the Training set.
step2:Build the Decision Tree associated to these K data points.
step3:Choose the number Ntree of trees you want to build and repeat STEP1&2
step4:For a new data point,make each one of your Ntree trees predict the value of Y to for the data point in question,and assign the new data point the average across all of the predicted Y values.
其中參數 n_estimators : integer, optional (default=10) ,The number of trees in the forest.
較多的子樹可以讓模型有更好的性能,但同時讓你的代碼變慢。 你應該選擇儘可能高的值,只要你的處理器能夠承受的住,因爲這使你的預測更好更穩定。
引用春雨裏de太陽所說的:就像是隨機森林,支持向量機,神經網絡等機器學習工具都具有高性能。 他們有很高的性能,但用戶一般並不瞭解他們實際上是如何工作的。 不知道該模型的統計信息不是什麼問題,但是不知道如何調整模型來擬合訓練數據,這將會限制用戶使用該算法來充分發揮其潛力。
王家林老師人工智能AI第11課 老師微信13928463918