Python學習 線性回歸和邏輯回歸應用

2021-08-22 10:09:49 字數 1968 閱讀 6136

線性回歸原理:

線性回歸實際上是尋找一組好的權重(向量w)用來與觀測向量相成並獲得近似目標值。

線性回歸是利用誤差平方和作為代價函式,最後求解出誤差最小的w權重向量。

首先利用線性回歸對波士頓房價資料進行**

boston.data資料中為各房子的基本資訊,target為房子**

from sklearn .datasets import load_boston

boston=load_boston()

from sklearn.cross_validation import train_test_split

x_train,x_test,y_train,y_test=train_test_split(boston.data, boston.target, test_size=0.2, random_state=0)

from sklearn.linear_model import linearregression

regr=linearregression()

regr.fit(x_train,y_train)

y_pre=regr.predict(x_test)

from sklearn.metrics import mean_absolute_error

print(mean_absolute_error(y_test,y_pre))

結果為3.842810589450492

效果不是很好。

接下來利用邏輯回歸對邏輯回歸對乙份信用卡異常資料進行**

import pandas as pd

credit=pd.read_csv("d:\pythonpractice\credit\creditcard.csv")

credit=credit.drop(['time','amount'],axis=1)

g1=credit[ credit["class"]==0]#對正常和異常資料進行分組

g2=credit[ credit["class"]==1]

g1=pd.dataframe.sample(g1,n=492)

#因正常資料樣本遠大於異常資料,所以對正常資料進行下取樣,保留與異常資料相同的樣本個數

from sklearn.utils import shuffle

df = shuffle(df)#讓新資料與異常資料拼接並打亂

x_train,x_test,y_train,y_test=train_test_split(df, df.class, test_size=0.2, random_state=0)

from sklearn.linear_model import logisticregression

clf=logisticregression()

clf.fit(x_train,y_train)

y_pred=clf.predict(x_test)

x_test["pre"]=y_pred

print(mean_absolute_error(y_test,y_pred))

結果非常好,0.005076142131979695

197條資料只有個別**錯誤

機器學習 線性回歸和邏輯回歸

1 線性回歸要求變數服從正態分佈,logistic回歸對變數分布沒有要求。2 線性回歸要求因變數是連續性數值變數,而logistic回歸要求因變數是分型別變數。3 線性回歸要求自變數和因變數呈線性關係,而logistic回歸不要求自變數和因變數呈線性關係 4 logistic回歸是分析因變數取某個值...

線性回歸和邏輯回歸

最近開始學習機器學習,有點心得體會,記錄一下,希望大家批評指正 監督學習 supervised learning 根據已有的資料集,知道輸入和輸出結果之間的關係。根據這種已知的關係,訓練得到乙個最優的模型。也就是說,在監督學習中訓練資料既有特徵 feature 又有標籤 label 通過訓練,讓機器...

線性回歸 和 邏輯回歸

跟著b站乙個小姐姐學的 很不錯 1 什麼是回歸 什麼是分類?簡單來說一般回歸問題在數值上是乙個連續的 而分類問題在數值上一般是離散型的 回歸 回歸模型的更傾向於很小的區域 或者是乙個x對應乙個y 線性回歸就不在討論了 這裡學習一下 邏輯回歸 邏輯回歸 聽起來是回歸 但實際上他是解決分類問題的 是乙個...