Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split

加入好友
加入社群
Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split - 儲蓄保險王

K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN):

Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split - 儲蓄保險王

KNN 需要資料的 N 個屬性都是順序尺度,

可以比大小,可以比遠近,這樣才能算距離

Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split - 儲蓄保險王

from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split

Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split - 儲蓄保險王

sklearn

Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split - 儲蓄保險王

predict

predict_proba

Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split - 儲蓄保險王

https://pse.is/3ty6rk

Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split - 儲蓄保險王

部分資料:
Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split - 儲蓄保險王
 
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
folder = “C:\Python\P107\doc”
fname = “student.csv”
import os
fpath = os.path.join(folder,fname)
# fpath = folder + “\\” + fname #同義
df = pd.read_csv(fpath,header=None,skiprows=[0])
df.to_excel(os.path.join(folder,”knsDF.xlsx”))
#df.index.size = 40  #df.columns.size = 5
X = df.drop([4],axis=1).values
#drop([0,4])可以提高score
#第0欄類似index, 非資料
#score仍勝出KNN去掉第0欄
y = df[4].values
Xtrain, Xtest, ytrain, ytest =\
    train_test_split(X,y,test_size=0.25,
                     random_state= 42 ,shuffle =True)
XtestDF = pd.DataFrame(Xtest)    
XtestDF.to_excel(os.path.join(folder,”knsXtest.xlsx”))
print(“Xtrain.shape:”,Xtrain.shape) #(30, 3)
print(“ytrain.shape:”,ytrain.shape) #(30,)
print(“Xtest.shape:”,Xtest.shape) #(10, 3)
print(“ytest.shape:”,ytest.shape) #(10,)
for k in range(4,11):
    kns = KNeighborsClassifier(n_neighbors=k)
    kns.fit(Xtrain,ytrain)
    ypred  = kns.predict(Xtest)
    ypred2 = kns.predict_proba(Xtest)
    print(“Prediction:”,ypred)
    print(“Prediction possibility:\n”,ypred2)
    howgood = kns.score(Xtest,ytest)
    print(“goodness:”,howgood,”in case k=”,k)
      
Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split - 儲蓄保險王
部分輸出結果:
Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split - 儲蓄保險王
 

加入好友
加入社群
Python 機器學習: K-近鄰演算法(K Nearest Neighbor ,簡稱 KNN) ; from sklearn.neighbors import KNeighborsClassifier ; from sklearn.model_selection import train_test_split - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *