《Python数据挖掘入门与实践》Robert Layton 人民邮电出版社

The OneR algorithm is:

  • For each variable

    • For each value of the variable

      • The prediction based on this variable goes the most frequent class
      • Compute the error of this prediction
    • Sum the prediction errors for all values of the variable
  • Use the variable with the lowest error
import numpy as np #numpy提供矩阵运算功能
# Load our dataset
from sklearn.datasets import load_iris #scikit-learn库内置了Iris植物分类数据集
#X, y = np.loadtxt("X_classification.txt"), np.loadtxt("y_classification.txt")
dataset = load_iris() #读入python自带的iris数据集
X = dataset.data #字典dataset中data键下的数据 每条数据含植物的四种特征属性
y = dataset.target #字典dataset中target键下的数据 0、1、2分别代表三种植物
print(dataset.DESCR) #字典dataset中DESCR键下的内容
n_samples, n_features = X.shape #shape获取X行列数  

离散化:高于该属性均值为1,低于均值为0

# Compute the mean for each attribute
attribute_means = X.mean(axis=0) #mean():函数求取均值;axis =0:对各列求均值,返回 1*n 矩阵
assert attribute_means.shape == (n_features,) #assert断言是声明其布尔值必须为真的判定,如果发生异常就说明表达示为假。用来测试表示式,其返回值为假,就会触发异常。
X_d = np.array(X >= attribute_means, dtype='int') <span style="font-family: Arial, Helvetica, sans-serif;">#NumPy的数组类被称作ndarray,通常被称作数组。array()创建数组,dtype设置数据类型,此处将X与均值比较大小后的布尔值转换为int
# 将离散后的数据集X_d分为训练集和测试集
from sklearn.cross_validation import train_test_split #train_test_split随机划分训练集和测试集  # Set the random state to the same number to get the same results as in the book
random_state = 14 #随机数种子:其实就是该组随机数的编号,在需要重复试验的时候,保证得到一组一样的随机数。  X_train, X_test, y_train, y_test = train_test_split(X_d, y, random_state=random_state)
print("There are {} training samples".format(y_train.shape))
print("There are {} testing samples".format(y_test.shape))  
from collections import defaultdict
from operator import itemgetterdef train(X, y_true, feature): #定义train函数,参数为数据集、类别、当前特征变量对应的索引值,计算给定特征变量时对应的预测结果和错误率,如给定以花瓣长度为判断依据,返回当花瓣长度大于平均值和小于平均值时,对应的预测类别,以及总错误率"""Computes the predictors and error for a given feature using the OneR algorithmParameters----------X: array [n_samples, n_features]The two dimensional array that holds the dataset. Each row is a sample, each columnis a feature.y_true: array [n_samples,]The one dimensional array that holds the class values. Corresponds to X, such thaty_true[i] is the class value for sample X[i].feature: intAn integer corresponding to the index of the variable we wish to test.0 <= variable < n_features  #本例中有0、1、2、3四个特征变量索引值Returns-------predictors: dictionary of tuples: (value, prediction)For each item in the array, if the variable has a given value, make the given prediction.error: floatThe ratio of training data that this rule incorrectly predicts."""# Check that variable is a valid numbern_samples, n_features = X.shape #获取参数X的行列数assert 0 <= feature < n_features #验证参数feature满足条件# Get all of the unique values that this variable hasvalues = set(X[:,feature])  #set()创建集合,values赋值为所有samples对应的该feature的值# Stores the predictors array that is returnedpredictors = dict() #预测出的类别errors = [] #错误率for current_value in values:  #遍历当前feature的每个值,本例中共0、1两个取值most_frequent_class, error = train_feature_value(X, y_true, feature, current_value)predictors[current_value] = most_frequent_class #当feature的取值为current_value时,预测类别为most_frequent_classerrors.append(error) #append() 方法用于在列表末尾添加新的对象# Compute the total error of using this feature to classify ontotal_error = sum(errors)  #把当前feature每个取值(此处为0和1)的错误率求和,作为该feature的错误率return predictors, total_error #({特征值1:类别1,特征值2:类别2},错误率)# Compute what our predictors say each sample is based on its value
#y_predicted = np.array([predictors[sample[feature]] for sample in X])def train_feature_value(X, y_true, feature, value): #定义函数,参数数据集、类别(0、1、2)、当前特征的索引值(0、1、2、3)、特征取值(0、1),计算给定特征值情况下的预测结果和错误率,如给定花瓣大于平均值,返回最可能的类别和错误率# Create a simple dictionary to count how frequency they give certain predictionsclass_counts = defaultdict(int)  #当给定特征值时,分别统计有几个个体属于类别0、类别1和类别2# Iterate through each sample and count the frequency of each class/value pairfor sample, y in zip(X, y_true): #遍历数据集中的每个个体,zip的应用:将一系列对象中对应的元素打包成一个tuple(元组),返回由这些tuples组成的列表if sample[feature] == value: #如果当前个体的当前特征取值为当前指定特征值,如:当前个体的花瓣长度大于平均值class_counts[y] += 1 #当前个体对应类别的统计次数+1,class_counts {类别1:数量1,类别2:数量2,类别3:数量3}# Now get the best one by sorting (highest first) and choosing the first itemsorted_class_counts = sorted(class_counts.items(), key=itemgetter(1), reverse=True) #排序,返回一个新列表[(类别1,数量1),(类别2,数量2),...]most_frequent_class = sorted_class_counts[0][0] #取出列表sorted_class_counts中第一个元祖中的第一个值,即类别1# The error is the number of samples that do not classify as the most frequent class# *and* have the feature value.n_samples = X.shape[1] #样本个体总数error = sum([class_count for class_value, class_count in class_counts.items()if class_value != most_frequent_class]) #sum的参数可为iterable,注意列表推导式(list comprehension)的应用return most_frequent_class, error #返回(类别1,总错误率)

