美文网首页
Python|机器学习-一元线性回归

Python|机器学习-一元线性回归

作者: rivrui | 来源:发表于2019-08-15 00:20 被阅读0次

    <p>一元线性回归模型很简单<br /></p><p><br /></p><p>y1=ax+b+ε,y1为实际值,ε为正态的误差。</p><p>y2=ax+b,y2为预测值。</p><p>ε=y1-y2。</p><section class="code-snippet__fix code-snippet__js"><pre class="code-snippet__js" data-lang="ruby"><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">def</span> <span class="code-snippet__title">model</span><span class="code-snippet__params">(a,b,x)</span></span>:</span></code><code><span class="code-snippet_outer">  <span class="code-snippet__comment"># x is vector,a and b are the common number.</span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> ax+b</span></code></pre></section><p>这里将整组数据的预测结果方差作为损失函数。</p><p><br /></p><p>J(a,b)=sum((y1-y2)^2)/n</p><section class="code-snippet__fix code-snippet__js"><pre class="code-snippet__js" data-lang="typescript"><code><span class="code-snippet_outer">def cost(a,b,x,y):</span></code><code><span class="code-snippet_outer">    # x is argu, y is actual result.    </span></code><code><span class="code-snippet_outer">    n=len(x)</span></code><code><span class="code-snippet_outer">    <span class="code-snippet__keyword">return</span> np.square(y-ax-b).sum()/n</span></code></pre></section><p>优化函数则进行使损失函数,即方差最小的方向进行搜索</p><p>a=a-theta(<span >∂</span>J/<span >∂</span>a)<br /></p><p>b=b-theta(<span >∂</span>J/<span >∂b</span>)</p><p>这里的解释就是,对影响因素a或b求损失函数J的偏导,如果损失函数随着a或b增大而增大,我们就需要反方向搜索,使得损失函数变小。</p><p>对于偏导的求取则看的别人的推导公式<img src="https://img.haomeiwen.com/i3799916/85217c78849c7b77.png" data-ratio="1" data-type="xmt-emoji" data-w="19" /><img src="https://img.haomeiwen.com/i3799916/5bad3f4474d6baea.png" data-ratio="1" data-type="xmt-emoji" data-w="19" /><br /></p><p><br /></p><p>theta为搜索步长,影响速度和精度(我猜的,实际没有验证)<br /></p><section class="code-snippet__fix code-snippet__js"><pre class="code-snippet__js" data-lang="properties"><code><span class="code-snippet_outer"><span class="code-snippet__meta">def optimize(a,b,x,y)</span>:</span></code><code><span class="code-snippet_outer"><span class="code-snippet__meta">    theta </span>=<span class="code-snippet__string"> 1e-1  # settle the step as 0.1</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__meta">    n</span>=<span class="code-snippet__string">len(x)</span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__attr">y_hat</span> = <span class="code-snippet__string">model(a,b,x)</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment">    # compute forecast values</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__meta">    da </span>=<span class="code-snippet__string"> ((y_hat-y)x).sum()/n</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__meta">    db </span>=<span class="code-snippet__string"> (y_hat-y).sum()/n</span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__attr">a</span> = <span class="code-snippet__string">a - thetada</span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__attr">b</span> = <span class="code-snippet__string">b - theta*db</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__attr">    return a, b</span></span></code></pre></section><p>使用sklearn库,可以使用现成的线性回归模型<br /></p><section class="code-snippet__fix code-snippet__js"><pre class="code-snippet__js" data-lang="makefile"><code><span class="code-snippet_outer">import numpy as np</span></code><code><span class="code-snippet_outer">from sklearn.linear_model import LinearRegression</span></code><code><span class="code-snippet_outer">import matplotlib.pyplot as plt</span></code><code><span class="code-snippet_outer"><br /></span></code><code><span class="code-snippet_outer">x = [1,2,4,5,6,6,7,9]</span></code><code><span class="code-snippet_outer">x = np.reshape(x,newshape=(8,1))</span></code><code><span class="code-snippet_outer">y =  [10,9,9,9,7,5,3,1]</span></code><code><span class="code-snippet_outer">y = np.reshape(y,newshape=(8,1))</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment"># create an instance of LinearRegression model</span></span></code><code><span class="code-snippet_outer">lr = LinearRegression()</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment"># train model</span></span></code><code><span class="code-snippet_outer">lr.fit(x,y)</span></code><code><span class="code-snippet_outer">lr.score(x,y)</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment"># compute y_hat</span></span></code><code><span class="code-snippet_outer">y_hat = lr.predict(x)</span></code><code><span class="code-snippet_outer"><span class="code-snippet__comment"># show the result</span></span></code><code><span class="code-snippet_outer">plt.scatter(x,y)</span></code><code><span class="code-snippet_outer">plt.plot(x, y_hat)</span></code><code><span class="code-snippet_outer">plt.show()</span></code></pre></section><p ><img class="rich_pages" data-ratio="0.8429878048780488" data-s="300,640" src="https://img.haomeiwen.com/i3799916/0911d64d83daf64a" data-type="png" data-w="656" ></p>

    相关文章

      网友评论

          本文标题:Python|机器学习-一元线性回归

          本文链接:https://www.haomeiwen.com/subject/iynojctx.html