https://www.cnblogs.com/HolyShine/p/10819831.html

# from sklearn.datasets import load_iris
import numpy as np #科学计算基础包
from scipy import sparse
import matplotlib.pyplot as plt
import pandas as pd
from IPython.display import display
import sys
import matplotlib
import sklearn
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier # x=np.array([[1,2,3],[4,5,6]])
# print("x:\n{}".format(x))
# eye=np.eye(4)
# print("NumPy array:\n{}".format(eye)) # x=np.linspace(-10,10,100)#在 -10和 10 之间生成一个数列,共100个数
# #用正弦函数创建第二个数组
# y=np.sin(x)
# plt.plot(x,y,marker="x")#no display,why? #pandas
# data={'Name':["John","Anna","Peter","Linda"],
# 'Location':["New York","Paris","Berlin","London"],
# 'Age':[24,13,53,33]
# }
# data_pandas = pd.DataFrame(data)
# display(data_pandas)
#
# display(data_pandas[data_pandas.Age>30]) # print('Python Version:{}'.format(sys.version))
# print('Pandas Version:{}'.format(pd.__version__))
# print('matplotlib Version:{}'.format(matplotlib.__version__))
# print('matplotlib Version:{}'.format(matplotlib.__version__))
# print('scikit-learn Version:{}'.format(sklearn.__version__)) iris_dataset=load_iris()
# print("Keys of iris_dataset:\n{}".format(iris_dataset.keys())) X_train,X_test, y_train, y_test=train_test_split(
iris_dataset['data'], iris_dataset['target'], random_state=0
)
# print("X_train sharpe:{}".format(X_train.shape))
# print("y_train shape:{}".format(y_train.shape))
#
#
# iris_dtaframe=pd.DataFrame(X_train, columns=iris_dataset.feature_names)
# grr=pd.scatter_matrix(iris_dtaframe, c=y_train, figsize=(15,15), marker='O',hist_kwds={'bins':20}, s=60, alpha=.8, cmap=mglearn.cm3) #1.7.4 构建第一个模型:K邻近算法
knn=KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)
#out
KNeighborsClassifier(algorithm='auto',leaf_size=30,metric='minkowski',metric_params=None, n_jobs=1, n_neighbors=1, p=2, weights='uniform') X_new=np.array([[5,2.9,1,0.2]])
print("X_new.shape:{}".format(X_new.shape))
prediction=knn.predict(X_new)
print("Prediction:{}".format(prediction))
print("Predicted target name:{}".format(iris_dataset['target_names'][prediction])) y_pred=knn.predict(X_test)
print("Test set predictions:\n{}".format(y_pred))
print("Test set score:{:.2f}".format(np.mean(y_pred == y_test)))
print("Test set score:{:.2f}".format(knn.score(X_test, y_test)))

python 机器学习基础教程——第一章,引言的更多相关文章

  1. Python机器学习基础教程-第2章-监督学习之决策树

    前言 本系列教程基本就是摘抄<Python机器学习基础教程>中的例子内容. 为了便于跟踪和学习,本系列教程在Github上提供了jupyter notebook 版本: Github仓库: ...

  2. Python机器学习基础教程-第2章-监督学习之线性模型

    前言 本系列教程基本就是摘抄<Python机器学习基础教程>中的例子内容. 为了便于跟踪和学习,本系列教程在Github上提供了jupyter notebook 版本: Github仓库: ...

  3. Python机器学习基础教程-第1章-鸢尾花的例子KNN

    前言 本系列教程基本就是摘抄<Python机器学习基础教程>中的例子内容. 为了便于跟踪和学习,本系列教程在Github上提供了jupyter notebook 版本: Github仓库: ...

  4. Python机器学习基础教程-第2章-监督学习之K近邻

    前言 本系列教程基本就是摘抄<Python机器学习基础教程>中的例子内容. 为了便于跟踪和学习,本系列教程在Github上提供了jupyter notebook 版本: Github仓库: ...

  5. Python机器学习基础教程-第2章-监督学习之决策树集成

    前言 本系列教程基本就是摘抄<Python机器学习基础教程>中的例子内容. 为了便于跟踪和学习,本系列教程在Github上提供了jupyter notebook 版本: Github仓库: ...

  6. Python机器学习基础教程

    介绍 本系列教程基本就是搬运<Python机器学习基础教程>里面的实例. Github仓库 使用 jupyternote book 是一个很好的快速构建代码的选择,本系列教程都能在我的Gi ...

  7. 村田噪声抑制基础教程-第一章 需要EMI静噪滤波器的原因

    1-1. 简介 EMI静噪滤波器 (EMIFIL®) 是为电子设备提供电磁噪声抑制的电子元件,配合屏蔽罩和其他保护装置一起使用.这种滤波器仅从通过连线传导的电流中提取并移除引起电磁噪声的元件.第1章说 ...

  8. Python基础教程-第一章-变量、函数、字符串

    1.1变量 变量基本上就是代表(或者引用)某个值的名字,举例来说,如果希望用x代表3,只需要执行下面的语句即可: >>>x = 3 这样的操作称为赋值(assignment),值3赋 ...

  9. 画出决策边界线--plot_2d_separator.py源代码【来自python机器学习基础教程】

    import numpy as np import matplotlib.pyplot as plt from .plot_helpers import cm2, cm3, discrete_scat ...

