需求:
基于信貸數據進行用戶信貸分類,使用Logistic算法構建模型,并比較這兩大類算法的效果
數據來源:
http://archive.ics.uci.edu/ml/datasets/Credit+Approval
數據格式:
b,30.83,0,u,g,w,v,1.25,t,t,01,f,g,00202,0,+
a,58.67,4.46,u,g,q,h,3.04,t,t,06,f,g,00043,560,+
a,24.50,0.5,u,g,q,h,1.5,t,f,0,f,g,00280,824,+
b,27.83,1.54,u,g,w,v,3.75,t,t,05,t,g,00100,3,+
b,20.17,5.625,u,g,w,v,1.71,t,f,0,f,s,00120,0,+
b,32.08,4,u,g,m,v,2.5,t,f,0,t,g,00360,0,+
b,33.17,1.04,u,g,r,h,6.5,t,f,0,t,g,00164,31285,+
a,22.92,11.585,u,g,cc,v,0.04,t,f,0,f,g,00080,1349,+
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import warnings
import sklearn
from sklearn.linear_model import LogisticRegressionCV
from sklearn.linear_model.coordinate_descent import ConvergenceWarning
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.neighbors import KNeighborsClassifier
## 設置字符集拇派,防止中文亂碼
mpl.rcParams['font.sans-serif']=[u'simHei']
mpl.rcParams['axes.unicode_minus']=False
## 攔截異常
warnings.filterwarnings(action = 'ignore', category=ConvergenceWarning)
加載數據并對數據進行預處理:
# 1. 加載數據
path = "datas/crx.data"
names = ['A1','A2','A3','A4','A5','A6','A7','A8',
'A9','A10','A11','A12','A13','A14','A15','A16']
df = pd.read_csv(path, header=None, names=names)
print ("數據條數:", len(df))
# 2. 異常數據過濾
df = df.replace("?", np.nan).dropna(how='any')
print ("過濾后數據條數:", len(df))
df.head(5)
數據條數: 690
過濾后數據條數: 653
了解A6特征的內容:
df['A6'].value_counts()
c 133
q 75
w 63
i 55
aa 52
ff 50
k 48
cc 40
m 38
x 36
d 26
e 24
j 10
r 3
Name: A6, dtype: int64
對部分特征進行啞編碼操作:
def parse(v, l):
return [1 if i == v else 0 for i in l]
def parseRecord(record):
result = []
## 格式化數據碉哑,將離線數據轉換為連續(xù)數據
a1 = record['A1']
for i in parse(a1, ('a', 'b')):
result.append(i)
result.append(float(record['A2']))
result.append(float(record['A3']))
a4 = record['A4']
for i in parse(a4, ('u', 'y', 'l', 't')):
result.append(i)
a5 = record['A5']
for i in parse(a5, ('g', 'p', 'gg')):
result.append(i)
a6 = record['A6']
for i in parse(a6, ('c', 'd', 'cc', 'i', 'j', 'k', 'm', 'r', 'q', 'w', 'x', 'e', 'aa', 'ff')):
result.append(i)
a7 = record['A7']
for i in parse(a7, ('v', 'h', 'bb', 'j', 'n', 'z', 'dd', 'ff', 'o')):
result.append(i)
result.append(float(record['A8']))
a9 = record['A9']
for i in parse(a9, ('t', 'f')):
result.append(i)
a10 = record['A10']
for i in parse(a10, ('t', 'f')):
result.append(i)
result.append(float(record['A11']))
a12 = record['A12']
for i in parse(a12, ('t', 'f')):
result.append(i)
a13 = record['A13']
for i in parse(a13, ('g', 'p', 's')):
result.append(i)
result.append(float(record['A14']))
result.append(float(record['A15']))
a16 = record['A16']
if a16 == '+':
result.append(1)
else:
result.append(0)
return result
數據特征處理(將數據轉換為數值類型的):
new_names = ['A1_0', 'A1_1',
'A2','A3',
'A4_0','A4_1','A4_2','A4_3',
'A5_0', 'A5_1', 'A5_2',
'A6_0', 'A6_1', 'A6_2', 'A6_3', 'A6_4', 'A6_5', 'A6_6',
'A6_7', 'A6_8', 'A6_9', 'A6_10', 'A6_11',
'A6_12', 'A6_13',
'A7_0', 'A7_1', 'A7_2', 'A7_3', 'A7_4', 'A7_5', 'A7_6',
'A7_7', 'A7_8',
'A8',
'A9_0', 'A9_1' ,
'A10_0', 'A10_1',
'A11',
'A12_0', 'A12_1',
'A13_0', 'A13_1', 'A13_2',
'A14','A15','A16']
datas = df.apply(lambda x: pd.Series(parseRecord(x),
index = new_names), axis=1)
names = new_names
## 展示一下處理后的數據
datas.head(5)
數據分割:
X = datas[names[0:-1]]
Y = datas[names[-1]]
X_train,X_test,Y_train,Y_test =
train_test_split(X,Y,test_size=0.1,random_state=0)
X_train.describe().T
Logistic算法模型構建(重點):
lr = LogisticRegressionCV(Cs=np.logspace(-4,1,10),
fit_intercept=True, penalty='l2', solver='lbfgs', tol=0.01,
multi_class='ovr')
lr.fit(X_train, Y_train)
LogisticRegressionCV(Cs=array([1.00000e-04, 3.59381e-04, 1.29155e-03, 4.64159e-03, 1.66810e-02,
5.99484e-02, 2.15443e-01, 7.74264e-01, 2.78256e+00, 1.00000e+01]),
class_weight=None, cv=None, dual=False, fit_intercept=True,
intercept_scaling=1.0, max_iter=100, multi_class='ovr',
n_jobs=1, penalty='l2', random_state=None, refit=True,
scoring=None, solver='lbfgs', tol=0.01, verbose=0)
Logistic算法效果輸出(重點):
lr_r = lr.score(X_train, Y_train)
print ("Logistic算法準確率:", lr_r)
print ("Logistic算法稀疏化特征比率:%.2f%%"
% (np.mean(lr.coef_.ravel() == 0) * 100))
print ("Logistic算法參數:",lr.coef_)
print ("Logistic算法截距:",lr.intercept_)
Logistic算法準確率: 0.889267461669506
Logistic算法稀疏化特征比率:2.13%
Logistic算法參數: [[-0.02376298 0.02376298 0.10977985 -0.05129678 0.0706074 -0.09335579
0.16299053 0. 0.0706074 -0.09335579 0.16299053 0.01032474
0.02940631 0.24367798 -0.24817577 -0.26062573 -0.15506122 -0.02849042
-0.09242933 0.00886997 0.12227728 0.50973846 0.25210634 -0.05958224
-0.36722359 0.01038447 0.11584139 -0.07895233 0.24565932 0.18756209
-0.29710075 -0.08548594 -0.09832244 -0.05788513 0.26194759 0.85940096
-0.85940096 0.14167783 -0.14167783 0.53642415 -0.06228778 0.06228778
0.0237657 -0.07649129 -0.0080863 -0.39691472 0.80897268]]
Logistic算法截距: [-0.40412228]
Logistic算法預測(預測所屬類別):
lr_y_predict = lr.predict(X_test)
Logistic算法獲取概率值(就是Logistic算法計算出來的結果值):
y1 = lr.predict_proba(X_test)
array([[ 1.13982099e-01, 8.86017901e-01],
[ 6.25759412e-02, 9.37424059e-01],
[ 5.07692157e-02, 9.49230784e-01],
[ 9.19448211e-01, 8.05517889e-02],
[ 4.34968508e-01, 5.65031492e-01],
[ 9.45823923e-05, 9.99905418e-01],
[ 9.51970606e-01, 4.80293939e-02],
[ 2.07954778e-02, 9.79204522e-01],
....
結果圖像展示:
x_len = range(len(X_test))
plt.figure(figsize=(14,7), facecolor='w')
plt.ylim(-0.1,1.1)
plt.plot(x_len, Y_test, 'ro',markersize = 6, zorder=3, label=u'真實值')
plt.plot(x_len, lr_y_predict, 'go', markersize = 10, zorder=2, label='Logis算法預測值,準確率=%.3f' % lr.score(X_test, Y_test))
plt.legend(loc = 'center right')
plt.xlabel('數據編號', fontsize=18)
plt.ylabel('是否審批(0表示通過,1表示通過)', fontsize=18)
plt.show()