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 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 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 normimport numpy as np import pandas as pdimport matplotlib.pyplot as pltimport 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 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 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 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.taodudu.cc/news/show-997411.html

相关文章:

  • 回归分析中自变量共线性_具有大特征空间的回归分析中的变量选择
  • python 面试问题_值得阅读的30个Python面试问题
  • 机器学习模型 非线性模型_机器学习:通过预测菲亚特500的价格来观察线性模型的工作原理...
  • pytorch深度学习_深度学习和PyTorch的推荐系统实施
  • 数据库课程设计结论_结论:
  • 网页缩放与窗口缩放_功能缩放—不同的Scikit-Learn缩放器的效果:深入研究
  • 未越狱设备提取数据_从三星设备中提取健康数据
  • 分词消除歧义_角色标题消除歧义
  • 在加利福尼亚州投资于新餐馆:一种数据驱动的方法
  • 近似算法的近似率_选择最佳近似最近算法的数据科学家指南
  • 在Python中使用Seaborn和WordCloud可视化YouTube视频
  • 数据结构入门最佳书籍_最佳数据科学书籍
  • 多重插补 均值插补_Feature Engineering Part-1均值/中位数插补。
  • 客户行为模型 r语言建模_客户行为建模:汇总统计的问题
  • 多维空间可视化_使用GeoPandas进行空间可视化
  • 机器学习 来源框架_机器学习的秘密来源:策展
  • 呼吁开放外网_服装数据集:呼吁采取行动
  • 数据可视化分析票房数据报告_票房收入分析和可视化
  • 先知模型 facebook_Facebook先知
  • 项目案例:qq数据库管理_2小时元项目:项目管理您的数据科学学习
  • 查询数据库中有多少个数据表_您的数据中有多少汁?
  • 数据科学与大数据技术的案例_作为数据科学家解决问题的案例研究
  • 商业数据科学
  • 数据科学家数据分析师_站出来! 分析人员,数据科学家和其他所有人的领导和沟通技巧...
  • 分析工作试用期收获_免费使用零编码技能探索数据分析
  • 残疾科学家_数据科学与残疾:通过创新加强护理
  • spss23出现数据消失_改善23亿人口健康数据的可视化
  • COVID-19研究助理
  • 缺失值和异常值的识别与处理_识别异常值-第一部分
  • 梯度 cv2.sobel_TensorFlow 2.0中连续策略梯度的最小工作示例

python 重启内核_Python从零开始的内核回归相关推荐

  1. python重启路由器_python 实现重启路由器

    有一些服务,需要动态IP,所以我们用重启路由器的方法实现.人工重启不可选,用定时脚本执行即可.贴代码,每种路由器,提示不一样.需要路由器有telnet功能才行.#!/usr/bin/env pytho ...

  2. python重启路由器_Python实现路由器的重启和查看实时流量

    0.问题引入 最近因为要使用一个可以插卡上网的4G路由器,但是不知道怎么回事,这个路由器总是在一个随机的时刻无法访问互联网(SIM卡是没有问题的),就是那种在网上刷着视频听着音乐看着直播突然就没有互联 ...

  3. python 重启程序_python重启程序

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! # 重启程序 def restart_program():print(重启... ...

  4. python定义符号常量_python从零开始学习(二):python中的变量与常量

    一.python变量与常量里的定义 变量:在内存中开辟一块区域,储存值,可以理解为给值起一个名字.例子 a=1,a就是变量,1就是值 常量:程序运行中不会改变的值,大部分情况和变量一起使用. 例子  ...

  5. 【Linux 内核】编译 Linux 内核 ⑦ ( 安装内核模块 | 安装内核 | 重启系统 | 查看当前内核版本 )

    文章目录 一.安装内核模块 二.安装内核 三.重启系统 四.查看当前内核版本 一.安装内核模块 确保 Linux 内核编译完成 , 没有任何报错之后 ; 参考 [Linux 内核]编译 Linux 内 ...

  6. python 股票自动交易从零开始_Python股票自动交易从零开始

    课程目录 1.Python股票自动交易从零开始~第一集~简介(Av24528809,P1).mp4 2.Python股票自动交易从零开始~第二集~安装工具包(Av24528809,P2).mp4 3. ...

  7. 初探内核之《Linux内核设计与实现》笔记上

    内核简介  本篇简单介绍内核相关的基本概念. 主要内容: 单内核和微内核 内核版本号 1. 单内核和微内核   原理 优势 劣势 单内核 整个内核都在一个大内核地址空间上运行. 1. 简单. 2. 高 ...

  8. linux内核单独安装,Linux内核编译与安装

    平台:虚拟机Ubuntu10.04 内核版本:3.0.80 ---------------------------------------------------------------------- ...

  9. 【Linux 内核】Linux 内核特性 ( 组织形式 | 进程调度 | 内核线程 | 多平台虚拟内存管理 | 虚拟文件系统 | 内核模块机制 | 定制系统调用 | 网络模块架构 )

    文章目录 一.Linux 内核特性 1.Linux 内核组织形式 2.Linux 进程调度 3.Linux 内核线程 4.Linux 内核多平台虚拟内存管理 5.Linux 虚拟文件系统 6.Linu ...

最新文章

  1. 【连载】优秀程序员的45个习惯之37——提供有用的错误信息
  2. AtCoder AGC022C Remainder Game (图论)
  3. 如何将char类型数据转化给int
  4. 65. Valid Number
  5. 【机器视觉】机器视觉光源详解
  6. 博客园代码高亮显示教程
  7. linux文件目录含义,Linux中文件权限目录权限的意义及权限对文件目录的意义
  8. Nagios 3.x 在线帮助中文版
  9. Android 中的MVP 模式
  10. 移动端前端月历日历_前端基础:必须要知道的移动端适配(2)——分辨率
  11. 命令: LIST 响应: 150 Opening BINARY mode data connection. 错误: 20 秒后无活动,连接超时 错误: 读取目录列表失败
  12. SQL实现将一个表的数据插入到另外一个表的代码
  13. Prim算法实现最小生成树MST(java)
  14. java下面哪些定义正确_Java认证考试题
  15. 问卷及量表统计与SPSS实战
  16. HTML常用table样式
  17. 新推多多旅行搅局在线旅游市场,拼多多有多少胜算?
  18. MLX90614修改地址
  19. 2017江苏高职计算机分数线,2017年江苏高考分数线公布
  20. OCCT培训笔记(刘星讲)--第2天

热门文章

  1. javascript如何阻止事件冒泡和默认行为
  2. 多线程之间的通信(等待唤醒机制、Lock 及其它线程的方法)
  3. JavaWeb项目前端规范(采用命名空间使js深度解耦合)
  4. 【leetcode】Median of Two Sorted Arrays
  5. linux驱动分离分层的概念
  6. 都客仿站系列教程四:javascript入门
  7. CNN理解比较好的文章
  8. Windows+VS2013爆详细Caffe编译安装教程
  9. python random随机数 RUNOOB python练习题50
  10. 3.13 判读是否是对称素数