# Compute all of the predictors
all_predictors = {variable: train(X_train, y_train, variable) for variable in range(X_train.shape[1])}
#all_predictors类别为字典{特征索引1:({特征值1:类别1,特征值2:类别2},错误率),...};注意字典推导的应用;此处的variable代表feature特征变量的索引值;shape[1]返回矩阵X_train的第二维长度,即特征变量的个数:4种特征。
errors = {variable: error for variable, (mapping, error) in all_predictors.items()}#获得{特征值索引1:错误率1,...}
# Now choose the best and save that as "model"
# Sort by error
best_variable, best_error = sorted(errors.items(), key=itemgetter(1))[0] #获得(特征值索引,错误率)
print("The best model is based on variable {0} and has error {1:.2f}".format(best_variable, best_error))
# Choose the bset model
model = {'variable': best_variable,'predictor': all_predictors[best_variable][0]} #获得{'variable':特征值索引,'predictor':({特征值1:类别1,特征值2:类别2}}
print(model)

#应用训练好的model对测试集进行预测

def predict(X_test, model): #定义函数
variable = model['variable']
predictor = model['predictor']
y_predicted = np.array([predictor[int(sample[variable])] for sample in X_test]) #int(sample[variable])取出sample的特征索引对应的变量值并转化为int,np.array生产数组,例如A=np.array([a for a in range(5)])
return y_predicted #获得array([类别0, 类别1,...])

y_predicted = predict(X_test, model) #预测
print(y_predicted)

accuracy = np.mean(y_predicted == y_test) * 100 #准确率
print("The test accuracy is {:.1f}%".format(accuracy))

from sklearn.metrics import classification_report #classification_report构建一个显示主要分类指标的文本报告

print(classification_report(y_test, y_predicted))



												

