分类

分类是数据挖掘领域最为常用的方法之一,不论是实际应用还是科研,都少不了它的身影。
根据检测数据确定植物的种类。类别的值为“植物属于哪个种类?
接下来这个代码就是实现这个问题
执行过程是先导入sklearn库中的数据,然后选取其中一部分数据进行训练,另一部分进行测试
这里采用的OneR算法会将所有的数据离散化,即大于平均值的为1,小于平均值的为0,然后计算出预测器和错误率,预测器中的内容是符合对应特征数量最多的种类,错误率是不符合特征的数量

OneR one rule 即采用一条最优的规则作为评判标准

这里规则是使用训练数据错误率最低的一个特征,然后通过该特征的预测器预测出测试值的种类
每种特征有两种取值 每种特征的错误率为两种取值错误率之和(即不符合的数量)
参考书籍:Python数据挖掘入门与实践

具体代码如下

from sklearn.datasets import load_iris
dataset = load_iris()
X = dataset.data   #特征         表示每种植物的四种特征
Y = dataset.target #对象 0 1 2 3 表示四种类型的植物 print X
print Y
#print dataset.DESCR #查看数据说明
[[5.1 3.5 1.4 0.2][4.9 3.  1.4 0.2][4.7 3.2 1.3 0.2][4.6 3.1 1.5 0.2][5.  3.6 1.4 0.2][5.4 3.9 1.7 0.4][4.6 3.4 1.4 0.3][5.  3.4 1.5 0.2][4.4 2.9 1.4 0.2][4.9 3.1 1.5 0.1][5.4 3.7 1.5 0.2][4.8 3.4 1.6 0.2][4.8 3.  1.4 0.1][4.3 3.  1.1 0.1][5.8 4.  1.2 0.2][5.7 4.4 1.5 0.4][5.4 3.9 1.3 0.4][5.1 3.5 1.4 0.3][5.7 3.8 1.7 0.3][5.1 3.8 1.5 0.3][5.4 3.4 1.7 0.2][5.1 3.7 1.5 0.4][4.6 3.6 1.  0.2][5.1 3.3 1.7 0.5][4.8 3.4 1.9 0.2][5.  3.  1.6 0.2][5.  3.4 1.6 0.4][5.2 3.5 1.5 0.2][5.2 3.4 1.4 0.2][4.7 3.2 1.6 0.2][4.8 3.1 1.6 0.2][5.4 3.4 1.5 0.4][5.2 4.1 1.5 0.1][5.5 4.2 1.4 0.2][4.9 3.1 1.5 0.2][5.  3.2 1.2 0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 2 2 2 2 2 2 2 2 2 2 2 22 2]
attribute_means = X.mean(axis=0)
#print X.mean()
print attribute_means #每个特征的平均值
[5.84333333 3.05733333 3.758      1.19933333]
import numpy as np
X_d = np.array(X>=attribute_means, dtype='int') #高于平均值的为1,低于的为0
print X_d
[[0 1 0 0][0 0 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 0 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 0 0 0][0 0 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 0 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 0 0 0][0 1 0 0][0 1 0 0][0 0 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 0 0 0][0 1 0 0][0 1 0 0][0 1 0 0][0 1 0 0][1 1 1 1][1 1 1 1][1 1 1 1][0 0 1 1][1 0 1 1][0 0 1 1][1 1 1 1][0 0 0 0][1 0 1 1][0 0 1 1][0 0 0 0][1 0 1 1][1 0 1 0][1 0 1 1][0 0 0 1][1 1 1 1][0 0 1 1][0 0 1 0][1 0 1 1][0 0 1 0][1 1 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 0 1 1][0 0 0 0][0 0 1 0][0 0 0 0][0 0 1 1][1 0 1 1][0 0 1 1][1 1 1 1][1 1 1 1][1 0 1 1][0 0 1 1][0 0 1 1][0 0 1 1][1 0 1 1][0 0 1 1][0 0 0 0][0 0 1 1][0 0 1 1][0 0 1 1][1 0 1 1][0 0 0 0][0 0 1 1][1 1 1 1][0 0 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 0 1 1][0 0 1 1][1 0 1 1][1 0 1 1][1 1 1 1][1 1 1 1][1 0 1 1][1 0 1 1][0 0 1 1][0 0 1 1][1 1 1 1][1 0 1 1][1 1 1 1][1 0 1 1][1 0 1 1][1 1 1 1][0 0 1 1][1 0 1 1][1 0 1 1][1 1 1 1][1 1 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 1 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 1 1 1][1 1 1 1][1 0 1 1][1 1 1 1][1 1 1 1][1 1 1 1][0 0 1 1][1 1 1 1][1 1 1 1][1 0 1 1][1 0 1 1][1 0 1 1][1 1 1 1][1 0 1 1]]
from collections import defaultdict
from operator import itemgetter
def train_feature_value(X,y_true,feature_index,value):class_counts = defaultdict(int)for sample,y in zip(X,y_true):   #zip函数 将多个列表打包成元组列表 list1=[a,b,c],list2=[1,2,3]  if sample[feature_index] == value: #list3=zip(list1,list2) list3=[(a,1),(b,2),(c,3)]  class_counts[y] += 1     #符合特征的数量sorted_class_counts = sorted(class_counts.items(),key=itemgetter(1),reverse=True)most_frequent_class = sorted_class_counts[0][0]error = sum([class_count for class_value, class_count in class_counts.items()if class_value != most_frequent_class])print errorreturn most_frequent_class,error   #返回符合特征最多的对象(种类) 错误率即不符合该对象特征的数量
def train_on_feature(X, y_true, feature_index):# Check that variable is a valid number#n_samples, n_features = X.shape#assert 0 <= feature_index < n_features# Get all of the unique values that this variable hasvalues = set(X[:,feature_index])#print values# Stores the predictors array that is returnedpredictors = {}     #键表示特征值、值表示类别errors = []for current_value in values:print current_valuemost_frequent_class, error = train_feature_value(X, y_true, feature_index, current_value)predictors[current_value] = most_frequent_classerrors.append(error)# Compute the total error of using this feature to classify ontotal_error = sum(errors)return predictors, total_error #返回每个符合特征的预测期和错误率
def predict(X_test, model):variable = model['variable']predictor = model['predictor']y_predicted = np.array([predictor[int(sample[variable])] for sample in X_test])return y_predicted
from sklearn.model_selection import train_test_split #cross_validation为老版本的模块,现在使用model_selection
X_train, X_test, y_train, y_test = train_test_split(X_d, Y,random_state=14)#random为随机状态,等于none是正真的随机
#将数据集随机切分为两部分,默认把数据集的25%作为测试集 #14为某种随机的情况,每次随机都是这个状况
#print X_train
all_predictiors = {}
errors = {}
for feature_index in range(X_train.shape[1]):#print X_test.shape[1]predictors, total_error = train_on_feature(X_train,y_train,feature_index)all_predictiors[feature_index] = predictors  errors[feature_index] = total_errorprint total_error
print all_predictiors #每个特征的预测器
0
19
1
22
41
0
35
1
23
58
0
6
1
31
37
0
7
1
30
37
{0: {0: 0, 1: 2}, 1: {0: 1, 1: 0}, 2: {0: 0, 1: 2}, 3: {0: 0, 1: 2}}
best_variable, best_error = sorted(errors.items(),key=itemgetter(1))[0]
#找出错误率最低的特征
print best_variable,best_error
print errors
2 37
{0: 41, 1: 58, 2: 37, 3: 37}
model = {'variable':best_variable, 'predictor':all_predictiors[best_variable]}
#使用该特征的预测器 即该特征为0预测的种类,该特征为1预测的种类
print model
{'variable': 2, 'predictor': {0: 0, 1: 2}}
y_predicted = predict(X_test,model) #返回样本特征预测的值
print y_predicted
[0 0 0 2 2 2 0 2 0 2 2 0 2 2 0 2 0 2 2 2 0 0 0 2 0 2 0 2 2 0 0 0 2 0 2 0 22]
accuracy = np.mean(y_predicted == y_test) * 100 #计算准确率 预测正确数/测试总数
print ("The test accuracy is {:.1f}%".format(accuracy))
The test accuracy is 65.8%

仅仅采用一条规则,准确率也达到了66,OneR算法还是可效的

使用OneR算法进行分类(Python实现)相关推荐

  1. OneR算法的Python简单实现

    OneR算法就是,在已有数据中,根据具有相同特征值的个体最可能属于哪个类别进行分类.即取效果最好的那个特征进行分类. #-*- coding=utf-8 -*- # import numpy as n ...

  2. OneR算法_0(python数据挖掘入门与实践-实验2)

    文章目录 结果 代码 结果 代码 from sklearn.datasets import load_iris import numpy as np from collections import d ...

  3. 用OneR算法对Iris植物数据进行分类

    数据集介绍 Iris是植物分类数据集,这个数据集一共有150条植物数据.每条数据都 给出了四个特征:sepal length.sepal width.petal length.petal width( ...

  4. python数据挖掘-oneR算法

    以下参考自<python数据挖掘入门与实战> 1.4分类 已知类别的数据集,经过训练得到一个分类模型,再用模型对类别未知的的数据进行分类.比如垃圾邮件过滤器 离散化:数据集的特征为连续值, ...

  5. Python数据挖掘-OneR算法简介

    OneR算法 内容来源于<Python数据挖掘入门与实践> 在末尾有源代码,但需要使用Jupyter notebook,大家可以去我的另一篇文章找安装教程.http://blog.csdn ...

  6. Python数据挖掘学习——鸢尾花分类、OneR算法

    <Python数据挖掘入门与实践>第一章内容,实现一个简单的分类处理,实现OneR算法. OneR算法的思路很简单,它根据已有的数据中,具有相同特征值的个体最可能属于哪个类别进行分类.On ...

  7. Python使用K-means聚类算法进行分类案例一则

    K-means算法是经典的基于划分的聚类方法,是十大经典数据挖掘算法之一,其基本思想是:以空间中k个点为中心进行聚类,对最靠近它们的对象归类.通过迭代的方法,逐次更新各聚类中心的值,直至得到最好的聚类 ...

  8. python机器学习实现oneR算法 以鸢尾data为例

    oneR即"一条规则".oneR算法根据已有的数据中,具有相同特征值的个体最可能属于哪个类别来进行分类. 以鸢尾data为例,该算法实现过程可解读为以下六步: 文章目录 一. 导包 ...

  9. python数据挖掘(2.分类 OneR算法)

    数据源 分类是数据挖掘最为常用的方法之一,无论实际应用还是调研,都需要它的帮忙.对于分类问题,我们通常能拿到表示实际对象或时间的数据及,而数据集中每一条数据都有所属于的类别,这些类别把一条条的数据划分 ...

最新文章

  1. Zabbix监控网络设备日志文件及字段报警
  2. java been 字段命名的坑
  3. Redis进阶-List底层数据结构精讲
  4. 安装好了java7怎么打开,Windows怎么安装JDK1.7 JDK1.7怎么安装图解
  5. Gradle 编译速度提升 70%!怎么做到的?
  6. 浅析Linux awk命令
  7. 在Elasticsearch中索引Java Bean的简单方法
  8. 36岁,还要不要去互联网公司
  9. fiddler APP抓包设置
  10. 快捷键截屏_win7截图快捷键是什么 win7截图快捷键怎么按
  11. cad的dwg文件转html文件,DWG格式CAD文件如何转为图片上传到网页?
  12. 软件测试文档类型有哪些?
  13. 【压力测试 2】JMeter压力测试之Internal server error 500 问题解决思路
  14. Gsensor的驱动流程 -- 基于IIC
  15. [转] http、https 等 常用默认端口号
  16. markdown 编辑器实现双屏同步滚动
  17. docker-compose安装nginx配置hppts 报错
  18. 行链接(Row Chaining)和行迁移(Row Migration)
  19. Fedora17下的hadoop-1.0.4系统配置
  20. 人民币-美元-日元-港元-欧元汇率数据(1985-2019年)

热门文章

  1. 5G技术即将到来,5G网络的基本特点和应用你了解了多少
  2. 数据结构 --- 图的遍历 DFS、BFS
  3. jquery delay_jQuery delay()函数
  4. 快速重命名文件夹,教你怎样在每个文件夹名中添加符号
  5. 阿里云服务器ECS中扩容云盘后磁盘容量没有增加的解决方法
  6. 凸优化之共轭函数(3)
  7. 诸神之战|福建赛区圆满收官,IP“论剑”引爆现场
  8. Oracle OMF参数
  9. C51单片机播放音乐《起风了》
  10. macbook air 卸载java,macbook air如何卸载软件 macbook air卸载软件的方法