# -*- coding: utf-8 -*-
"""
Created on Wed May 16 16:51:13 2018

@author: 被遗弃的庸才
"""
import numpy as np
#import matplotlib.pyplot as plt
xandy=[]
with open('train-classification.txt') as files:
while True:
if line.strip():
for j in range(1,59):
xandy.append(float(line.split(',')[j]))
#pass
#print()#换行
else:
break
xandy=np.array(xandy).reshape(4001,58)#得到的是4001行，57x，1列y的值
#随机给出x和y的值
#w=np.random.randn(57,1)
w=np.random.randn(57,1)
b=np.random.randn(1)
lr=10#手动调学习率
literator=10000#迭代次数为10000次
cost=[]
hen=[]
for j in range(literator):
y=np.dot(xandy[:,0:57],w)+b
active=1/(1+np.exp(-y))
#print(np.log(1-active))
#cost.append(-sum(xandy[:,57].reshape(4001,1)*np.log(active)+(1-xandy[:,57]).reshape(4001,1)*np.log(1-active))/xandy[:,57].size)

hen.append(j)

#print(cost)
for i in range(xandy[:,57].size):
if active[i]>0.5:
active[i]=1
else:
active[i]=0
contss=0
for i in range(xandy[:,57].size):
if active[i]== xandy[i,57]:
contss+=1
print(contss)
#plt.plot(hen[100:1000],cost[100:1000])

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120