Python机器学习基础教程(一)

我要不要学一下谏山创,创哥,来个至2000年后的你,doge
我觉得吧,这本书算是个入门的,塌下心来,至少做到理解每一行代码啥意思,就好了
欢迎有其他小伙伴共同学习,如有错误,请不吝赐教
个人要求:
	坚持用英语注释
	坚持写博客
							至两个星期后的你

第一章前言

这是第一个例子,老生常谈了,给花分类,我觉得这一步做到熟悉流程就好了,毕竟才第一章
我默认看到我博客的各位,装好了环境,对于专有名词的解释,例如回归,分类等,都会
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split # use to split the dataset as testset and trainningset
from sklearn.neighbors import KNeighborsClassifier
import numpy as np

iris_dataset = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris_dataset['data'], iris_dataset['target'], random_state = 0) # random choice

print("X_train size is : \n{}".format(X_train.shape))
print("X_test size is : \n{}".format(X_test.shape))
print("y_train size is : \n{}".format(y_train.shape))
print("y_test size is : \n{}".format(y_test.shape))

# by using kNeighborsClassifier, we can get the prediction type of the flower
knn = KNeighborsClassifier(n_neighbors = 1) 
# deu to the first learning test, i set the no. of neighbors 1
# actually, the no. always between 3-5
knn.fit(X_train, y_train) # trainning

X_new = np.array([[5, 2.9, 1, 0.2]])
print("X_new.shape : {}".format(X_new.shape))

prediction = knn.predict(X_new)
print("Prediction : {}".format(prediction))
print("predicted target name : {}".format(iris_dataset['target_names'][prediction]))
# the following part is used for evaluate the accuracy of this model

y_pred = knn.predict(X_test)
print("Test set score : {:.2f}".format(np.mean(y_pred == y_test))) # 0.97 means that the accuracy is 97%, nearly credible.

RESULT

输出结果如下

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值