python 重启内核_Python从零开始的内核回归

python 重启内核

Every beginner in Machine Learning starts by studying what regression means and how the linear regression algorithm works. In fact, the ease of understanding, explainability and the vast effective real-world use cases of linear regression is what makes the algorithm so famous. However, there are some situations to which linear regression is not suited. In this article, we will see what these situations are, what the kernel regression algorithm is and how it fits into the scenario. Finally, we will code the kernel regression algorithm with a Gaussian kernel from scratch. Basic knowledge of Python and numpy is required to follow the article.

e。通过学习什么回归方式,以及如何进行线性回归算法的工作非常初学者在机器学习开始。 实际上,算法的易懂性,可解释性和广泛有效的线性回归实际使用案例就是使该算法如此出名的原因。 但是,在某些情况下线性回归不适合。 在本文中,我们将了解这些情况,内核回归算法是什么以及它如何适合该场景。 最后,我们将从头开始使用高斯内核对内核回归算法进行编码。 阅读本文需要具备Python和numpy的基础知识。

线性回归简要回顾 (Brief Recap on Linear Regression)

Given data in the form of N feature vectors x=[x₁, x₂, …, xₙ] consisting of n features and the corresponding label vector y, linear regression tries to fit a line that best describes the data. For this, it tries to find the optimal coefficients cᵢ, i∈{0, …, n} of the line equation y = c+ cx₁+cx₂+…+cxₙ usually by gradient descent with the model accuracy measured on the RMSE metric. The equation obtained is then used to predict the target yₜ for new unseen input vector xₜ.

N个特征向量x = [X₁,x₂,...,Xₙ]组成的n个特征和对应的标签向量y,线性回归尝试的形式给定的数据,以适应线路最能描述的数据。 对于这一点,它试图找到最佳系数cᵢ, ∈{0,...,N}的直线方程Y = C₀+ C₁X₁+ C₂X₂+ ... + CₙX通常由梯度ₙ的下降,并以RMSE指标衡量的模型准确性。 然后将获得的方程式用于预测新的看不见的输入向量x的目标y

Linear regression is a simple algorithm that cannot model very complex relationships between the features. Mathematically, this is because well, it is linear with the degree of the equation being 1, which means that linear regression will always model a straight line. Indeed, this linearity is the weakness of the linear regression algorithm. Why?

线性回归是一种简单的算法,无法对要素之间的非常复杂的关系建模。 从数学上讲,这是因为它很好,它在方程的次数为1时是线性的,这意味着线性回归将始终对直线建模。 确实,这种线性是线性回归算法的弱点。 为什么?

Well, let’s consider a situation where our data doesn’t have the form of a straight line: let’s take data generated using the function f(x) = x³. If we use linear regression to fit a model to this data, we will never get anywhere close to the true cubic function because the equation for which we are finding the coefficients does not have a cubic term! So, for any data not generated using a linear function, linear regression is very likely to underfit. So, what do we do?

好吧,让我们考虑一下数据不呈直线形式的情况:让我们看一下使用函数f(x)=x³生成的数据 如果我们使用线性回归将模型拟合到该数据,我们将永远无法接近真正的三次函数,因为我们要为其找到系数的方程式没有三次项! 因此,对于未使用线性函数生成的任何数据,线性回归很可能不适合。 那么我们该怎么办?

We can use another type of regression called polynomial regression which tries to find optimal coefficients of a (as the name suggests) polynomial equation with the degree of the equation being n, n⪈1. However, with polynomial regression another problem arises: as a data analyst, you cannot know what the degree of the equation should be so that the resulting equation fits best to the data. This can only be determined by trial and error which is made more difficult by the fact that above degree 3, the model built using polynomial regression is difficult to visualize.

我们可以使用一个叫做多项式回归另一种类型的回归它试图找到一个最佳系数(顾名思义)多项式方程与方程为N,N-⪈1的程度。 但是,使用多项式回归会出现另一个问题:作为数据分析人员,您不知道方程式的度数以使所得方程式最适合数据。 这只能通过反复试验来确定,而由于三次以上的事实,使用多项式回归建立的模型难以可视化,因此更加困难。

