python邏輯回歸
- 2020 年 9 月 22 日
- AI
- Python
- 邏輯回歸
#-*- coding:UTF-8 -*- #允許中文
########簡單邏輯回歸################
from collections import OrderedDict
import pandas as pd
import matplotlib.pyplot as plt #散點圖matplotlib
import numpy
#1. 數據集
examDict={'LearningTime':[0.5,0.75,1.00,1.25,1.50,1.75,1.75,2.00,2.25,2.50,2.75,3.00,3.25,3.50,4.00,4.25,4.50,4.75,5.00,5.50],
'Score':[0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]}
examOrderDict=OrderedDict(examDict)
examDf=pd.DataFrame(examOrderDict)
exam_X=examDf.loc[:,'LearningTime'] #提取特徵
exam_y=examDf.loc[:,'Score'] #提取label
#作圖看分布情況
#plt.scatter(exam_X,exam_y,color="b",label="exam data")#散點圖matplotlib, b:blue, w:white等
#plt.xlabel("Learning Time") #添加圖標
#plt.ylabel("Score") #添加圖標
#plt.show() #顯示影像
#2. 建立訓練集和測試集
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(exam_X,exam_y,train_size= .8) #train_size訓練集佔比
#輸出特徵和標籤
print('OriginalFeature: ',exam_X.shape,'TrainingFeature: ',X_train.shape,'TestFeature: ',X_test.shape) #.shape可以快速讀取矩陣的形狀
print('OriginalLable: ',exam_X.shape,'TrainingLable: ',y_train.shape,'TestLable: ',y_test.shape)
print(type(X_train))
##相關係數矩陣
#rDf=examDf.corr() #.corr()矩陣相關性
#print(rDf)
#3. 將訓練集特徵轉化為二維數組**行1列
X_train=X_train.values.reshape(-1,1)
#將測試集特徵轉化為二維數組**行1列
X_test=X_test.values.reshape(-1,1)
#查看用於訓練集和測試集數據
#print("The training set is \n" + str(X_train))
#print("The test set is \n" + str(X_test))
#4. 訓練模型--邏輯回歸
from sklearn.linear_model import LogisticRegression
model=LogisticRegression()
model.fit(X_train,y_train)
#LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
#penalty='l2', random_state=None, solver='liblinear', tol=0.0001, verbose=0, warm_start=False)
##最佳擬合線
#a=model.coef_
#b=model.intercept_
#print("The coeffeciency of the model is " + str(a) + ". The fit_intercept of the model is " + str(b)) #z=ax+b,y=1/(1+exp(-z))
#評估模型精確度:決定係數R平方看模型擬合程度
ModelScore=model.score(X_test,y_test)
print("The Score of the model is " + str(ModelScore))
#實施模型
import numpy as np
b=model.intercept_
a=model.coef_
x=3
z=b+a*x
pred_Y=1/(1+np.exp(-z)) #邏輯回歸的計算公式
print('The probability of passing the test is :',pred_Y)