從上圖可以看出來,學習時間越大,考試成績越高,相關係數為0.93,學習時間和考試成績是強相關。
但是假設我們將以上成績只是用及格和不及格來表示的話。我們則建立函數。
dataframe增加一列變成上圖的一樣,然後我們對及格不及格和花費的時間進行分析,繪製散點圖,結果如下。我們發現使用線性回歸的「最佳擬合線」在這裡就是不合適的。這裡就需要使用邏輯回歸。
現在正式開始今天的正文,如果利用python做回歸性分析。
1、線性回歸
a、提取特徵x和標籤y
exam_x = df.loc[:, '學習時間']exam_y = df.loc[:, '成績']b、建立訓練數據和測試數據
x_train, x_test, y_train, y_test = train_test_split(exam_x, exam_y, train_size=.8)
c、輸出數據
print('原始數據特徵', exam_x.shape, '訓練數據特徵', x_train.shape, '測試數據特徵', x_test.shape)
print('原始數據特徵', exam_y.shape, '訓練數據特徵', y_train.shape, '測試數據特徵', y_test.shape)d、訓練模型和計算回歸截距回歸係數
model1 = LinearRegression()
x_train = x_train.values.reshape(-1,1)x_test = x_test.values.reshape(-1,1)model1.fit(x_train,y_train)
a = model1.intercept_b = model1.coef_print('最佳擬合線:截距a=',a,'回歸係數b=',b)plt.scatter(x_train,y_train,edgecolors='red')y_train_pred = model1.predict(x_train)plt.plot(x_train,y_train_pred,color='g')plt.xlabel('hours')plt.ylabel('data')plt.show()e、模型準確性評估
t = model1.score(x_test,y_test)print('線性回歸模型準確率為',t)最終線性回歸的列印結果和繪製的圖標如下圖:
2、邏輯回歸
邏輯回歸的步驟和線性回歸基本差不多,具體見一下代碼。
1、提取特徵x和標籤yexam_x1 = df.loc[:, '學習時間']exam_y1 = df.loc[:, '是否及格']
x1_train, x1_test, y1_train, y1_test = train_test_split(exam_x1, exam_y1, train_size=.8)
print('原始數據特徵', exam_x1.shape, '訓練數據特徵', x1_train.shape, '測試數據特徵', x1_test.shape)
print('原始數據特徵', exam_y1.shape, '訓練數據特徵', y1_train.shape, '測試數據特徵', y1_test.shape)
model2 = LogisticRegression()
x1_train = x1_train.values.reshape(-1,1)x1_test = x1_test.values.reshape(-1,1)model2.fit(x1_train,y1_train)
t1 = model2.score(x1_test,y1_test)print('邏輯回歸模型準確率為',t1)邏輯回歸的最終列印結果如下圖:
以上就是整個線性回歸和邏輯回歸分析的python操作步驟,在python中需要使用引入三個函數。
from sklearn.linear_model import LinearRegressionfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LogisticRegression其中train_test_split是交叉驗證中常用的函數,功能是從樣本中隨機的按比例選取訓練數據(train)和測試數據(test)。
以下是整個分析的完整代碼,希望對大家具有參考作用。
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltfrom sklearn.linear_model import LinearRegressionfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LogisticRegression
d = {'學習時間': [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4, 4.25, 4.5, 4.75, 5, 5.5], '成績': [10, 20, 13, 43, 20, 22, 33, 50, 62, 48, 55, 75, 62, 73, 81, 75, 64, 82, 90, 93]}df = pd.DataFrame(d)
def score(x): if x >= 60: y = '及格' else: y = '不及格' return y
df['是否及格'] = df['成績'].apply(score)
exam_x = df.loc[:, '學習時間']exam_y = df.loc[:, '成績']
x_train, x_test, y_train, y_test = train_test_split(exam_x, exam_y, train_size=.8)
print('原始數據特徵', exam_x.shape, '訓練數據特徵', x_train.shape, '測試數據特徵', x_test.shape)
print('原始數據特徵', exam_y.shape, '訓練數據特徵', y_train.shape, '測試數據特徵', y_test.shape)
model1 = LinearRegression()
x_train = x_train.values.reshape(-1,1)x_test = x_test.values.reshape(-1,1)model1.fit(x_train,y_train)
a = model1.intercept_b = model1.coef_print('最佳擬合線:截距a=',a,'回歸係數b=',b)plt.scatter(x_train,y_train,edgecolors='red')y_train_pred = model1.predict(x_train)plt.plot(x_train,y_train_pred,color='g')plt.xlabel('hours')plt.ylabel('data')plt.show()
t = model1.score(x_test,y_test)print('線性回歸模型準確率為',t)
exam_x1 = df.loc[:, '學習時間']exam_y1 = df.loc[:, '是否及格']
x1_train, x1_test, y1_train, y1_test = train_test_split(exam_x1, exam_y1, train_size=.8)
print('原始數據特徵', exam_x1.shape, '訓練數據特徵', x1_train.shape, '測試數據特徵', x1_test.shape)
print('原始數據特徵', exam_y1.shape, '訓練數據特徵', y1_train.shape, '測試數據特徵', y1_test.shape)
model2 = LogisticRegression()
x1_train = x1_train.values.reshape(-1,1)x1_test = x1_test.values.reshape(-1,1)model2.fit(x1_train,y1_train)
t1 = model2.score(x1_test,y1_test)print('邏輯回歸模型準確率為',t1)