This is where kernel regression can come to the rescue!

这是内核回归可以解决的地方!

什么是内核回归? (What is Kernel Regression?)

Seeing the name, you may ask that if ‘linear’ in linear regression meant a linear function and ‘polynomial’ in polynomial regression meant a polynomial function, what does ‘kernel’ mean? Turns out, it means a kernel function! So, what is a kernel function? Simply, it is a similarity function that takes two inputs and spits out how similar they are. We will see shortly how a kernel function is used in kernel regression.

看到名称,您可能会问,如果线性回归中的“线性”表示线性函数,而多项式回归中的“多项式”意味着多项式函数,那么“内核”是什么意思? 原来,这意味着内核功能! 那么,什么内核函数? 简而言之,它是一个相似度函数,它接受两个输入并吐出它们的相似度。 我们很快将看到在内核回归中如何使用内核函数。

Now about kernel regression. Unlike linear and polynomial regression in which the optimal parameter vector c=[c₁, c₂, …, cₙ] needs to be learnt, kernel regression is non-parametric, meaning that it calculates the target yₜ by performing computations directly on the input xₜ.

现在介绍内核回归。 与线性和多项式回归,其中最佳参数矢量c = [C₁,C₂,...,Cₙ]需要学习,核回归是非参数,这意味着它计算目标ÿₜ通过直接在执行计算输入xₜ。

How?

怎么样?

Given data points (xᵢ, yᵢ) Kernel Regression goes about predicting by first constructing a kernel k for each data point xᵢ. Then for a given new input xₜ, it computes a similarity score with each xᵢ (given by xᵢ-xₜ) using the kernel ; the similarity score acts as a weight wᵢ that represents the importance of that kernel (and corresponding label yᵢ) in predicting the target yₜ. The prediction is then obtained by multiplying the weight vector w= [w₁, w₂, …, wₙ] with the label vector y= [y₁, y₂, …, yₙ].

给定数据点( xy )内核回归通过首先为每个数据点x构造一个内核k来进行预测。 然后,对于一个给定的新的输入xₜ,它计算一个相似性得分与每个xᵢ使用内核(由Xᵢ-Xₜ给出); 相似度得分作为权重wᵢ,表示内核的重要性(和相应的标记yᵢ)在预测对象物Yₜ。 然后通过将权重向量w = [ w₁w 2,… wₙ]乘以标记向量y = [ y yy 2,… yₙ]来获得预测

Image for post
Image by Author: Kernel Regression in Equations
图片由作者提供:方程中的内核回归

Now, there can be different kernel functions which give rise to different types of kernel regressions. One such type is the Gaussian Kernel Regression in which the shape of the constructed kernel is the Gaussian curve also known as the bell-shaped curve. In the context of Gaussian Kernel Regression, each constructed kernel can also be viewed as a normal distribution with mean value xᵢ and standard deviation b. Here, b is a hyperparameter that controls the shape (in particular, the width of the Gaussian curve in Gaussian kernels) of the curve. The equation for the Gaussian kernel k is given below. Notice the similarity between this equation and that of the Gaussian (also called normal) distribution.

现在,可以有不同的内核函数,从而导致不同类型的内核回归。 一种这样的类型是高斯核回归,其中构造的核的形状是高斯曲线,也称为钟形曲线。 在高斯核回归的情况下,各构造内核还可以被看作是与平均值xᵢ和标准偏差B A正态分布 在此,b是控制曲线的形状(特别是高斯核中的高斯曲线的宽度)的超参数。 高斯核k的等式在下面给出。 请注意,该方程式与高斯分布(也称为正态分布)的相似性。

Image for post
Image by Author: Gaussian Kernel Equation
图片作者:高斯核方程

We will code this type of kernel regression next.

接下来,我们将编码这种类型的内核回归。

编码高斯核回归 (Coding Gaussian Kernel Regression)

We will first look at the case of a one-dimensional feature vector and then extend it to n dimensions.

