scikit-learn学习笔记

参考资料:

python机器学习库scikit-learn简明教程之:随机森林 

http://nbviewer.jupyter.org/github/donnemartin/data-science-ipython-notebooks/blob/master/kaggle/titanic.ipynb

Python中的支持向量机SVM的使用(有实例) 

基于SIFT特征和SVM的图像分类

scikit-learn sklearn 0.18 官方文档中文版  

只需十四步:从零开始掌握 Python 机器学习(附资源) 

https://github.com/jakevdp/sklearn_pycon2015 

官网:http://scikit-learn.org/stable/

Scikit-learn (sklearn) 优雅地学会机器学习 (莫烦 Python 教程) 

python机器学习库scikit-learn简明教程之:AdaBoost算法 

http://www.docin.com/p-1775095945.html

https://www.bilibili.com/video/av22530538/?p=6 

三维点云目标提取总结 

https://github.com/Fdevmsy/Image_Classification_with_5_methods

https://github.com/huangchuchuan/SVM-HOG-images-classifier

https://blog.csdn.net/always2015/article/details/47100713

DBScan https://www.cnblogs.com/pinard/p/6208966.html

1.KNN的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
carto@cartoPC:~$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> from sklearn import datasets
>>> from sklearn.cross_validation import train_test_split
/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
>>> from sklearn.neighbors import KNeighborsClassifier
>>> iris=datasets.load_iris()
>>> iris_X=iris.data
>>> iris_y=iris.target
>>> print(iris_X[:2,:])
[[ 5.1  3.5  1.4  0.2]
 [ 4.9  3.   1.4  0.2]]
>>> print(iris_y)
[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]
>>> X_train,X_test,y_train,y_test=train_test_split(iris_X,iris_y,test_size=0.3)
>>> print(y_train)
[2 1 0 0 0 2 0 0 1 1 2 2 1 1 2 2 2 0 1 0 2 2 1 1 1 1 1 0 1 1 0 2 1 0 0 2 2
 0 0 2 1 0 0 2 1 2 1 2 1 1 1 2 1 2 0 2 0 1 1 2 1 0 1 2 2 0 2 2 1 0 1 1 2 2
 1 0 1 1 2 0 0 1 0 1 0 2 0 1 1 0 2 1 2 0 2 0 2 0 2 1 0 2 0 2 2]
>>> knn=KNeighborsClassifier()
>>> knn.fit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fit() takes exactly 3 arguments (1 given)
>>> knn.fit(X_train,y_train)
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
           metric_params=None, n_jobs=1, n_neighbors=5, p=2,
           weights='uniform')
>>> print(knn.predict(X_test))
[1 1 2 0 1 1 1 1 2 0 0 2 0 1 0 0 0 1 2 2 2 2 0 1 2 0 1 2 2 0 1 2 0 0 1 0 0
 0 0 1 0 1 1 2 0]
>>> print(y_test)
[1 1 2 0 1 1 1 1 2 0 0 2 0 1 0 0 0 1 2 2 2 2 0 1 2 0 1 2 2 0 2 2 0 0 2 0 0
 0 0 1 0 1 1 2 0]
>>>

 2.SVC的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn import svm
from sklearn.model_selection import train_test_split
def load_data():
    iris=datasets.load_iris()
    X_train,X_test,y_train,y_test=train_test_split(
    iris.data,iris.target,test_size=0.10,random_state=0)
    return X_train,X_test,y_train,y_test
 
def test_LinearSVC(X_train,X_test,y_train,y_test):
    cls=svm.LinearSVC()
    cls.fit(X_train,y_train)
    print('Coefficients:%s, intercept %s'%(cls.coef_,cls.intercept_))
    print('Score: %.2f' %cls.score(X_test,y_test))
 
if __name__=="__main__":
    X_train,X_test,y_train,y_test=load_data()
    test_LinearSVC(X_train,X_test,y_train,y_test)

 调用

1
2
3
4
5
carto@cartoPC:~/python_ws$ python svmtest2.py
Coefficients:[[ 0.18424504  0.45123335 -0.80794237 -0.45071267]
 [-0.13381099 -0.75235247  0.57223898 -1.11494325]
 [-0.7943601  -0.95801711  1.31465593  1.8169808 ]], intercept [ 0.10956304  1.86593164 -1.72576407]
Score: 1.00

 

posted @   太一吾鱼水  阅读(543)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
历史上的今天:
2014-08-15 MFC和Direct3D9一起使用
点击右上角即可分享
微信分享提示