scikit-learn

Machine Learning in Python

  • Simple and efficient tools for data mining and data analysis
  • Accessible to everybody, and reusable in various contexts
  • Built on NumPy, SciPy, and matplotlib
  • Open source, commercially usable - BSD license

http://scikit-learn.org/stable/index.html

sklearn中算法有四类,分类,回归,聚类,降维

分类和回归是监督式学习,即每个数据对应一个 label。

聚类 是非监督式学习,即没有 label。

降维,当数据集有很多很多属性的时候,可以通过 降维 算法把属性归纳起来。例如 20 个属性只变成 2 个,注意,这不是挑出 2 个,而是压缩成为 2 个,它们集合了 20 个属性的所有特征,相当于把重要的信息提取的更好,不重要的信息就不要了。

然后看问题属于哪一类问题,是分类还是回归,还是聚类,就选择相应的算法。 当然还要考虑数据的大小,例如 100K 是一个阈值。

可以发现有些方法是既可以作为分类,也可以作为回归,例如 SGD

 监督学习(supervised learning):监督学习的任务是学习一个模型,使模型能够对任意一个输入给出一个预测的输出,监督学习是统计学的一个重要分支。

  1. from sklearn import datasets
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.neighbors import KNeighborsClassifier
  4. #下载iris数据集
  5. iris = datasets.load_iris()
    #将数据的data部分和target进行赋值, data包含iris花朵的长宽和茎的长宽
  6. iris_X = iris.data
  7. iris_Y = iris.target
  8. iris_X
  9. Out[9]:
  10. array([[5.1, 3.5, 1.4, 0.2],
  11. [4.9, 3. , 1.4, 0.2],
  12. [4.7, 3.2, 1.3, 0.2],
  13. 。。。 。。。
  14. [6.7, 3. , 5.2, 2.3],
  15. [6.3, 2.5, 5. , 1.9],
  16. [6.5, 3. , 5.2, 2. ],
  17. [6.2, 3.4, 5.4, 2.3],
  18. [5.9, 3. , 5.1, 1.8]])
  19.  
  20. #iris_Y是花的种类,共三种类型
  21. iris_Y
  22. Out[10]:
  23. array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  26. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  27. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  28. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  29. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
  30.  
  31. #将数据分为训练集合测试集, 用到sklearn API train_test_split, test_size=0.3代表测试集占总数据集的30%。
  32. X_train, X_test, y_train, y_test = train_test_split(iris_X, iris_Y, test_size=0.3)
  33. y_train
  34. Out[13]:
  35. array([2, 0, 0, 0, 0, 2, 2, 0, 2, 0, 1, 2, 0, 2, 1, 1, 1, 1, 1, 2, 2, 2,
  36. 1, 2, 0, 0, 1, 2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 0, 0, 1, 1, 1, 0, 0,
  37. 0, 0, 0, 2, 0, 0, 2, 2, 0, 2, 2, 2, 1, 2, 1, 2, 0, 0, 2, 2, 0, 2,
  38. 0, 2, 0, 1, 1, 1, 2, 0, 2, 1, 2, 1, 2, 2, 0, 1, 2, 0, 1, 2, 0, 0,
  39. 2, 0, 1, 1, 2, 2, 0, 0, 1, 2, 1, 1, 2, 0, 0, 0, 1])
  40. X_train
  41. Out[14]:
  42. array([[6.7, 3.1, 5.6, 2.4],
  43. [5.4, 3.4, 1.7, 0.2],
  44. [5.1, 3.8, 1.9, 0.4],
  45. 。。。 。。。
  46. [5.4, 3.9, 1.7, 0.4],
  47. [4.6, 3.4, 1.4, 0.3],
  48. [5.5, 3.5, 1.3, 0.2],
  49. [5.5, 2.6, 4.4, 1.2]])
  50.  
  51. #建立模型
  52. knn = KNeighborsClassifier()
    #训练
  53. knn.fit(X_train, y_train)
  54. Out[16]:
  55. KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
  56. metric_params=None, n_jobs=1, n_neighbors=5, p=2,
  57. weights='uniform')
    #预测
  58. knn.predict(X_test)
  59. Out[17]:
  60. array([0, 0, 2, 2, 1, 2, 0, 0, 1, 1, 0, 2, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0,
  61. 0, 0, 2, 2, 2, 0, 1, 0, 2, 2, 1, 1, 1, 2, 2, 0, 1, 0, 2, 1, 2, 1,
  62. 1])
    #对比预测值和测试值
  63. y_test
  64. Out[18]:
  65. array([0, 0, 2, 1, 1, 2, 0, 0, 2, 1, 0, 2, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0,
  66. 0, 0, 2, 1, 2, 0, 1, 0, 2, 2, 1, 1, 1, 2, 2, 0, 1, 0, 2, 1, 2, 1,
  67. 1])

