旋轉隨機森林演算法

來源:互聯網
上載者:User

標籤:ica   int   影響   models   uil   它的   法則   position   失效   

 

  當輸入資料中存在非線性關係的時候,基於線性迴歸的模型就會失效,而基於樹的演算法則不受資料中非線性關係的影響,基於樹的方法最大的一個困擾時為了避免過擬合而對樹進行剪枝的難度,對於潛在資料中的雜訊,大型的樹傾向於受影響,導致低偏差(過度學習)或高方差(極度不擬合)。不過如果我們產生大量的樹,最終的預測值採用整合所有樹產生的輸出的平均值,就可以避免方差的問題。


1. 隨機森林:整合技術,採用大量的樹來建模,但這裡我們要保證樹之間沒有相互關聯,不能選擇所有屬性,而是隨機播放一個屬性的子集給某個樹。雖然我們再隨機森林中構建最大深度的樹,這樣它們可以很好適應自舉的樣本,得到的偏差較低,後果是引入了高方差,但通過構建大量樹,使用平均法則作為最後的預測值,可以解決方差問題。

2. 超隨機樹:比隨機森林引入更多隨機化,可以更高效地解決方差問題,它的運算複雜度也略有降低。隨機森林是自舉部分執行個體來給每棵樹,但超隨機樹是使用完整的訓練集資料,另外關於給定K作為給定節點隨機播放的屬性數量,它隨機播放割點,不考慮目標變數,不像隨機森林那樣基於基尼不純度或熵標準。這種更多隨機化帶來的架構可以更好的降低方差。而且由於劃分節點不需要相關標準,因此不需要花費時間來評鑑最適合用來劃分資料集的屬性。

3. 旋轉森林:前兩種需要整合大量的樹才能獲得好效果,而旋轉森林可以用較小的樹來擷取相同甚至更好的效果。演算法情境是投票情境,屬性被劃分為大小相等的K個不重疊的子集,然後結合PCA、旋轉矩陣來完成模型的構建。

下面是代碼:

  1 # -*- coding: utf-8 -*-  2 """  3 Created on Wed Apr 11 17:01:22 2018  4 @author: Alvin AI  5 """  6   7 from sklearn.datasets import make_classification  8 from sklearn.metrics import classification_report  9 from sklearn.cross_validation import train_test_split 10 from sklearn.decomposition import PCA 11 from sklearn.tree import DecisionTreeClassifier 12 import numpy as np 13  14  15 # 載入資料 16 def get_data(): 17     no_features = 50 18     redundant_features = int(0.1 * no_features) 19     informative_features = int(0.6 * no_features) 20     repeated_features = int(0.1 * no_features) 21     x, y = make_classification(n_samples=500, n_features=no_features,  22                                flip_y=0.03, n_informative=informative_features,  23                                n_redundant=redundant_features,  24                                n_repeated=repeated_features, random_state=7) 25     return x, y 26  27  28 # 得到隨機子集 29 def get_random_subset(iterable, k): 30     subsets = [] 31     iteration = 0 32     np.random.shuffle(iterable)  # 打亂特徵索引 33     subset = 0 34     limit = len(iterable) / k 35     while iteration < limit: 36         if k <= len(iterable): 37             subset = k 38         else: 39             subset = len(iterable) 40         subsets.append(iterable[-subset:]) 41         del iterable[-subset:] 42         iteration += 1 43     return subsets 44  45  46 # 建立旋轉森林模型 47 def build_rotationtree_model(x_train, y_train, d, k): 48     models = []  # 決策樹 49     r_matrices = []  # 與樹相關的旋轉矩陣 50     feature_subsets = []  # 迭代中用到的特徵子集 51     for i in range(d): 52         x, _, _, _ = train_test_split(x_train, y_train, test_size=0.3, random_state=7) 53         # 特徵的索引 54         feature_index = range(x.shape[1]) 55         # 擷取特徵的子集 56         # 10個子集,每個子集包含5個索引 57         random_k_subset = get_random_subset(feature_index, k)  # 10個子集 58         feature_subsets.append(random_k_subset)  # 25個樹,每個樹10個子集 59         R_matrix = np.zeros((x.shape[1], x.shape[1]), dtype=float)  # 旋轉矩陣 60         for each_subset in random_k_subset: 61             pca = PCA() 62             x_subset = x[:, each_subset]  # 提取出子集內索引對應的x值 63             pca.fit(x_subset)  # 主成分分析 64             for ii in range(0, len(pca.components_)): 65                 for jj in range(0, len(pca.components_)): 66                     R_matrix[each_subset[ii], each_subset[jj]] =  67                         pca.components_[ii, jj] 68         x_transformed = x_train.dot(R_matrix) 69  70         model = DecisionTreeClassifier() 71         model.fit(x_transformed, y_train) 72         models.append(model) 73         r_matrices.append(R_matrix) 74     return models, r_matrices, feature_subsets 75  76  77 def model_worth(models, r_matrices, x, y): 78     predicted_ys = [] 79     for i, model in enumerate(models): 80         x_mod = x.dot(r_matrices[i]) 81         predicted_y = model.predict(x_mod) 82         predicted_ys.append(predicted_y) 83  84         predicted_matrix = np.asmatrix(predicted_ys)  # 轉化為矩陣 25*350 85  86         final_prediction = [] 87         for i in range(len(y)): 88             pred_from_all_models = np.ravel(predicted_matrix[:, i])  # 將多維陣列降為一維 89             non_zero_pred = np.nonzero(pred_from_all_models)[0]  # nonzeros(a)返回數組a中值不為零的元素的下標 90             is_one = len(non_zero_pred) > len(models) / 2  # 如果非0預測大於模型內樹的總數的一半則為1 91             final_prediction.append(is_one) 92         print classification_report(y, final_prediction) 93     return predicted_matrix 94  95  96 # 主函數 97 if __name__ == "__main__": 98     x, y = get_data() 99     # 資料集劃分100     x_train, x_test_all, y_train, y_test_all = train_test_split(x, y, 101                                                                 test_size=0.3, random_state=9)102     x_dev, x_test, y_dev, y_test = train_test_split(x_test_all, y_test_all, 103                                                     test_size=0.3, random_state=9)104     models, r_matrices, features = build_rotationtree_model(x_train, y_train, 25, 5)  # 輸的數量25,要用的特徵子集5105     predicted_matrix1 = model_worth(models, r_matrices, x_train, y_train)106     predicted_matrix2 = model_worth(models, r_matrices, x_dev, y_dev)

詳情參見80090175

旋轉隨機森林演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.