我们将首先看一维特征向量的情况,然后将其扩展到n维。

from scipy.stats import norm
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import mathclass GKR:

def __init__(self, x, y, b):
self.x = x
self.y = y
self.b = b

'''Implement the Gaussian Kernel'''
def gaussian_kernel(self, z):
return (1/math.sqrt(2*math.pi))*math.exp(-0.5*z**2)

'''Calculate weights and return prediction'''
def predict(self, X):
kernels = [self.gaussian_kernel((xi-X)/self.b) for xi in self.x]
weights = [len(self.x) * (kernel/np.sum(kernels)) for kernel in kernels]
return np.dot(weights, self.y)/len(self.x)

We define a class for Gaussian Kernel Regression which takes in the feature vector x, the label vector y and the hyperparameter b during initialization. Inside the class, we define a function gaussian_kernel() that implements the Gaussian kernel. You can see that we just write out the mathematical equation as code. Next, we define the function predict() that takes in the feature vector xₜ (referred to in code as X) whose target value has to be predicted. Inside the function, we construct kernels for each xᵢ, calculate the weights and return the prediction, again by plugging in the mathematical equations into code as-is.

我们为高斯核回归定义了一个类,该类在初始化过程中接受特征向量x,标签向量y和超参数b 。 在类内部,我们定义了一个实现高斯内核的函数gaussian_kernel() 。 您可以看到我们只是将数学方程写为代码。 接下来,我们定义函数predict() ,该函数接受必须预测目标值的特征向量x X (在代码中称为X )。 在函数内部,我们为每个x construct构造内核,计算权重并返回预测,再次将数学方程式直接插入代码中。

Image for post
Image by Author: Visualizing the Different Constructed Kernels
作者提供的图像:可视化不同构造的内核

Now, let’s pass in some dummy data and see the prediction that is output. We predict the value for xₜ = 50 (by ignoring for demonstration purposes that it is already present in training data)

现在,让我们传递一些虚拟数据,看看输出的预测。 我们预测值对于xₜ= 50(通过忽略用于演示目的,它已经存在于训练数据)

gkr = GKR([10,20,30,40,50,60,70,80,90,100,110,120], [2337,2750,2301,2500,1700,2100,1100,1750,1000,1642, 2000,1932], 10)
gkr.predict(50)

This gives us an output of 1995.285

这样我们得到的输出是1995.285

Image for post
Image by Author: Graphically, we can observe that the weights w_i for x_t = 50 are the points where a perpendicular from the point of intersection between different kernels and the dotted line meet the y-axis
作者的图片:通过图形,我们可以观察到x_t = 50的权重w_i是不同核与虚线的交点的垂直线与y轴相交的点

Now, let’s extend the code for the case of n dimensional feature vectors. The only modification we need to make is in the similarity score calculation. Instead of obtaining the difference between xᵢ and xₜ, we calculate the similarity score in the n dimensional case as the Euclidean distance ||xᵢ-xₜ|| between them. Note that for the purposes of handling n dimensional vectors, we use numpy wherever needed.

现在,让我们针对n维特征向量的情况扩展代码。 我们需要做的唯一修改是相似度分数计算。 代替获得Xᵢ并且xₜ,我们计算在n维的情况作为欧几里得距离的相似性得分||之间的差的 Xᵢ-Xₜ|| 它们之间。 请注意,出于处理n维向量的目的,我们在需要时使用numpy。

from scipy.stats import multivariate_normal
'''Class for Gaussian Kernel Regression'''
class GKR:

def __init__(self, x, y, b):
self.x = np.array(x)
self.y = np.array(y)
self.b = b

'''Implement the Gaussian Kernel'''
def gaussian_kernel(self, z):
return (1/np.sqrt(2*np.pi))*np.exp(-0.5*z**2)

'''Calculate weights and return prediction'''
def predict(self, X):
kernels = np.array([self.gaussian_kernel((np.linalg.norm(xi-X))/self.b) for xi in self.x])
weights = np.array([len(self.x) * (kernel/np.sum(kernels)) for kernel in kernels])
return np.dot(weights.T, self.y)/len(self.x)
Image for post
Image by Author: Visualizing the Constructed 3D Gaussian Kernels
图片由作者提供:可视化构造的3D高斯核