随机推荐

  1. plsql查询数据显示为乱码解决方案

    1.首先安装plsql之后连接数据库,发现使用sql查询出来的中文数据是??,即乱码.原因,因为数据库的编码与本地的编码不一致,plsql默认加载的是本机win10的编码. 2.解决办法: 参数如下: ...

  2. 6 种微服务 RPC 框架,你知道几个?

    开源 RPC 框架有哪些呢? 一类是跟某种特定语言平台绑定的,另一类是与语言无关即跨语言平台的. 跟语言平台绑定的开源 RPC 框架主要有下面几种. Dubbo:国内最早开源的 RPC 框架,由阿里巴 ...

  3. jQuery 源码分析(十九) DOM遍历模块详解

    jQuery的DOM遍历模块对DOM模型的原生属性parentNode.childNodes.firstChild.lastChild.previousSibling.nextSibling进行了封装 ...

  4. C++ delete 和 delete []的区别

    转载自https://blog.csdn.net/cbNotes/article/details/38900799 1.我们通常从教科书上看到这样的说明:delete 释放new分配的单个对象指针指向 ...

  5. SpringBoot 和Vue前后端分离入门教程(附源码)

    作者:梁小生0101 juejin.im/post/5c622fb5e51d457f9f2c2381 推荐阅读(点击即可跳转阅读) 1. SpringBoot内容聚合 2. 面试题内容聚合 3. 设计 ...

  6. 两种查看EFCore生成Sql语句的方法

    一.利用反射生成查询语句 该方法转载自:https://jhrs.com/2019/28488.html (略有修改) using Microsoft.EntityFrameworkCore.Quer ...

  7. ElasticSearch简介(三)——中文分词

    很多时候,我们需要在ElasticSearch中启用中文分词,本文这里简单的介绍一下方法.首先安装中文分词插件.这里使用的是 ik,也可以考虑其他插件(比如 smartcn). $ ./bin/ela ...

  8. 接口的 COM 组件调用 QueryInterface 因以下错误而失败: 库没有注册。

    这个问题原因是因为安装了高版本的office然后卸载掉,又安装了低版本的office导致的. 博主是 office2016卸载后,安装了office2013. EXCEL报错信息为: 无法将类型为“M ...

  9. java基础第十六篇之多线程

    1:线程的概念 进程(任务):一个正在运行的程序 进程的调度:CPU来决定什么时候该运行哪个进程 (时间片轮流法) 线程在一个应用程序中,同时,有多个不同的执行路径,是进程中的实际运作单位. 好处是提 ...

  10. python 生成 树状结构

    树状结构: 字典里只有一个键值对, key 为根, 值为一个列表, 列表里的某个或多个元素可以再进行分支(分支还是列表) 比如: 邮件的发件人, 收件人, 转发关系树状结构 forwarding_re ...