In [4]:
import numpy as np
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv("./pokemon.csv")
In [3]:
Out[3]:
In [5]:
alldata = df[ (df['attack_strong_type'] == 'Normal') |(df['attack_strong_type'] == 'Flying') ]
# alldata = alldata[df['attack_strong_type'] == 'Flying']
In [6]:
f1 = alldata['height'].tolist()
f2 = alldata['weight'].tolist()
y = alldata['attack_strong_type']=='Normal'
y = [ 1 if i else 0 for i in y.tolist()]
f1 = np.array(f1)
f2 = np.array(f2)
In [7]:
c = [ 'g' if i==1 else 'b' for i in y ]
plt.scatter(f1, f2, 20, c=c, alpha=0.5,
label="Type")
plt.xlabel("Height")
plt.ylabel("Weight")
plt.legend(loc=2)
plt.show()
In [8]:
from sklearn.preprocessing import scale,MinMaxScaler
scaler1 = MinMaxScaler()
scaler2 = MinMaxScaler()
f1 = f2.reshape([f1.shape[0],1])
f2 = f2.reshape([f2.shape[0],1])
scaler1.fit(f1)
scaler2.fit(f2)
f1 = scaler1.transform(f1)
f2 = scaler2.transform(f2)
In [9]:
f1 = f1.reshape(f1.shape[0])
f2 = f2.reshape(f2.shape[0])
In [10]:
c = [ 'g' if i==1 else 'b' for i in y ]
plt.scatter(f1, f2, 20, c=c, alpha=0.5,
label="Type")
plt.xlabel("Height")
plt.ylabel("Weight")
plt.legend(loc=2)
plt.show()
In [11]:
Y = np.array([1,1,0,0,1])
A = np.array([0.8, 0.7, 0.2, 0.1, 0.9])
A2 = np.array([0.6, 0.6, 0.2, 0.1, 0.3])
def cross_entropy(Y,A):
# small tip 因 log(0) 會趨近負無限大,會產生 nan ,故這裡統一加上 0.00001
Y = np.array(Y)
A = np.array(A)
m = len(A)
cost = -(1.0/m) * np.sum(Y*np.log(A+0.00001) + (1-Y)*np.log(1-A+0.00001))
return cost
# Test cross_entropy Function
print cross_entropy(Y,A)
print cross_entropy(Y,A2)
print cross_entropy(Y,Y)
推導過程請參考 : http://speech.ee.ntu.edu.tw/~tlkagk/courses/ML_2017/Lecture/Logistic%20Regression%20(v4).pdf 中的 3~13 頁,其重點步驟如下
In [16]:
import math
w1 =1
w2 =1
b = 0
r = 0.001
def fx(x1,x2):
temp = w1*x1 + w2*x2 + b
y_head = 1. / (1. + math.exp(-1.*temp))
return y_head
In [17]:
def cross_entropy(Y, A):
m = len(A)
cost = -(1.0 / m) * np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A))
return cost
for i in range(10000):
w1_delta=0
w2_delta=0
b_delta = 0
y_error = 0
for x1,x2,y_now in zip(f1,f2,y):
y_error = y_now - fx(x1,x2)
w1_delta = -1*x1*y_error
w2_delta = -1*x2*y_error
b_delta = -1*y_error
w1 -= r*w1_delta
w2 -= r*w2_delta
b -= r*b_delta
if i % 100==0 :
error_rate = 0
y_predict = []
for x1,x2,y_now in zip(f1,f2,y):
y_predict.append(fx(x1,x2))
if y_now==1 and fx(x1,x2) < 0.5:
error_rate+=1
elif y_now==0 and fx(x1,x2) >=0.5:
error_rate+=1
print("{:0,.3f}, {:0,.3f}, {:0,.3f}, {:0,.3f}, {:0,.3f}".format(error_rate*1./len(y) ,cross_entropy(np.array(y),np.array(y_predict)),w1,w2,b) )