SoFunction
Updated on 2024-10-30

python implementation of k-nearest neighbor regression with equal and unequal weights

python implementation of k-nearest neighbor regression with equal and unequal weights

Updated January 23, 2019 13:54:13 by UESTC_C2_403
Today, I'd like to share with you a python implementation of K nearest neighbor regression, using equal and unequal weights, with good reference value, I hope it will help you. Together follow the editor over to see it

As shown below:

from  import load_boston
 
boston = load_boston()
 
from sklearn.cross_validation import train_test_split
 
import numpy as np;
 
X = 
y = 
 
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 33, test_size = 0.25)
 
print 'The max target value is: ', ()
print 'The min target value is: ', ()
print 'The average terget value is: ', ()
 
from  import StandardScaler
 
ss_X = StandardScaler()
ss_y = StandardScaler()
 
X_train = ss_X.fit_transform(X_train)
X_test = ss_X.transform(X_test)
y_train = ss_y.fit_transform(y_train)
y_test = ss_y.transform(y_test)
 
from  import KNeighborsRegressor
 
uni_knr = KNeighborsRegressor(weights = 'uniform')
uni_knr.fit(X_train, y_train)
uni_knr_y_predict = uni_knr.predict(X_test)
 
dis_knr = KNeighborsRegressor(weights = 'distance')
dis_knr.fit(X_train, y_train)
dis_knr_y_predict = dis_knr.predict(X_test)
 
from  import r2_score, mean_squared_error, mean_absolute_error
 
print 'R-squared value of uniform weights KNeighorRegressor is: ', uni_knr.score(X_test, y_test)
print 'The mean squared error of uniform weights KNeighorRegressor is: ', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(uni_knr_y_predict))
print 'The mean absolute error of uniform weights KNeighorRegressor is: ', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(uni_knr_y_predict))
 
print 'R-squared of distance weights KNeighorRegressor is: ', dis_knr.score(X_test, y_test)
print 'the value of mean squared error of distance weights KNeighorRegressor is: ', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(dis_knr_y_predict))
print 'the value of mean ssbsolute error of distance weights KNeighorRegressor is: ', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(dis_knr_y_predict))

The above this python implementation of K nearest neighbor regression, using equal and unequal weights is all I have to share with you, I hope to give you a reference, and I hope you support me more.

  • python
  • K-nearest neighbor
  • weights

Related articles

  • The most detailed Python formatting output usage explanation (recommended)

    This article introduces the most detailed Python formatting output usage, this article gives you a very detailed introduction to your study or work has a certain reference value, the need for friends can refer to the following
    2021-01-01
  • Example of data sharing for python multiple module py files

    Today I will share a python multiple modules py file data sharing example, has a good reference value, I hope to help you. Together follow the editor over to see it
    2019-01-01
  • Installing Python3 on CentOS7 Tutorial Analysis

    This article is mainly introduced in CentOS7 under the installation of Python3 tutorial analysis, the text of the sample code through the introduction of the very detailed, for everyone to learn or work with certain reference learning value, the need for friends can refer to the following
    2020-07-07
  • Introduction to Python Loop Statements

    Hello, this article is mainly about the introduction of Python loop statements, interested students quickly take a look at it, to help you remember to favorite, easy to browse next time!
    2021-12-12
  • Using OpenCV-Python to Recognize Answer Key Judging Functions

    This article introduces the use of OpenCV-Python to recognize the answer card to determine the paper, this article step by step through the example code to give you a very detailed introduction to your study or work has a certain reference value, the need for friends can refer to the next!
    2021-12-12
  • An in-depth look at several ways Python parses XML

    This article is mainly for you to explain in detail the depth of Python parsing XML several ways to ElementTree module as an example, to demonstrate the specific use of methods and scenarios, interested partners can refer to it!
    2016-02-02
  • How Python implements logic circuits for perceptrons

    This article introduces how to implement the logic circuit of the perceptron in Python, to help you better understand and use python, interested parties can learn about the
    2020-12-12
  • Python+OpenCV implementation of threshold segmentation in detail

    Threshold Segmentation is a region-based image segmentation technique based on the principle of categorizing image pixels into a number of classes. In this article, we will utilize Python+OpenCV to implement threshold segmentation, interested in
    2022-05-05
  • PyTorch Installation and Basic Usage Details

    This article introduces the PyTorch installation and basic use of the details, the text through the sample code is very detailed, for everyone's learning or work has a certain reference learning value, the need for friends below with the editorial to learn together!
    2020-08-08
  • Some suggestions for reuse and downsizing in Python programming

    This article mainly introduces the Python programming for some of the reuse and reduction of the proposal, from the official IBM technical documentation, the need for friends can refer to the following
    2015-04-04

Latest Comments