銀行信用卡
環(huán)境準(zhǔn)備
構(gòu)建notebook 插件環(huán)境
首先安裝anaconda 環(huán)境躏吊,再安裝如下插件
pip install jupyter_contrib_nbextensions && jupyter contrib nbextension install
數(shù)據(jù)準(zhǔn)備
raw_data.jpg
向量
接下來,我們思考如何把每一條數(shù)據(jù)平窘,向量化瑰艘。
這樣在物理上我們就可以按照下圖的方式看待數(shù)據(jù)了肤舞。
我們把房產(chǎn)放在x軸李剖,存款放在y軸,用還款的情況決定點(diǎn)的形狀偶芍。
安裝matplotlib模塊
!pip install matplotlib
%matplotlib inline
#以兩個(gè)點(diǎn)為例
import matplotlib.pyplot as plt
plt.scatter(1, 2000, marker='x')
plt.scatter(2, 50000, marker='o')
plt.xlabel('house_property')
plt.ylabel('deposit')
plt.show()
image.png
向量的知識(shí)點(diǎn)回顧
import numpy as np
from numpy import linalg
v1=np.array([3, 4]) #創(chuàng)建向量
image.png
v1.shape #查看形狀
(2,)
linalg.norm(v1) #求模長(zhǎng)
5.0
向量的加減法
v1 = np.array([0, 1])
v2 = np.array([1, 0])
v_sum = v1 + v2
v_sum
array([1, 1])
向量相加
v_sum - v1
array([1, 0])
v_sum - v2
array([0, 1])
image.png
linalg.norm(v_sum)
1.4142135623730951
向量的距離
v_sum
array([1, 1])
v1
array([0, 1])
dist = linalg.norm(v_sum - v1)
dist
1.0
new_v1 = np.array([1,1])
new_v2 = np.array([2,2])
image.png
new_dist_l1 = linalg.norm(new_v2 - new_v1, 1)
new_dist_l1 #曼哈頓距離
2.0
new_dist_l2 = linalg.norm(new_v2 - new_v1, 2)
image.png
new_dist_l2 #歐氏距離
1.4142135623730951
向量的投影
v1 = np.array([1, 1])
v2 = np.array([1, 0])
image.png
np.dot(v1, v2)
1
|v1| * |v2| * cos45
linalg.norm(v1) * np.cos(45 * np.pi/180) * linalg.norm(v2)
1.0000000000000002
linalg.norm(v1) * linalg.norm(v2) * np.cos(45 * np.pi / 180)
1.0000000000000002
有一點(diǎn)到直線的距離用向量該如何物理表示:
image.png
image.png
v3 = np.array([-1, 1])
v2 = np.array([1, 0])
np.dot(v3, v2)
-1
image.png
加載數(shù)據(jù)
建立數(shù)據(jù)集合
!pip install pandas
!pip install xlrd
import pandas as pd
!pwd
/Users/mac/pytorch_learn
data = pd.read_excel('/Users/mac/pytorch_learn/card_sample.xlsx',
names=['id','name','id_card','age','native_place','house_property','deposit','repayment'])
data
image.png
data['name']
0 張可愛
1 詹姆斯
2 歐文
3 科比
4 喬丹
5 字母哥
6 邢帥帥
Name: name, dtype: object
test_index = [2,3] #行的計(jì)數(shù)從0開始
train_index= data.index.drop(test_index).values
test_data = data.loc[test_index,:]
train_data = data.loc[train_index,:]
test_data
image.png
train_data
image.png
構(gòu)建X段化,Y
features = ['house_property','deposit']
label = ['repayment']
train_X = train_data[features].values
train_Y = train_data[label].values
test_X = test_data[features].values
test_Y = test_data[label].values
train_X
array([[ 1, 2000],
[ 1, 2100],
[ 3, 60000],
[ 3, 70000],
[ 4, 80000]])
train_Y
array([[-1],
[-1],
[ 1],
[ 1],
[ 1]])
test_X
array([[ 1, 2200],
[ 2, 50000]])
test_Y
array([[-1],
[ 1]])
建立模型
我們的目標(biāo)是找到一個(gè)函數(shù):
這個(gè)函數(shù)的輸入是傳入一個(gè)X,返回一個(gè)Y
X是一個(gè)人的房產(chǎn)和存儲(chǔ)
Y是預(yù)測(cè)此人未來是否還款
我們提出一個(gè)假設(shè)
然后用這個(gè)假設(shè)去判斷所有的X特征
本例中我們可以假設(shè)一個(gè)W,
這個(gè)W與X做內(nèi)積為正則返回+1谜疤,
這個(gè)WX做內(nèi)積為負(fù)則返回-1
這里我們思考一下內(nèi)積正負(fù)的物理意義(與向量垂直的向量稱之為法向量)
image.png
PLA.gif
對(duì)數(shù)據(jù)集合里面的所有點(diǎn)重復(fù)做操作夷磕,直到找到一個(gè)適合的W滿足所有需求
如果有一個(gè)點(diǎn)不滿足就要提出新的假設(shè)仔沿,也就是修改W大值
W怎么修改呢?
想想向量+绵跷,向量-
image.png
#init_w = np.random.random(2)
#init_w = np.array([-100000, 10])
init_w = np.array([-100000, 0])
def sign(dot):
if dot > 0:
return 1
elif dot< 0:
return -1
else:
return 0
def pla_fit(iter_num=1000, init_w=None, count=0):
print(0, init_w)
for i in range(iter_num):
if count == len(train_X):
print(i, count , "find")
return init_w
else:
count = 0
for x , y in zip(train_X, train_Y):
if (sign(np.dot(x, init_w)) != np.asscalar(y) ):
init_w = init_w + np.asscalar(y) * x
break
else:
count += 1
#print(count, init_w)
print("數(shù)據(jù)在迭代次數(shù)內(nèi)碾局,未找到w,有可能是迭代次數(shù)不夠或數(shù)據(jù)線性不可分", init_w)
len(train_X)
5
def pla_test(test_X, test_Y, new_w):
for x, y in zip(test_X, test_Y):
if sign(np.dot(x, new_w) == np.asscalar(y) ):
print(x, sign(np.dot(x, new_w)))
訓(xùn)練
iter_num=10000000000
new_w = pla_fit( iter_num, init_w=init_w)
print(new_w)
0 [-100000 0]
4477826 5 find
[-4000041 1900]
驗(yàn)證
def pla_test(test_X, test_Y, new_w):
for x, y in zip(test_X, test_Y):
if sign(np.dot(x, new_w)) == np.asscalar(y):
print(x, y)
print(sign(np.dot(x, new_w)))
pla_test(train_X, train_Y, new_w)
#可以先看一眼訓(xùn)練集合上的結(jié)果
[ 1 2000] [-1]
-1
[ 1 2100] [-1]
-1
[ 3 60000] [1]
1
[ 3 70000] [1]
1
[ 4 80000] [1]
1
pla_test(test_X, test_Y, new_w)
#看一下測(cè)試集合的結(jié)果
[ 2 50000] [1]
1
只驗(yàn)證對(duì)了一個(gè)
預(yù)測(cè)
現(xiàn)在如果有人來填寫資產(chǎn)調(diào)查清單内斯,你就可以使用剛才的學(xué)習(xí)所得來預(yù)測(cè)效果了
def pred(pred_X, w):
return sign(np.dot(pred_X, new_w))
pred_X = np.array([1, 1900])
#注意俘闯,此時(shí)你只有X忽冻,沒有Y
pred(pred_X, w=new_w)
-1
這里做一個(gè)大討論
1 如果你的數(shù)據(jù)是線性不可分的怎么辦僧诚?
2 用PDCA總結(jié)這次的內(nèi)容