我正在尝试使用fmin进行逻辑回归,但是由于不同的数组形状而出现错误.这是代码.
import numpy as np
import scipy.optimize as sp
data= #an array of dim (188,3)
X=data[:,0:2]
y=data[:,2]
m,n=np.shape(X)
y=y.reshape(m,1)
x=np.c_[np.ones((m,1)),X]
theta=np.zeros((n+1,1))
def hypo(x,theta):
return np.dot(x,theta)
def sigmoid(z):
return 1/(1+np.exp(-z))
def gradient(x,y,theta):#calculating Gradient
m=np.shape(x)[0]
t=hypo(x,theta)
hx=sigmoid(t)
J=-(np.dot(np.transpose(np.log(hx)),y)+np.dot(np.transpose(np.log(1-hx)),(1-y)))/m
grad=np.dot(np.transpose(x),(hx-y))/m
J= J.flatten()
grad=grad.flatten()
return J,grad
def costFunc(x,y,theta):
return gradient(x,y,theta)[0]
def Grad():
return gradient(x,y,theta)[1]
sp.fmin( costFunc, x0=theta, args=(x, y), maxiter=500, full_output=True)
Run Code Online (Sandbox Code Playgroud)
File "<ipython-input-3-31a0d7ca38c8>", line 35, in costFunc
return gradient(x,y,theta)[0]
File "<ipython-input-3-31a0d7ca38c8>", line 25, in gradient
t=hypo(x,theta)
File "<ipython-input-3-31a0d7ca38c8>", line 16, in hypo
return …Run Code Online (Sandbox Code Playgroud) |2014|,|H0AK00097|,|N00032846|,|John Cox (R)|,|R|,|AK01|,| |,| |,|Y|,|C|,|RL|,| |
Run Code Online (Sandbox Code Playgroud)
我正在.txt通过下面的代码在文件中读取上面的行.
with open("E:\campaign_finance\CampaignFin14\cands14.txt", "r+") as f:
data = list(csv.reader(f))
Run Code Online (Sandbox Code Playgroud)
有没有办法通过删除来读取此行|.