小编Sah*_*iya的帖子

Python:ValueError:形状(3,)和(118,1)未对齐:3(暗淡0)!= 118(暗淡0)

我正在尝试使用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)

python numpy scipy

6
推荐指数
1
解决办法
5万
查看次数

如何解析.txt文件?

|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)

有没有办法通过删除来读取此行|.

python csv io

1
推荐指数
1
解决办法
78
查看次数

标签 统计

python ×2

csv ×1

io ×1

numpy ×1

scipy ×1