scikit-learn实现近邻算法分类的示例

scikit-learn实现近邻算法分类的示例

scikit-learn库

scikit-learn已经封装好很多数据挖掘的算法

现介绍数据挖掘框架的搭建方法

  • 转换器(Transformer)用于数据预处理,数据转换
  • 流水线(Pipeline)组合数据挖掘流程,方便再次使用(封装)
  • 估计器(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]) 
www.zeeklog.com - scikit-learn实现近邻算法分类的示例
for parameter, scores in zip(parameter_values, all_scores): n_scores = len(scores) plt.plot([parameter] * n_scores, scores, '-o') 
www.zeeklog.com - scikit-learn实现近邻算法分类的示例
plt.plot(parameter_values, all_scores, 'bx') 
www.zeeklog.com - scikit-learn实现近邻算法分类的示例
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') 
www.zeeklog.com - scikit-learn实现近邻算法分类的示例
plt.plot(parameter_values, avg_scores, '-o') 
www.zeeklog.com - scikit-learn实现近邻算法分类的示例

Read more

最新电子电气架构(EEA)调研-3

而新一代的强实时性、高确定性,以及满足CAP定理的同步分布式协同技术(SDCT),可以实现替代TSN、DDS的应用,且此技术已经在无人车辆得到验证,同时其低成本学习曲线、无复杂二次开发工作,将开发人员的劳动强度、学习曲线极大降低,使开发人员更多的去完成算法、执行器功能完善。 五、各大车厂的EEA 我们调研策略是从公开信息中获得各大车厂的EEA信息,并在如下中进行展示。 我们集中了华为、特斯拉、大众、蔚来、小鹏、理想、东风(岚图)等有代表领先性的车辆电子电气架构厂商。        1、华为 图12 华为的CCA电子电气架构              (1)华为“计算+通信”CC架构的三个平台                         1)MDC智能驾驶平台;                         2)CDC智能座舱平台                         3)VDC整车控制平台。        联接指的是华为智能网联解决方案,解决车内、车外网络高速连接问题,云服务则是基于云计算提供的服务,如在线车主服务、娱乐和OTA等。 华

By Ne0inhk
Apache IoTDB 架构特性与 Prometheus+Grafana 监控体系部署实践

Apache IoTDB 架构特性与 Prometheus+Grafana 监控体系部署实践

Apache IoTDB 架构特性与 Prometheus+Grafana 监控体系部署实践 文章目录 * Apache IoTDB 架构特性与 Prometheus+Grafana 监控体系部署实践 * Apache IoTDB 核心特性与价值 * Apache IoTDB 监控面板完整部署方案 * 安装步骤 * 步骤一:IoTDB开启监控指标采集 * 步骤二:安装、配置Prometheus * 步骤三:安装grafana并配置数据源 * 步骤四:导入IoTDB Grafana看板 * TimechoDB(基于 Apache IoTDB)增强特性 * 总结与应用场景建议 Apache IoTDB 核心特性与价值 Apache IoTDB 专为物联网场景打造的高性能轻量级时序数据库,以 “设备 - 测点” 原生数据模型贴合物理设备与传感器关系,通过高压缩算法、百万级并发写入能力和毫秒级查询响应优化海量时序数据存储成本与处理效率,同时支持边缘轻量部署、

By Ne0inhk
SQL Server 2019安装教程(超详细图文)

SQL Server 2019安装教程(超详细图文)

SQL Server 介绍) SQL Server 是由 微软(Microsoft) 开发的一款 关系型数据库管理系统(RDBMS),支持结构化查询语言(SQL)进行数据存储、管理和分析。自1989年首次发布以来,SQL Server 已成为企业级数据管理的核心解决方案,广泛应用于金融、电商、ERP、CRM 等业务系统。它提供高可用性、安全性、事务处理(ACID)和商业智能(BI)支持,并支持 Windows 和 Linux 跨平台部署。 一、获取 SQL Server 2019 安装包 1. 官方下载方式 前往微软官网注册账号后,即可下载 SQL Server Developer 版本(

By Ne0inhk