Python-分类问题示例-OneR-学习笔记相关推荐

  1. Python基础教程-菜鸟教程学习笔记1

    Python基础教程-菜鸟教程学习笔记1 文章目录 Python基础教程-菜鸟教程学习笔记1 前言 Python 简介 1. 第一个Python程序 2. Python 中文编码 3. 基本语法 1) ...

  2. Python预测 数据分析与算法 学习笔记(特征工程、时间序列)

    微信公众号:数学建模与人工智能 GitHub - QInzhengk/Math-Model-and-Machine-Learning 第3章 探索规律 3.1 相关分析 相关关系是一种与函数关系相区别 ...

  3. Python可视化物理随机过程---pygame学习笔记2

    Python可视化物理随机过程-pygame学习笔记2 文章目录 Python可视化物理随机过程---pygame学习笔记2 一.扩散现象的简单的介绍 二.代码实现 三.运行代码的效果展示 四.总结 ...

  4. python查看方法作用_python学习笔记1,新手小白也能看得懂

    这是酸菜在风变编程上学习python时积累的学习笔记,希望能帮到同样也在学习中的小伙伴.持续更新~ 第0关 Print()函数 (1)不带引号:让计算机读懂括号里的内容,打印最终的结果 例:print ...

  5. python __name__怎么使用_python学习笔记26(python中__name__的使用)

    在python中,每个py文件都是一个模块,也都是一个可执行文件,即包含main方法.因此,对每个py文件,可以单独运行,也可以import它给其他客户使用,这两种情况不一样. 1. 如果模块是被导入 ...

  6. python的continue用法_Python学习笔记之Break和Continue用法分析

    本文实例讲述了Python学习笔记之Break和Continue用法.分享给大家供大家参考,具体如下: Python 中的Break 和 Continue break:控制何时循环应该结束 conti ...

  7. python中continue用法_Python学习笔记之Break和Continue用法分析

    本文实例讲述了Python学习笔记之Break和Continue用法.分享给大家供大家参考,具体如下: Python 中的Break 和 Continue break:控制何时循环应该结束 conti ...

  8. python运维开发招聘_GitHub - PlutoaCharon/LiunxNotes: 校招-运维开发(Liunx,Python,Golang)面试学习笔记...

    校招-运维开发(Liunx,Python,Golang)面试学习笔记 1. 网络基础类 2. Linux系统管理类 3. Linux服务管理类 4. 数据库管理 ​ 索引(包括分类及优化方式,失效条件 ...

  9. python 网络接口 开发_Python自动化学习笔记(八)——接口开发、发送网络请求、发送邮件、写日志...

    1.接口开发(flask模块) Python自动化学习笔记(七)接口开发部分的内容补充 1.1参数为json格式: flask.request.is_json #判断参数是否是json格式 flask ...

  10. python 面向对象(类)--学习笔记

    面向对象是一种编程方式, 主要集中在类和对象的两个概念 python 中的类符合封装, 继承, 多态的特征 类 是一个模板, 是n多函数的集成 对象 是类的实例化 类的成员分为三大类:字段.方法.属性 ...

最新文章

  1. oracle 和sybase比较,oracle和sybase的一些区别
  2. Nginx与PHP(FastCGI)的安装、配置
  3. HD 1525 Euclid's Game
  4. Yacc 与 Lex 快速入门
  5. matlab如何把选中区域标亮
  6. 18、数据的备份和还原
  7. SpringBoot2.1.9 分布式锁ShedLock
  8. PyTorch 靠谱的模型可视化教程
  9. js 运行中断停止_如何终止JS继续运行??
  10. matlab语句运算相关论文,毕业论文MATLAB在复变函数中的应用.doc
  11. 最新地形测量全套实习任务指导
  12. 遥感水文前景_遥感水文
  13. Win10画图实用小功能------反色
  14. linux7如何改ssid,ssid怎么设置,教您网络ssid怎么设置
  15. {转]太经典了,我不得不收藏
  16. kotlin发音!2021年Android面试心得,安卓系列学习进阶视频
  17. 机械革命笔记本开关键盘亮度
  18. 弘扬岭南画派爱国精神,广州市海珠区文博管理中心等联袂举办爱国名画进校园
  19. 数字图像处理(第二章)
  20. 电子商务B2C网站购物车设计

热门文章

  1. 音频处理—SOX音效
  2. gnuplot绘图程序中对线型(linetype)、点型(pointtype)、线条宽度(linewidth)、点大小(pointsize)、图样
  3. 【图片上传与图片显示】 SpringBoot ajax (跨域问题)
  4. win10升级到win11教程
  5. [转]NSIS常用代码整理
  6. CCF的基于UEBA的用户上网异常行为分析baseline(线上0.9263)
  7. OpenCV参考手册之Mat类详解(三)
  8. 图片url显示服务器,服务器上图片的url地址
  9. 荣耀手机怎么使用鸿蒙系统,荣耀供应链6月全面恢复,安卓系统依然是第一选择,鸿蒙只是备选...
  10. 深入浅出理解 MyBatis-Plus(最新版3.5.2)