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):监督学习的任务是学习一个模型,使模型能够对任意一个输入给出一个预测的输出,监督学习是统计学的一个重要分支。

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
#下载iris数据集
iris = datasets.load_iris()
#将数据的data部分和target进行赋值, data包含iris花朵的长宽和茎的长宽
iris_X = iris.data
iris_Y = iris.target
iris_X
Out[9]:
array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
。。。 。。。
[6.7, 3. , 5.2, 2.3],
[6.3, 2.5, 5. , 1.9],
[6.5, 3. , 5.2, 2. ],
[6.2, 3.4, 5.4, 2.3],
[5.9, 3. , 5.1, 1.8]]) #iris_Y是花的种类,共三种类型
iris_Y
Out[10]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) #将数据分为训练集合测试集, 用到sklearn API train_test_split, test_size=0.3代表测试集占总数据集的30%。
X_train, X_test, y_train, y_test = train_test_split(iris_X, iris_Y, test_size=0.3)
y_train
Out[13]:
array([2, 0, 0, 0, 0, 2, 2, 0, 2, 0, 1, 2, 0, 2, 1, 1, 1, 1, 1, 2, 2, 2,
1, 2, 0, 0, 1, 2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 0, 0, 1, 1, 1, 0, 0,
0, 0, 0, 2, 0, 0, 2, 2, 0, 2, 2, 2, 1, 2, 1, 2, 0, 0, 2, 2, 0, 2,
0, 2, 0, 1, 1, 1, 2, 0, 2, 1, 2, 1, 2, 2, 0, 1, 2, 0, 1, 2, 0, 0,
2, 0, 1, 1, 2, 2, 0, 0, 1, 2, 1, 1, 2, 0, 0, 0, 1])
X_train
Out[14]:
array([[6.7, 3.1, 5.6, 2.4],
[5.4, 3.4, 1.7, 0.2],
[5.1, 3.8, 1.9, 0.4],
。。。 。。。
[5.4, 3.9, 1.7, 0.4],
[4.6, 3.4, 1.4, 0.3],
[5.5, 3.5, 1.3, 0.2],
[5.5, 2.6, 4.4, 1.2]]) #建立模型
knn = KNeighborsClassifier()
#训练
knn.fit(X_train, y_train)
Out[16]:
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=None, n_jobs=1, n_neighbors=5, p=2,
weights='uniform')
#预测
knn.predict(X_test)
Out[17]:
array([0, 0, 2, 2, 1, 2, 0, 0, 1, 1, 0, 2, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0,
0, 0, 2, 2, 2, 0, 1, 0, 2, 2, 1, 1, 1, 2, 2, 0, 1, 0, 2, 1, 2, 1,
1])
#对比预测值和测试值
y_test
Out[18]:
array([0, 0, 2, 1, 1, 2, 0, 0, 2, 1, 0, 2, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0,
0, 0, 2, 1, 2, 0, 1, 0, 2, 2, 1, 1, 1, 2, 2, 0, 1, 0, 2, 1, 2, 1,
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. HTML5和CSS3兼容清单

    1.CSS3 2.CSS3选择器 3.HTML5 4.HTML5 From  

  2. SQL Server中数据去重单列数据合并

    sql中我们偶尔会用到对数据进行合并,但其中的某一列数据要进行合并的操作: 如下图,一个用户有多个角色ID,如果我们想要统计一个用户有哪些角色,并且以单列的展现形式,单纯的用DISTINCT去掉肯定是 ...

  3. base64加密小案例

    python终端下: import base64 >>> dict='{"name":"tom"}' >>> dict.en ...

  4. 使用spring配置类代替xml配置文件注册bean类

    spring配置类,即在类上加@Configuration注解,使用这种配置类来注册bean,效果与xml文件是完全一样的,只是创建springIOC容器的方式不同: //通过xml文件创建sprin ...

  5. 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第5节 线程池_1_线程池的概念和原理

    线程的底层原理 集合有很多种,线程池的集合用LinkedList最好

  6. MySQL 编码:utf8 与 utf8mb4,utf8mb4_unicode_ci 与 utf8mb4_general_ci

    参考:mysql字符集小结 utf8mb4 已成为 MySQL 8.0 的默认字符集,在MySQL 8.0.1及更高版本中将 utf8mb4_0900_ai_ci 作为默认排序规则. 新项目只考虑 u ...

  7. maximize_window()最大化浏览器和刷新当前页面refresh()

    from selenium import webdriverdriver = webdriver.Firefox()driver.get("https://www.baidu.com&quo ...

  8. BootStrap前端框架

    BootStrap前端框架 Bootstrap 教程:http://www.runoob.com/bootstrap/bootstrap-tutorial.html BpptStrap操作手册:htt ...

  9. js 获取屏幕宽高

    网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: document.body.offset ...

  10. 知识点C++

    比较2个字符串的大小…… s1=s2,strcmp(s1,s2) == ; s1>s2, strcmp(s1,s2) == ; s1<s2, strcmp(s1,s2) == -; str ...