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)))

Original: https://www.cnblogs.com/quietwalk/p/10990743.html
Author: quietwalk
Title: python 机器学习基础教程——第一章,引言

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/9597/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部
最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总