python 机器学习基础教程——第一章,引言

https://www.cnblogs.com/HolyShine/p/10819831.html

# from sklearn.datasets import load_iris
import numpy as np #科学计算基础包
from scipy import sparse
import matplotlib.pyplot as plt
import pandas as pd
from IPython.display import display
import sys
import matplotlib
import sklearn
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier # x=np.array([[1,2,3],[4,5,6]])
# print("x:\n{}".format(x))
# eye=np.eye(4)
# print("NumPy array:\n{}".format(eye)) # x=np.linspace(-10,10,100)#在 -10和 10 之间生成一个数列,共100个数
# #用正弦函数创建第二个数组
# y=np.sin(x)
# plt.plot(x,y,marker="x")#no display,why? #pandas
# data={'Name':["John","Anna","Peter","Linda"],
# 'Location':["New York","Paris","Berlin","London"],
# 'Age':[24,13,53,33]
# }
# data_pandas = pd.DataFrame(data)
# display(data_pandas)
#
# display(data_pandas[data_pandas.Age>30]) # print('Python Version:{}'.format(sys.version))
# print('Pandas Version:{}'.format(pd.__version__))
# print('matplotlib Version:{}'.format(matplotlib.__version__))
# print('matplotlib Version:{}'.format(matplotlib.__version__))
# print('scikit-learn Version:{}'.format(sklearn.__version__)) iris_dataset=load_iris()
# print("Keys of iris_dataset:\n{}".format(iris_dataset.keys())) X_train,X_test, y_train, y_test=train_test_split(
iris_dataset['data'], iris_dataset['target'], random_state=0
)
# print("X_train sharpe:{}".format(X_train.shape))
# print("y_train shape:{}".format(y_train.shape))
#
#
# iris_dtaframe=pd.DataFrame(X_train, columns=iris_dataset.feature_names)
# grr=pd.scatter_matrix(iris_dtaframe, c=y_train, figsize=(15,15), marker='O',hist_kwds={'bins':20}, s=60, alpha=.8, cmap=mglearn.cm3) #1.7.4 构建第一个模型:K邻近算法
knn=KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)
#out
KNeighborsClassifier(algorithm='auto',leaf_size=30,metric='minkowski',metric_params=None, n_jobs=1, n_neighbors=1, p=2, weights='uniform') 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])) y_pred=knn.predict(X_test)
print("Test set predictions:\n{}".format(y_pred))
print("Test set score:{:.2f}".format(np.mean(y_pred == y_test)))
print("Test set score:{:.2f}".format(knn.score(X_test, y_test)))
上一篇:村田噪声抑制基础教程-第一章 需要EMI静噪滤波器的原因


下一篇:阅读SQL基础教程