dataset

在scikit-learn包自带的数据集,R包也自带数据集iris鸢尾花数据集,做训练集。特征较少。

from sklearn import datasets
# Import necessary modules
from sklearn import datasets
import matplotlib.pyplot as plt # Load the digits dataset: digits
digits = datasets.load_digits() # Print the keys and DESCR of the dataset
print(digits.keys())
# DESCR:对于数据集的描述
print(digits.DESCR) # Print the shape of the images and data keys
print(digits.images.shape)
# shape:输出矩阵或则数组的维度
print(digits.data.shape) # Display digit 1010
plt.imshow(digits.images[1010], cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()

KNN(K-近邻算法)

  • 二分类器

    离哪个近,就归那类,还是距离度量,就是用最近的邻居来代表自己,如果你的邻居是个2b,那在knn里面你也是

有写好的API可以直接用,然后调参。

记得基本knn的貌似误差很大,这里可以用智能算法优化,可以想一下啊,我记得是有paper的,看下思路

KNeighborsClassifier(n_neighbors=5, weights=’uniform’, algorithm=’auto’, leaf_size=30,p=2, metric=’minkowski’, metric_params=None, n_jobs=1, **kwargs)

基本参数含义

jianshu

  • n_neighbors : int, optional (default = 5) Number of neighbors to use by default for kneighbors queries…

    选择最近邻居的数目,选择要适中,否则会出现过拟合或者欠拟合的现象

  • weights : str or callable, optional (default = ‘uniform’)weight function used in prediction. Possible values::

    • ‘uniform’ : uniform weights. All points in each neighborhood are weighted equally.
    • ‘distance’ : weight points by the inverse of their distance. in this case, closer neighbors of a query point will have a greater influence

      than neighbors which are further away.
    • [callable] : a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.
  • algorithm : {‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, optional

  • Algorithm used to compute the nearest neighbors:

    • ‘ball_tree’ will use BallTree
    • ‘kd_tree’ will use KDTree
    • ‘brute’ will use a brute-force search.
    • ‘auto’ will attempt to decide the most appropriate algorithm based on the values passed to fit method.

      auto是用户自定义

      Note: fitting on sparse input will override the setting of this parameter, using brute force.
  • leaf_size : int, optional (default = 30)Leaf size passed to BallTree or KDTree. This can affect the speed of the construction and query, as well as the memory required to store the tree. The optimal value depends on the nature of the problem.

  • p : integer, optional (default = 2)Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

    这个就是选择不同的距离度量方式,不过常见的欧式距离比较多啊

  • metric : string or callable, default ‘minkowski’the distance metric to use for the tree. The default metric is minkowski, and with p=2 is equivalent to the standard Euclidean metric. See the documentation of the DistanceMetric class for a list of available metrics.

  • metric_params : dict, optional (default = None)Additional keyword arguments for the metric function.

  • n_jobs : int or None, optional (default=None)The number of parallel jobs to run for neighbors search. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details. Doesn’t affect fit method.

    衡量一个模型的表现能力

  • 使用预测精度来测量模型的拟合效果

  • 在拟合模型的时候,需要先对样本进行训练集和测试集的分类,一般75%的样本做训练集,剩下的做测试集

# Import necessary modules
# 这几个模块导入的方式我写错了
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split # Create feature and target arrays
X = digits.data
y = digits.target # Split into training and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=42, stratify=y) # Create a k-NN classifier with 7 neighbors: knn
knn = KNeighborsClassifier(n_neighbors=7) # Fit the classifier to the training data
knn.fit(X_train, y_train) # Print the accuracy
print(knn.score(X_test, y_test)) <script.py> output:
0.9833333333333333

Overfitting and underfitting

欠拟合与过拟合

可以参考之前的笔记

https://www.cnblogs.com/gaowenxingxing/p/12234179.html

scikit-learn包学习笔记1的更多相关文章

  1. Query意图分析:记一次完整的机器学习过程(scikit learn library学习笔记)

    所谓学习问题,是指观察由n个样本组成的集合,并根据这些数据来预测未知数据的性质. 学习任务(一个二分类问题): 区分一个普通的互联网检索Query是否具有某个垂直领域的意图.假设现在有一个O2O领域的 ...

  2. R parallel包学习笔记2

    这个部分我在datacamp上面学习笔记,可视化的性能很差,使用的函数也很少. 可以参考一下大佬的博客园个人感觉他们讲的真的很详细 https://cosx.org/2016/09/r-and-par ...

  3. R Tidyverse dplyr包学习笔记2

    Tidyverse 学习笔记 1.gapminder 我理解的gapminder应该是一个内置的数据集 加载之后使用 > # Load the gapminder package > li ...

  4. pandas包学习笔记

    目录 zip Importing & exporting data Plotting with pandas Visual exploratory data analysis 折线图 散点图 ...

  5. java.util.concurrent包学习笔记(一)Executor框架

    类图: 其实从类图我们能发现concurrent包(除去java.util.concurrent.atomic 和 java.util.concurrent.locks)中的内容并没有特别多,大概分为 ...

  6. pyecharts包学习笔记

    目录 pyecharts包简介 特性 or 优点 版本 pyecharts包简介 精巧的图表设计.原作者说,当数据分析遇到数据可视化的时候github,该包就诞生了. 可以批量,直观的输出可视化图标吧 ...

  7. 如何打jar包 学习笔记

    jar包是由.class文件压缩而成.要查看jar包中的内容,使用压缩工具 解压缩即可.也可以做修改,并重新打成jar包.总结一下最近学到的一些打jar包的方法: 一.DOS下使用jar命令 打jar ...

  8. Java中的包学习笔记

    一.总结 1.引入包的概念的原因和包的作用比如有多个人开发一个大型程序,A定义了一个Math.java类,B也定义了一个Math.java类,它们放在不同目录,使用的时候也是用目录来区分,包实际上就是 ...

  9. java Concurrent包学习笔记(一):ExecutorService

    一.介绍 ExecutorService是java.util.concurrent包中的一个线程池实现接口.其有两个实现类: 1)ThreadPoolExecutor:普通线程池通过配置线程池大小,能 ...

随机推荐

  1. Centos 7 x64 系统初始化

    前言 Hi,小伙伴们,系统初始化是运维工作中重要的一环,它能有效的提升工作效率,并且是标准化规范化的前提:它能省去要用时再去下载的麻烦,另外,还可以避免因未初始化引起的一些故障问题,可谓好处多多.系统 ...

  2. 关于JAVA中源码级注解的编写及使用

    一.注解简介: 1.1.什么是"注解": ​ 在我们编写代码时,一定看到过这样的代码: class Student { private String name; @Override ...

  3. 源码详解系列(八) ------ 全面讲解HikariCP的使用和源码

    简介 HikariCP 是用于创建和管理连接,利用"池"的方式复用连接减少资源开销,和其他数据源一样,也具有连接数控制.连接可靠性测试.连接泄露控制.缓存语句等功能,另外,和 dr ...

  4. CVE-2019-0199:Apache Tomcat DDOS

    CVE-2019-0199:Apache Tomcat DDOS 0X00漏洞概述 Apache Tomcat HTTP/2拒绝服务漏洞,该漏洞是由于应用服务允许接收大量的配置流量,并且客户端在没有读 ...

  5. 《C# 爬虫 破境之道》:第二境 爬虫应用 — 第七节:并发控制与策略

    我们在第五节中提到一个问题,任务队列增长速度太快,与之对应的采集.分析.处理速度远远跟不上,造成内存快速增长,带宽占用过高,CPU使用率过高,这样是极度有害系统健康的. 我们在开发采集程序的时候,总是 ...

  6. JVM性能优化系列-(4) 编写高效Java程序

    4. 编写高效Java程序 4.1 面向对象 构造器参数太多怎么办? 正常情况下,如果构造器参数过多,可能会考虑重写多个不同参数的构造函数,如下面的例子所示: public class FoodNor ...

  7. [Effective Java 读书笔记] 第8章 通用程序设计

    本章主要讲了以下几条基本的JAVA编程原则: 1.将局部变量的作用域控制在最小,在使用时才定义 2.for-each优于for循环 有三个例外(1,2点主旨就是,for each只能用于读取,不能用于 ...

  8. 《自拍教程6》打开Windows文件后缀

    如果你用的是Windows操作系统, 请把文件后缀名打开,千万别隐藏后缀名, 后续有各类 .py, .sh, .bat, .exe等不同文件, 需要你时刻关注文件后缀名. 如果是我看到我手下的测试人员 ...

  9. [Windows]远程管理服务WinRM远程管理Windows服务器 Invalid use of command line. Type "winrm -?" for help.

    运行环境 Windows 2012 R2 1. Windows需要打开WinRM服务,Server系统默认打开,默认端口5985 # WinRM服务查看 PS C:\Users\Administrat ...

  10. Python 实现转堆排序算法原理及时间复杂度(多图解释)

    原创文章出自公众号:「码农富哥」,欢迎转载和关注,如转载请注明出处! 堆基本概念 堆排序是一个很重要的排序算法,它是高效率的排序算法,复杂度是O(nlogn),堆排序不仅是面试进场考的重点,而且在很多 ...