如何从.CSV文件中得到相同大小的x和y来绘制散点图?

2024-05-12 21:41:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用一些.csv值来重塑数组,它给了我一个多行的错误,我也试着在StackOverflow上找到一些例子,但我没能找出这里的实际问题是什么!!每当我尝试使用np.zeros()、np.ones()和np.array()时,每次都会出现这个错误

我有考试数据,包括考试-1,考试-2和录取决定。我的x,y和θ大小不一样

def sigmoid(z):
    new_val = 1 / (1 + np.exp(-z))
    return new_val  

def h(theta,X):
    return sigmoid(np.dot(X,theta))#------Value Error    


def compute_logistic_cost(theta, X, y):
    m= len(y)
    J = (1/m) * np.sum((-y * np.log(h(theta,X))) - ((1 - y)*np.log(1 - h(theta,X))))#-----Value Error

    eps = 1e-12
    hypothesis[hypothesis < eps] = eps
    eps = 1.0 - 1e-12
    hypothesis[hypothesis > eps] = eps

    return J

X = np.ones( (3, 1) )#------Value Error and If I put 100 instead of 1 it is working
X[1:,:] = X.T

theta = np.zeros( (3, 1) )
print(compute_logistic_cost(theta, X, y))#------Value Error

theta = np.array([[1.0],
              [1.0],
              [1.0]])
print(compute_logistic_cost(theta, X, y))

theta = np.array([[0.1],
              [0.1],
              [0.1]])
print(compute_logistic_cost(theta, X, y))    

下面是错误信息,请帮助我理解值错误:形状(3100)和(3,1)未对齐:100(尺寸1)!=3(尺寸0)


Tags: returnvaluedef错误npzeroserroreps
1条回答
网友
1楼 · 发布于 2024-05-12 21:41:24

看起来这是一个数学上的失败-由于矩阵不兼容,取两个矩阵的点积将失败。有关显示此值错误的文档,请参见here。具体来说:

Raises: 

ValueError

    If the last dimension of a is not the same size as the second-to-last dimension of b.

矩阵数学很难。看起来您只需要转换较小的矩阵,点积就可以成功执行。我不知道是否足够告诉你脚本的其余部分是正确的,但这至少会清除错误,让你继续

希望有帮助

相关问题 更多 >