from sklearn.datasets import load_iris
from sklearn.cross_validation import train_test_split
import numpy as np
def sGradDescent(x,y):
alpha=0.01
deviation = 1
iter = 0
w=np.random.rand(4)
while True:
for i in range(112):
deviation = 0
h = w[0] * x[i][0] + w[1] * x[i][1]+w[2] * x[i][2] + w[3] * x[i][3]
for j in range(4):
w[j] = w[j] + alpha * (y[i] - h)*x[i][j]
iter = iter + 1
for i in range(112):
deviation = deviation + (y[i] - (w[0] * x[i][0] + w[1] * x[i][1]+w[2] * x[i][2] + w[3] * x[i][3])) ** 2
if iter >150 :
break
return w
def sigmoid(x):
return (1/(1+np.exp(-x)) )
if __name__=="__main__":
data = load_iris()
X = data.data
y = data.target
features = data.feature_names
targets = data.target_names
X_train_raw, X_test_raw, y_train, y_test = train_test_split(X,y)#112,38
weight1 = []
ih=[]
for i in range(3):
labelMat1 = []
for j in range(112):
if y_train[j] == i:
labelMat1.append( 1)
else:
labelMat1.append(0)
weight1.append(sGradDescent(X_train_raw,labelMat1))
for j in range(38):
ob=0
h=np.zeros(3)
for i in range(3):
ob=X_test_raw[j][0]*weight1[i][0]+X_test_raw[j][1]*weight1[i][1]+X_test_raw[j][2]*weight1[i][2]+X_test_raw[j][3]*weight1[i][3]
h[i] = sigmoid(ob)
voteResult=h.tolist()
ih.append(voteResult.index(max(voteResult)) )
# print(np.where(h==np.max(h)),y_test[j])
error = 0.0
for j in range(38):
if ih[j] != y_test[j]:
error = error +1
pro = 1 - error / 38
print (pro)