1372 rows × 5 columns
adf.insert(0,'f',1) df=adf.values numpy.random.shuffle(df) x=df[:,0:5] y=df[:,5:6] def sigmoid(x): return 1 / (1 + np.exp(-x)) def hx(x,theta): return sigmoid(np.dot(x,theta.T))# dot 相乘相加,放入sigmoid将数值映射到概率 theta=np.zeros([1,5]) theta #参数 array([[0., 0., 0., 0., 0.]]) x[1370:1400,:] array([[ 1. , -1.87820005, -6.58650017, 4.84859991, -0.021566 ], [ 1. , 4.8906002 , -3.35840011, 3.42020011, 1.0905 ]]) y array([[1.], [0.], [1.], ..., [0.], [1.], [0.]]) def loss(x,y,theta): a=np.multiply(-y,np.log(hx(x,theta)))# multiply 两个数组对应位置相乘,结果成为数组(矩阵) b=np.multiply(1-y,np.log(1-hx(x,theta))) return np.sum(a-b)/len(y) loss(x,y,theta) 0.6931471805599454 def pd(x,y,theta): grad=np.zeros(theta.shape)#梯度结果,占位 error=(hx(x,theta)-y).ravel()# ravel 多维数组拉成一维数组 for i in range(len(theta.ravel())):# 所有参数的偏导 grad[0,i]=(np.sum(np.multiply(error,x[:,i]))/len(x)) return grad def DM1(x,y,theta,YuZhi,learnning_Rate): start_time=time.time() i=0 # k=0 grad=np.zeros(theta.shape) Loss=[loss(x,y,theta)] while True: grad=pd(x[k:k+1],y[k:k+1],theta) k+=1 if k>=len(x): k=0 numpy.random.shuffle(df) x=df[:,0:5] y=df[:,5:6] theta=theta-(learnning_Rate*grad) Loss.append(loss(x,y,theta)) i+=1 if i>YuZhi: break return theta,i-1,Loss,time.time()-start_time ENDtheta,times,ENDcosts,spend=DM1(x,y,theta,50000,0.003) print('耗时:%f'%(spend)) fig,ax=plt.subplots(figsize=(12,4)) ax.plot(np.arange(len(ENDcosts)),ENDcosts,'r') ax.set_xlabel("Iter") ax.set_ylabel("Loss") 耗时:11.588989 print(ENDcosts[-1]) 0.03195626474525613