机器学习小白入门——决策树和K-近邻

1、先来一个简单的练手,关于机器学习中,灰狗和拉布拉多的身高特征分类,代码如下:

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> greyhounds=500
>>> labs=500
>>> grey_height=28+4*np.random.randn(greyhounds)
>>> lab_height=24+4*np.random.randn(labs)
>>> plt.hist([grey_height,lab_height],stacked=True,color=['r','b'])
([array([  0.,   3.,  23.,  59., 106., 162.,  88.,  46.,  12.,   1.]), array([  6.,  33., 106., 198., 249., 236., 106.,  53.,  12.,   1.])], array([12.59903609, 15.48870961, 18.37838313, 21.26805665, 24.15773017,
       27.04740369, 29.93707721, 32.82675073, 35.71642425, 38.60609777,
       41.49577129]), <a list of 2 Lists of Patches objects>)
>>> plt.show()
>>> exit()

2、编写一条基础的监督式学习的管道,展示关于不同的分类器如何解决同一个问题,以下代码为决策树和K-近邻的比较。

1、决策树:

>>> from sklearn import datasets
>>> iris=datasets.load_iris()
>>> xx=iris.data
>>> yy=iris.target
>>> from sklearn.cross_validation import train_test_split
E:\Anaconda\lib\site-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)
>>> X_train,X_test,y_train,y_test=train_test_split(xx,yy,test_size=.5)
>>> from sklearn import tree
>>> my_classifier=tree.DecisionTreeClassifier()
>>> my_classifier.fit(X_train,y_train)
DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
            max_features=None, max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, presort=False, random_state=None,
            splitter='best')
>>> predictions=my_classifier.predict(X_test)
>>> print(predictions)
[0 2 2 0 2 2 1 0 2 2 1 0 2 2 2 1 1 0 0 2 0 2 2 0 2 0 2 1 0 1 0 2 0 0 2 1 1
 0 1 1 1 1 2 0 1 2 2 1 1 0 2 2 2 0 1 0 0 2 2 0 1 0 1 2 0 2 0 2 0 2 0 0 0 1
 2]
>>> from sklearn.metrics import accuracy_score
>>> print(accuracy_score(y_test,predictions))
0.9466666666666667

2、K最近邻居分类器
将上述代码段中间关于决策树分类器的代码换掉,即

>>> from sklearn import tree
>>> my_classifier=tree.DecisionTreeClassifier()

替换为以下

>>> from sklearn.neighbors import KNeighborsClassifier
>>> my_classifier=KNeighborsClassifier()

继续按照1中代码运行。

3、两次运行甚至多次运行会得到不同的准确度?

原因有二:
第一,分类器的工作原理是有区别的;
第二,每次训练集和测试集的拆分是随机的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值