極限森林

weixin_45540546發表於2020-10-06

極限森林

from sklearn.ensemble import ExtraTreesClassifier,RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import cross_val_score
from sklearn import datasets
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
#決策樹,進行裂分是根據資訊增益最大進行裂分
#極限森林:樣本隨機,分裂條件隨機(不是分類最好的條件)

X,y = datasets.load_wine(True)
clf = DecisionTreeClassifier()
cross_val_score(clf,X,y,cv = 6,scoring='accuracy').mean()#交叉驗證
forest = RandomForestClassifier(n_estimators =100)
cross_val_score(forest,X,y,cv = 6,scoring='accuracy').mean()#交叉驗證
extra = ExtraTreesClassifier(n_estimators =100,)
cross_val_score(extra,X,y,cv = 6,scoring='accuracy').mean()#交叉驗證

梯度提升的原理

import numpy as np
#梯度
#引入迴歸,迴歸是分類的極限思想
#分類的列別多到一定程度,就可以看作迴歸
from sklearn.ensemble import GradientBoostingClassifier,GradientBoosingRegressor
from sklearn import datasets
from sklearn.model_selection import train_test_split
X,y = datasets.load_iris(True)
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2)
gbdt = GradientBoostingClassifier(n_estimators = 10)
gbdt.fit(X_train,y_train)
gbdt.score(X_test,y_test)
#梯度提升的原理

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.ensemble import GradientBoostingClassifier
from sklearn import tree
#X:資料  上網時間和購物金額
#y:目標值
X = np.array([[800,3],[1200,1],[1800,4],[2500,2]])
y = np.array([14,16,24,26])

```python
gbdt = GradientBoostingClassifier(n_estimators = 10)
gbdt.fit(X,y)
#使用迴歸去學習
gbdt = GradientBoostingRegressor(n_estimators = 10)
gbdt.fit(X,y)
gbdt.predict(X)
plt.figure(figsize=(9,6))
tree.plot_tree(gbdt[0,0],filled = True,feature_names = ['消費','上網'])

梯度下降

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
f = lambda x : (x-3)**2 + 2.5*x - 7.5
f
x = np.linspace(-2,5,100)
y = f(x)
plt.plot(x,y)```

```python
#導數函式
d = lambda x:2*(x-3) + 2.5
#學習率
learning_rate = 0.1
min_value = np.random.randint(-3,5,size = 1)[0]
print(min_value)
#退出條件
min_value_last = min_value +0.1

#tollerence容忍度,誤差在萬分之一,任務結束
tol = 0.0001

while True:
    if np.abs(min_value-min_value_last)< tol:
        break
#梯度下降
    min_value_last = min_value
#更新值:梯度下降
    min_value = min_value - learning_rate*d(min_value)
    print(min_value)
print(min_value)

相關文章