AI-sklearn 学习笔记(一)sklearn 一般概念的更多相关文章

  1. .NET Remoting学习笔记(一)概念

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 背景 自接触编程以来,一直听过这个名词Remotin ...

  2. 【转载】.NET Remoting学习笔记(一)概念

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 背景 自接触编程以来,一直听过这个名词Remotin ...

  3. 【学习笔记】sklearn数据集与估计器

    数据集划分 机器学习一般的数据集会划分为两个部分: 训练数据:用于训练,构建模型 测试数据:在模型检验时使用,用于评估模型是否有效 训练数据和测试数据常用的比例一般为:70%: 30%, 80%: 2 ...

  4. sklearn学习笔记1

    Image recognition with Support Vector Machines #our dataset is provided within scikit-learn #let's s ...

  5. sklearn学习笔记之简单线性回归

    简单线性回归 线性回归是数据挖掘中的基础算法之一,从某种意义上来说,在学习函数的时候已经开始接触线性回归了,只不过那时候并没有涉及到误差项.线性回归的思想其实就是解一组方程,得到回归函数,不过在出现误 ...

  6. sklearn学习笔记3

    Explaining Titanic hypothesis with decision trees decision trees are very simple yet powerful superv ...

  7. sklearn学习笔记2

    Text classifcation with Naïve Bayes In this section we will try to classify newsgroup messages using ...

  8. sklearn学习笔记

    用Bagging优化模型的过程:1.对于要使用的弱模型(比如线性分类器.岭回归),通过交叉验证的方式找到弱模型本身的最好超参数:2.然后用这个带着最好超参数的弱模型去构建强模型:3.对强模型也是通过交 ...

  9. sklearn学习笔记(一)——数据预处理 sklearn.preprocessing

    https://blog.csdn.net/zhangyang10d/article/details/53418227 数据预处理 sklearn.preprocessing 标准化 (Standar ...

  10. sklearn学习笔记之岭回归

    岭回归 岭回归是一种专用于共线性数据分析的有偏估计回归方法,实质上是一种改良的最小二乘估计法,通过放弃最小二乘法的无偏性,以损失部分信息.降低精度为代价获得回归系数更为符合实际.更可靠的回归方法,对病 ...

随机推荐

  1. Bugku 杂项 telnet

    letnet 下载zip后发现是一个数据包,放到wireshark中打开 右键追踪tcp流后可以发现flag

  2. Go简易分布式对象存储 合并文件的所有分块为一个文件

    项目 项目地址: https://github.com/Draymonders/cloud 欢迎大家Watch or Star 缘由 由于项目中对大文件进行5MB为一个分块上传(多线程,提升上传效率) ...

  3. 二、angular7的基础知识学习

    <p> hello works </p> <div *ngIf="isShow">我是测试内容</div> <p> &l ...

  4. ORB an efficient alternative to SIFT or SURF

    AbstractFeature matching is at the base of many computer vision problems, such as object recognition ...

  5. 「LibreOJ β Round #2」计算几何瞎暴力

    https://loj.ac/problem/517 题解 首先我们如果没有排序这个骚操作的话,可以直接记一下各个数位的前缀和,然后异或标记给全局打,查询的时候先把区间信息提取出来然后整体异或就好了. ...

  6. java1.8 10大新特性

    http://blog.csdn.net/u013598111/article/details/49720867 一.接口的默认方法 Java 8允许我们给接口添加一个非抽象的方法实现,只需要使用 d ...

  7. leetcode-mid-array-73 set matrix zeros

    mycode 空间复杂度 m+n 思路:用set把为0元素所在行.列记录下来 注意:注释的方法更快 class Solution(object): def setZeroes(self, matrix ...

  8. 类BigInteger

    BigInteger类 可以让超过Integer范围内的数据进行运算 构造方法 public BigIntege(String val); package com.jacky; import java ...

  9. PHP 实现并发-进程控制 PCNTL

    参考 基于PCNTL的PHP并发编程 PCNTL 是 PHP 中的一组进程控制函数,可以用来 fork(创建)进程,传输控制信号等. 在PHP中,进程控制支持默认关闭.编译时通过 --enable-p ...

  10. C++笔记(4)——引用及结构体

    引用 C++中有一个很方便的语法叫做引用,作用就是使得函数能够对传入的参数作出全局有效的改动.用法很简单,就是在传入参数的类型后面加上&就可以指明传入的参数是引用. 例子: #include ...