Again, let’s pass in some 2D dummy data and predict for xₜ = [20, 40].

再次,让我们传递一些2D虚拟数据,并预测x = [20,40]。

gkr = GKR([[11,15],[22,30],[33,45],[44,60],[50,52],[67,92],[78,107],[89,123],[100,137]], [2337,2750,2301,2500,1700,1100,1000,1642, 1932], 10)
gkr.predict([20,40])

We get yₜ = 2563.086.

我们得到y = 2563.086。

The extended code (including for visualizations) for this article can be found on GitHub and Kaggle.

可以在GitHub和Kaggle上找到本文的扩展代码(包括可视化)。

结论 (Conclusion)

We saw where and why linear regression and polynomial regression cannot be used and with that background understood the intuition behind and the working of kernel regression and how it can be used as an alternative. We went into the details of the Gaussian kernel regression and coded it from scratch in Python by simply plugging in the mathematical equations to code.

我们看到了不能在何处以及为什么不能使用线性回归和多项式回归,并且在这种背景下了解了内核回归的直觉和工作原理以及如何将其用作替代方法。 我们研究了高斯核回归的细节,并通过简单地将数学方程式插入代码中,从头开始用Python对其进行了编码。

翻译自: https://towardsdatascience.com/kernel-regression-from-scratch-in-python-ea0615b23918

python 重启内核

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390994.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

回归分析中自变量共线性_具有大特征空间的回归分析中的变量选择

回归分析中自变量共线性介绍 (Introduction) Performing multiple regression analysis from a large set of independent variables can be a challenging task. Identifying the best subset of regressors for a model involves optimizing against things like bias, multi…

python 面试问题_值得阅读的30个Python面试问题

python 面试问题Interview questions are quite tricky to predict. In most cases, even peoples with great programming ability fail to answer some simple questions. Solving the problem with your code is not enough. Often, the interviewer will expect you to hav…

机器学习模型 非线性模型_机器学习:通过预测菲亚特500的价格来观察线性模型的工作原理...

机器学习模型 非线性模型Introduction介绍 In this article, I’d like to speak about linear models by introducing you to a real project that I made. The project that you can find in my Github consists of predicting the prices of fiat 500.在本文中,…

10款中小企业必备的开源免费安全工具

10款中小企业必备的开源免费安全工具 secist2017-05-188共527453人围观 ,发现 7 个不明物体企业安全工具很多企业特别是一些中小型企业在日常生产中,时常会因为时间、预算、人员配比等问题,而大大减少或降低在安全方面的投入。这时候&#xf…

图片主成分分析后的可视化_主成分分析-可视化

图片主成分分析后的可视化If you have ever taken an online course on Machine Learning, you must have come across Principal Component Analysis for dimensionality reduction, or in simple terms, for compression of data. Guess what, I had taken such courses too …

TP引用样式表和js文件及验证码

TP引用样式表和js文件及验证码 引入样式表和js文件 <script src"__PUBLIC__/bootstrap/js/jquery-1.11.2.min.js"></script> <script src"__PUBLIC__/bootstrap/js/bootstrap.min.js"></script> <link href"__PUBLIC__/bo…

pytorch深度学习_深度学习和PyTorch的推荐系统实施

pytorch深度学习The recommendation is a simple algorithm that works on the principle of data filtering. The algorithm finds a pattern between two users and recommends or provides additional relevant information to a user in choosing a product or services.该…

Java 集合-集合介绍

2017-10-30 00:01:09 一、Java集合的类关系图 二、集合类的概述 集合类出现的原因&#xff1a;面向对象语言对事物的体现都是以对象的形式&#xff0c;所以为了方便对多个对象的操作&#xff0c;Java就提供了集合类。数组和集合类同是容器&#xff0c;有什么不同&#xff1a;数…

