python算法演練_One Rule 算法(詳解)
這樣某一個特征只有0和1兩種取值,數(shù)據(jù)集有三個類別。當取0的時候,假如類別A有20個這樣的個體,類別B有60個這樣的個體,類別C有20個這樣的個體。所以,這個特征為0時,最有可能的是類別B,但是,還是有40個個體不在B類別中,所以,將這個特征為0分到類別B中的錯誤率是40%。然后,將所有的特征統(tǒng)計完,計算所有的特征錯誤率,再選擇錯誤率最低的特征作為唯一的分類準則——這就是OneR。
現(xiàn)在用代碼來實現(xiàn)算法。
# OneR算法實現(xiàn) import numpy as np from sklearn.datasets import load_iris # 加載iris數(shù)據(jù)集 dataset = load_iris() # 加載iris數(shù)據(jù)集中的data數(shù)組(數(shù)據(jù)集的特征) X = dataset.data # 加載iris數(shù)據(jù)集中的target數(shù)組(數(shù)據(jù)集的類別) y_true = dataset.target # 計算每一項特征的平均值 attribute_means = X.mean(axis=0) # 與平均值比較,大于等于的為“1”,小于的為“0”.將連續(xù)性的特征值變?yōu)殡x散性的類別型。 x = np.array(X >= attribute_means, dtype="int") from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(x, y_true, random_state=14) from operator import itemgetter from collections import defaultdict # 找到一個特征下的不同值的所屬的類別。 def train_feature_class(x, y_true, feature_index, feature_values): num_class = defaultdict(int) for sample, y in zip(x, y_true): if sample[feature_index] == feature_values: num_class[y] += 1 # 進行排序,找出最多的類別。按從大到小排列 sorted_num_class = sorted(num_class.items(), key=itemgetter(1), reverse=True) most_frequent_class = sorted_num_class[0][0] error = sum(value_num for class_num , value_num in sorted_num_class if class_num != most_frequent_class) return most_frequent_class, error # print train_feature_class(x_train, y_train, 0, 1) # 接著定義一個以特征為自變量的函數(shù),找出錯誤率最低的最佳的特征,以及該特征下的各特征值所屬的類別。 def train_feature(x, y_true, feature_index): n_sample, n_feature = x.shape assert 0 <= feature_index < n_feature value = set(x[:, feature_index]) predictors = {} errors = [] for current_value in value: most_frequent_class, error = train_feature_class(x, y_true, feature_index, current_value) predictors[current_value] = most_frequent_class errors.append(error) total_error = sum(errors) return predictors, total_error # 找到所有特征下的各特征值的類別,格式就如:{0:({0: 0, 1: 2}, 41)}首先為一個字典,字典的鍵是某個特征,字典的值由一個集合構(gòu)成,這個集合又是由一個字典和一個值組成,字典的鍵是特征值,字典的值為類別,最后一個單獨的值是錯誤率。 all_predictors = {feature: train_feature(x_train, y_train, feature) for feature in xrange(x_train.shape[1])} # print all_predictors # 篩選出每個特征下的錯誤率出來 errors = {feature: error for feature, (mapping, error) in all_predictors.items()} # 對錯誤率排序,得到最優(yōu)的特征和最低的錯誤率,以此為模型和規(guī)則。這就是one Rule(OneR)算法。 best_feature, best_error = sorted(errors.items(), key=itemgetter(1), reverse=False)[0] # print "The best model is based on feature {0} and has error {1:.2f}".format(best_feature, best_error) # print all_predictors[best_feature][0] # 建立模型 model = {"feature": best_feature, "predictor": all_predictors[best_feature][0]} # print model # 開始測試——對最優(yōu)特征下的特征值所屬類別進行分類。 def predict(x_test, model): feature = model["feature"] predictor = model["predictor"] y_predictor = np.array([predictor[int(sample[feature])] for sample in x_test]) return y_predictor y_predictor = predict(x_test, model) # print y_predictor # 在這個最優(yōu)特征下,各特征值的所屬類別與測試數(shù)據(jù)集相對比,得到準確率。 accuracy = np.mean(y_predictor == y_test) * 100 print "The test accuracy is {0:.2f}%".format(accuracy) from sklearn.metrics import classification_report # print(classification_report(y_test, y_predictor))
總結(jié):OneR算法,我在最開始的以為它是找到一個錯誤率最低的特征之后可以判斷所有特征的分類,其實,現(xiàn)在明白它只能判斷這個特征下的各特征值的分類,所以,明顯它會有一些局限性。只是說它比較快捷也比較簡單明了。但是,還是得是情況而判斷是否使用它。
class precision recall f1-score support
0 0.94 1.00 0.97 17
1 0.00 0.00 0.00 13
2 0.40 1.00 0.57 8
avg / total 0.51 0.66 0.55 38
注:
# 在上面代碼中。
for sample in x_test:
print sample[0]
# 得到的是x_test的第一列數(shù)據(jù)。而用下面的代碼得到的是x_test的第一行數(shù)據(jù)。
print x_test[0]
# 注意兩者區(qū)別
以上這篇python算法演練_One Rule 算法(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Python算法的時間復雜度和空間復雜度(實例解析)
- Python算法中的時間復雜度問題
- python算法題 鏈表反轉(zhuǎn)詳解
- python算法與數(shù)據(jù)結(jié)構(gòu)之單鏈表的實現(xiàn)代碼
- python算法與數(shù)據(jù)結(jié)構(gòu)之冒泡排序?qū)嵗斀?/a>
- 詳解python算法之冒泡排序
- Python算法之圖的遍歷
- Python算法輸出1-9數(shù)組形成的結(jié)果為100的所有運算式
- python算法表示概念掃盲教程
- Python算法應用實戰(zhàn)之棧詳解
- Python算法之棧(stack)的實現(xiàn)
- python算法學習之計數(shù)排序?qū)嵗?/a>
- Python集成學習之Blending算法詳解
相關(guān)文章
Jupyter?Notebook出現(xiàn)不是內(nèi)部或外部的命令解決方案
這篇文章主要介紹了Jupyter?Notebook出現(xiàn)不是內(nèi)部或外部的命令解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06十個Python練手的實戰(zhàn)項目,學會這些Python就基本沒問題了(推薦)
這篇文章主要介紹了Python實戰(zhàn)項目,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04使用python+Flask實現(xiàn)日志在web網(wǎng)頁實時更新顯示
日志是一種可以追蹤某些軟件運行時所發(fā)生事件的方法,下面這篇文章主要給大家介紹了關(guān)于使用python+Flask實現(xiàn)日志在web網(wǎng)頁實時更新顯示的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08Python線程池ThreadPoolExecutor使用方式
這篇文章主要介紹了Python線程池ThreadPoolExecutor使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02Python統(tǒng)計列表元素出現(xiàn)次數(shù)的方法示例
這篇文章主要介紹了Python統(tǒng)計列表元素出現(xiàn)次數(shù)的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04Python實現(xiàn)對一個函數(shù)應用多個裝飾器的方法示例
這篇文章主要介紹了Python實現(xiàn)對一個函數(shù)應用多個裝飾器的方法,結(jié)合實例形式分析了Python編程中一個函數(shù)使用多個裝飾器的簡單操作技巧,需要的朋友可以參考下2018-02-02