close

 

 

#Logistic Regression
#雖然名為迴歸,但常⽤於分類(⼆元或多類別)
from sklearn import preprocessing, linear_model
import pandas as pd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cross_validation  import train_test_split
plt.style.use('ggplot')
plt.rcParams['font.family']='SimHei' #⿊體

df2=pd.read_csv("kaggle_titanic_train.csv",encoding="big5") #鐵達尼
df3=df2[['Sex','Pclass','Age','Survived']]                                    #用'Sex','Pclass','Age' 三個變數預測'Survived'(存活率)
df3.head()
l1.PNG

 


# 創造 dummy variables   將SEX轉換成 0,1
label_encoder = preprocessing.LabelEncoder()
encoded_Sex = label_encoder.fit_transform(df3["Sex"])
df3["Sex"]=encoded_Sex
df3.head()

l2.PNG

 

 

#切分訓練 測試資料
x=df3[['Sex','Pclass','Age']]
y=df3[['Survived']]

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=20170816) #random_state 種子值

x_train


#標準化 :為了避免偏向某個變數去做訓練
from sklearn.preprocessing  import StandardScaler
sc=StandardScaler()

sc.fit(x_train)

x_train_nor=sc.transform(x_train)
x_test_nor=sc.transform(x_test)

 

#訓練資料分類效果(3個參數)
from sklearn.linear_model  import LogisticRegression
lr=LogisticRegression()
lr.fit(x_train_nor,y_train)

# 印出係數
print(lr.coef_)
# 印出截距
print(lr.intercept_ )

output:

係數: [[-1.30778904 -1.19424825 -0.64119698]]
截距: [-0.5100545]

 

#機率分類判斷
import numpy as np
np.round(lr.predict_proba(x_test_nor),3)

output:

分別計算存活率是yes的機率、是no的機率

 

                no            yes

array([[ 0.903,  0.097],
       [ 0.086,  0.914],
       [ 0.559,  0.441],
       [ 0.661,  0.339],
       [ 0.912,  0.088],
       [ 0.924,  0.076],
       [ 0.328,  0.672],
       [ 0.093,  0.907],
       [ 0.389,  0.611],
       [ 0.07 ,  0.93 ],
       [ 0.409,  0.591],
       [ 0.917,  0.083],

             ....

 

#評估分類模型的好壞--用混淆矩陣


from sklearn.metrics import confusion_matrix
cnf=confusion_matrix(y_test,lr.predict(x_test_nor))
print('混淆矩陣: ',cnf)


import itertools
target_name=['yes','no'] #是否生存 
plot_confusion_matrix(cnf,classes=target_name,title='混淆矩陣')
plt.show()
 

 

 

 

 

l3.PNG

混淆矩陣的評估 如下圖:

l4.PNG

 

因此,此邏吉斯回規模型的準確率為:

Accuracy:  106+64/106+64+24+21 =0.79 (79%的準確率)

 

 

PS: 要使用視覺化混淆矩陣要先執行以下的code (官網提供的)

#plot confusion matrix 官網提供

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 to52016 的頭像
    to52016

    Eason [資料科學//Python學習/資料庫] & [拍片&剪片]

    to52016 發表在 痞客邦 留言(0) 人氣()