Exchange 2016部署实施案例篇-04.Ex基础配置篇(下)

上二篇我们对全新部署完成的Exchange Server做了基础的一些配置&#xff0c;今天继续基础配置这个话题。 DAG配置 先决条件 首先在配置DGA之前我们需要确保DAG成员服务器上磁盘的盘符都是一样的&#xff0c;大小建议最好也相同。 其次我们需要确保有一块网卡用于数据复制使用&…

数据库课程设计结论_结论:

数据库课程设计结论In this article, we will learn about different types[Z Test and t Test] of commonly used Hypothesis Testing.在本文中&#xff0c;我们将学习常用假设检验的不同类型[ Z检验和t检验 ]。 假设是什么&#xff1f; (What is Hypothesis?) This is a St…

配置Java_Home,临时环境变量信息

一、内容回顾 上一篇博客《Java运行环境的搭建---Windows系统》 我们说到了配置path环境变量的目的在于控制台可以在任意路径下都可以找到java的开发工具。 二、配置其他环境变量 1. 原因 为了获取更大的用户群体&#xff0c;所以使用java语言开发系统需要兼容不同版本的jdk&a…

网页缩放与窗口缩放_功能缩放—不同的Scikit-Learn缩放器的效果:深入研究

网页缩放与窗口缩放内部AI (Inside AI) In supervised machine learning, we calculate the value of the output variable by supplying input variable values to an algorithm. Machine learning algorithm relates the input and output variable with a mathematical func…

Python自动化开发01

一、 变量变量命名规则变量名只能是字母、数字或下划线的任意组合变量名的第一个字符不能是数字以下关键字不能声明为变量名 [and, as, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not,…

未越狱设备提取数据_从三星设备中提取健康数据

未越狱设备提取数据Health data is collected every time you have your phone in your pocket. Apple or Android, the phones are equipped with a pedometer that counts your steps. Hence, health data is recorded. This data could be your one free data mart for a si…

[BZOJ2599][IOI2011]Race 点分治

2599: [IOI2011]Race Time Limit: 70 Sec Memory Limit: 128 MBSubmit: 3934 Solved: 1163[Submit][Status][Discuss]Description 给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N < 200000, K < 1000000 Input 第一行 两个整数 n, k第二..n行 每行三…

分词消除歧义_角色标题消除歧义

分词消除歧义折磨数据&#xff0c;它将承认任何事情 (Torture the data, and it will confess to anything) Disambiguation as defined in the vocabulary.com dictionary refers to the removal of ambiguity by making something clear and narrowing down its meaning. Whi…

北航教授李波:说AI会有低潮就是胡扯,这是人类长期的追求

这一轮所谓人工智能的高潮&#xff0c;和以往的几次都有所不同&#xff0c;那是因为其受到了产业界的极大关注和参与。而以前并不是这样。 当今世界是一个高度信息化的世界&#xff0c;甚至我们有一只脚已经踏入了智能化时代。而在我们日常交流和信息互动中&#xff0c;迅速发…

在加利福尼亚州投资于新餐馆:一种数据驱动的方法

“It is difficult to make predictions, especially about the future.”“很难做出预测&#xff0c;尤其是对未来的预测。” ~Niels Bohr〜尼尔斯波尔 Everything is better interpreted through data. And data-driven decision making is crucial for success in any ind…

阿里云ESC上的Ubuntu图形界面的安装

系统装的是Ubuntu Server 16.04 64位版的图形界面&#xff0c;这里是转载的一个大神的帖子 http://blog.csdn.net/dk_0228/article/details/54571867&#xff0c; 当然自己也再记录一下&#xff0c;加深点印象 1.更新apt-get 保证最新 apt-get update 2.用putty或者Xshell连接远…

近似算法的近似率_选择最佳近似最近算法的数据科学家指南

近似算法的近似率by Braden Riggs and George Williams (gwilliamsgsitechnology.com)Braden Riggs和George Williams(gwilliamsgsitechnology.com) Whether you are new to the field of data science or a seasoned veteran, you have likely come into contact with the te…