關於多元線性回歸分析 Python SPSS

2022-10-04 14:54:21 字數 3068 閱讀 9270

原始資料在這裡

1.觀察資料

首先,用pandas開啟資料,並進行觀察。

import numpy

import pandas as pd

import matplotlib.pyplot as plt

%matplotlib inline

data = pd.read_csv('folds5x2_pp.csv')

data.head()

會看到資料如下所示:

這份資料代表了乙個迴圈發電廠,每個資料有5列,分別是:at(溫度), v(壓力), ap(濕度), rhwww.cppcns.com(壓強), pe(輸出電力)。我們不用糾結於每項具體的意思。

我們的問題是得到乙個線性的關係,對應pe是樣本輸出,而at/v/ap/rh這4個是樣本特徵, 機器學習的目的就是得到乙個線性回歸模型,即: pe=0+1∗at+2∗v+3∗ap+4∗rh 而需要學習的,就是0,1,2,3,4這5個引數。

接下來對資料進行歸一化處理:

data = (data - data.mean())/data.std()

因為回歸線的截距0是不受樣本特徵影響的,因此我們在此可以設立乙個x0=1,使得回歸模型為:

pe=0*x0+1∗at+2∗v+3∗ap+4∗rh

將方程向量化可得:

www.cppcns.compe = h(x) = x (應轉置)

2.線性回歸

**性回歸中,首先應建立 cost function,當 cost function 的值最小時所取得值為所求的。

**性回歸中,cost function如下所示:

因此,可以在python中建立函式求損失方程:

def costfunction(x,y,theta):

inner = np.power((x*theta.t)-y,2)

return np.sum(inner)/(2*len(x))

然後,設初始為=[0,0,0,0,0],可得到最初的j()值為0.49994774247491858,**如下所示

col = data.shape[1]

x = data.iloc[:,0:col-1]

y = data.iloc[:,col-1:col]

x = np.matrix(x.values)

y = np.matrix(y.values)

theta = np.matrix(np.array([0,0,0,0,0]))程式設計客棧

temp = np.matrix(np.zeros(theta.shape))

costfnoqldldunction(x,y,theta)

接下來,有兩種方法可以使用。1.梯度下降法(gradient descent)和 2.最小二乘法(normal equation)。在此我們使用梯度下降法來求解。

梯度下降法是求得j對的偏導數,通過設定步長,迭代使j()逐步下降,從而求得區域性最優解。

公式如下所示:

j:特徵編號

m:樣本編號

我們可以在python中寫出計算迭代後的和j()

def gradientdescent(x,y,theta,alpha,iters):

temp = np.matrix(np.zeros(theta.shape))

parameters = int(theta.r**el().shape[1])

cost = np.zeros(iters)

for i in range(iters):

error = (x*theta.t)-y

for j in range(parameters):

term = np.multiply(error,x[:,j])

temp[0,j] = theta[0,j] - (alpha/len(x))*np.sum(term)

theta = temp

cost[i] = costfunction(x,y,theta)

return theta,cost

在此,我設定初始的為0.1,可求得迭代1000次後0,1,2,3,4的值分別是:

-5.22080706e-14,-8.63485491e-01,-1.74182863e-01,2.16058120e-02,-1.35205248e-01

此時 j()的值為0.0379648。

通過,視覺化j()和迭代次數可以發現,j()收斂的非常快。

畫圖觀察**值和損失值,距離直線約近說明損失越小:

predicted = x*g.t

predicted = predicted.flatten().a[0]

y_f= y.flatten().a[0]

fig, ax = plt.subplots()

ax.scatter(y_f,predicted)

ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)

ax.set_xlabel('measured')

ax.set_ylabel('predicted')

plt.show()

3.sckit-learn

因為j()收斂的太快了…所以我又用sckit-learn和spss驗證了一下。

先看sckit-learn,在sklearn中,線性回歸是使用的最小二乘法而不是梯度下降法,用起來也十分的簡單。

**如下:

from sklearn import linear_model

model = linear_model.linearregression()

model.fit(x, y)

列印出值後發現和梯度下降法算出來的相差無幾,0,1,2,3,4的值分別是:

0,-0.86350078,-0.17417154,0.02160293,-0.13521023

4.spss

在看看spss

同樣先將資料標準化後進行線

然後進行線性回歸分析得到結果:

嘛…和前面兩種方法的結果也差不多…就這樣吧。

本文標題: 關於多元線性回歸分析——python&spss

本文位址: /jiaoben/python/301306.html

多元線性回歸分析

功能 多元線性回歸分析 include math.h include stdio.h include stdlib.h typedef struct rmatrix rm,rmp rm 實矩陣型別,rmp 實矩陣型別指標 typedef struct cnumber cnum,cnump cnum ...

多元線性回歸分析示例

呼叫的回歸函式如下 function beta,stats,ynew,ylr regres2 x,y,xnew,pp beta stats ynew ylr n,p size x m p 1 if n 1 pp 0 enda ones size y x beta,btm1,rtm,rtm1,stat...

多元線性回歸

多元線性回歸的基本原理和基本計算過程與一元線性回歸相同,但由於自變數個數多,計算相當麻煩,一般在實際中應用時都要借助統計軟體。介紹多元線性回歸的一些基本問題。但由於各個自變數的單位可能不一樣,比如說乙個消費水平的關係式中,工資水平 受教育程度 職業 地區 家庭負擔等等因素都會影響到消費水平,而這些影...