python Machine Learning Basics Tutorial - Chapter One Introduction

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




#np.array = X ([[l, 2,3], [4,5,6]]) 
# Print ( "X: \ n-{}." the format (X)) 
# Eye np.eye = (. 4) 
# Print ( "Array NumPy: \ n-{}." the format (Eye)) 


# X = np.linspace (-10,10,100) between 10 and # 10 generates a column number, a total number of 100 
# # sinusoidal function to create a second array 
# Y = np.sin (X) 
# plt.plot (X, Y, marker = "X") # NO the display, Why? 

# PANDAS 
# Data = { 'the Name': [ "John" , "Anna", "Peter", "Linda"], 
#        'the Location': [ "New York", "Paris", "Berlin", "London"], 
#        'Age': [24,13,53, 33 is] 
#        } 
# 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)))

 

Guess you like

Origin www.cnblogs.com/quietwalk/p/10990743.html