Python利用scikit-learn实现近邻算法分类的示例详解
作者:吃肉的小馒头 时间:2021-01-09 18:43:44
scikit-learn库
scikit-learn已经封装好很多数据挖掘的算法
现介绍数据挖掘框架的搭建方法
1.转换器(Transformer)用于数据预处理,数据转换
2.流水线(Pipeline)组合数据挖掘流程,方便再次使用(封装)
3.估计器(Estimator)用于分类,聚类,回归分析(各种算法对象)
所有的估计器都有下面2个函数
fit() 训练
用法:estimator.fit(X_train, y_train)
estimator = KNeighborsClassifier() 是scikit-learn算法对象
X_train = dataset.data 是numpy数组
y_train = dataset.target 是numpy数组
predict() 预测
用法:estimator.predict(X_test)
estimator = KNeighborsClassifier() 是scikit-learn算法对象
X_test = dataset.data 是numpy数组
示例
%matplotlib inline
# Ionosphere数据集
# https://archive.ics.uci.edu/ml/machine-learning-databases/ionosphere/
# 下载ionosphere.data和ionosphere.names文件,放在 ./data/Ionosphere/ 目录下
import os
home_folder = os.path.expanduser("~")
print(home_folder) # home目录
# Change this to the location of your dataset
home_folder = "." # 改为当前目录
data_folder = os.path.join(home_folder, "data")
print(data_folder)
data_filename = os.path.join(data_folder, "ionosphere.data")
print(data_filename)
import csv
import numpy as np
# Size taken from the dataset and is known已知数据集形状
X = np.zeros((351, 34), dtype='float')
y = np.zeros((351,), dtype='bool')
with open(data_filename, 'r') as input_file:
reader = csv.reader(input_file)
for i, row in enumerate(reader):
# Get the data, converting each item to a float
data = [float(datum) for datum in row[:-1]]
# Set the appropriate row in our dataset用真实数据覆盖掉初始化的0
X[i] = data
# 1 if the class is 'g', 0 otherwise
y[i] = row[-1] == 'g' # 相当于if row[-1]=='g': y[i]=1 else: y[i]=0
# 数据预处理
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=14)
print("训练集数据有 {} 条".format(X_train.shape[0]))
print("测试集数据有 {} 条".format(X_test.shape[0]))
print("每条数据有 {} 个features".format(X_train.shape[1]))
输出:
训练集数据有 263 条
测试集数据有 88 条
每条数据有 34 个features
# 实例化算法对象->训练->预测->评价
from sklearn.neighbors import KNeighborsClassifier
estimator = KNeighborsClassifier()
estimator.fit(X_train, y_train)
y_predicted = estimator.predict(X_test)
accuracy = np.mean(y_test == y_predicted) * 100
print("准确率 {0:.1f}%".format(accuracy))
# 其他评价方式
from sklearn.cross_validation import cross_val_score
scores = cross_val_score(estimator, X, y, scoring='accuracy')
average_accuracy = np.mean(scores) * 100
print("平均准确率 {0:.1f}%".format(average_accuracy))
avg_scores = []
all_scores = []
parameter_values = list(range(1, 21)) # Including 20
for n_neighbors in parameter_values:
estimator = KNeighborsClassifier(n_neighbors=n_neighbors)
scores = cross_val_score(estimator, X, y, scoring='accuracy')
avg_scores.append(np.mean(scores))
all_scores.append(scores)
输出:
准确率 86.4%
平均准确率 82.3%
from matplotlib import pyplot as plt
plt.figure(figsize=(32,20))
plt.plot(parameter_values, avg_scores, '-o', linewidth=5, markersize=24)
#plt.axis([0, max(parameter_values), 0, 1.0])
for parameter, scores in zip(parameter_values, all_scores):
n_scores = len(scores)
plt.plot([parameter] * n_scores, scores, '-o')
plt.plot(parameter_values, all_scores, 'bx')
from collections import defaultdict
all_scores = defaultdict(list)
parameter_values = list(range(1, 21)) # Including 20
for n_neighbors in parameter_values:
for i in range(100):
estimator = KNeighborsClassifier(n_neighbors=n_neighbors)
scores = cross_val_score(estimator, X, y, scoring='accuracy', cv=10)
all_scores[n_neighbors].append(scores)
for parameter in parameter_values:
scores = all_scores[parameter]
n_scores = len(scores)
plt.plot([parameter] * n_scores, scores, '-o')
plt.plot(parameter_values, avg_scores, '-o')
来源:https://blog.csdn.net/qq_42034590/article/details/129243282
标签:Python,scikit-learn,近邻算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中的数学运算操作符使用进阶
2021-05-24 16:05:46
go流程控制代码详解
2023-10-15 18:14:19
python执行scp命令拷贝文件及文件夹到远程主机的目录方法
2023-07-10 09:12:19
使用带批注的 XDR 架构创建 XML 视图
2008-09-04 14:24:00
ASP中不用模板生成HTML静态页面的方法
2011-03-06 10:49:00
python画双y轴图像的示例代码
2021-12-07 16:07:12
对pandas的dataframe绘图并保存的实现方法
2021-12-21 14:54:50
作符合用户体验的input输入框
2008-01-10 12:21:00
Django rest framework分页接口实现原理解析
2023-08-22 21:26:36
![](https://img.aspxhome.com/file/2023/8/62548_0s.png)
asp自动生成javascript检验函数
2009-01-21 19:40:00
TensorFlow平台下Python实现神经网络
2023-10-11 22:31:31
![](https://img.aspxhome.com/file/2023/1/86411_0s.png)
Flask 使用类组织配置详情
2023-06-15 02:31:30
![](https://img.aspxhome.com/file/2023/6/60716_0s.png)
Google的用户体验设计原则
2009-01-12 18:31:00
立足于传统行业设计
2008-07-26 12:36:00
WorkBench管理操作MySQL
2010-10-14 14:21:00
![](https://img.aspxhome.com/file/UploadPic/201010/14/20101014143458336s.png)
python使用分治法实现求解最大值的方法
2021-07-14 12:06:09
sqlserver数据库主键的生成方式小结(sqlserver,mysql)
2012-08-21 10:25:45
Python实现截图生成符合markdown的链接
2021-07-09 10:18:22
![](https://img.aspxhome.com/file/2023/9/67299_0s.png)
用pywin32实现windows模拟鼠标及键盘动作
2023-07-04 21:26:30
Python技法之如何用re模块实现简易tokenizer
2021-08-04 17:03:52