Introduction k-nearest neighbors (knn) algorithm is a type of supervised ml algorithm which can be used for both classification as well as regression predictive problems. However



Download 0,73 Mb.
Pdf ko'rish
bet6/17
Sana31.12.2021
Hajmi0,73 Mb.
#222840
1   2   3   4   5   6   7   8   9   ...   17
Bog'liq
machine learning with python tutorial-139-150

KNN as Regressor 

First, start with importing necessary Python packages:

 

import numpy as np   



import pandas as pd  

 

Next, download the iris dataset from its weblink as follows: 



path = "https://archive.ics.uci.edu/ml/machine-learning-

databases/iris/iris.data" 

Next, we need to assign column names to the dataset as follows: 

headernames = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 

'Class'] 

 

Now, we need to read dataset to pandas dataframe as follows: 



data = pd.read_csv(url, names=headernames)  

array = data.values  

X = array[:,:2] 

Y = array[:,2] 

data.shape

 

 



output:(150, 5)

 

Next, import KNeighborsRegressor from sklearn to fit the model:



 

from sklearn.neighbors import KNeighborsRegressor 

knnr = KNeighborsRegressor(n_neighbors=10) 

knnr.fit(X, y) 

At last, we can find the MSE as follows:

 

print ("The MSE is:",format(np.power(y-knnr.predict(X),2).mean())) 



Output

 

The MSE is: 0.12226666666666669 



 


Machine Learning with Python 

        


 

 

 



136 

 


Download 0,73 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   17




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish