我要不要学一下谏山创,创哥,来个至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