繁体   English   中英

从 python 中的 CSV 文件读取时如何修复未对齐的形状

[英]How to fix shapes not aligned when reading from CSV file in python

我正在尝试从 Python 中的 CSV 文件导入数据集,但它显示“形状未对齐”的错误。 我想知道是否有人知道如何解决这个问题。

这是我的代码

import numpy as np
from numpy import genfromtxt

x = genfromtxt('problem1.csv', delimiter=',')

def model(x,w):
    a = w[0] + np.dot(x.T,w[1:])
    return a.T

#define sigmoid function
def sigmoid(t):
    return 1/(1 + np.exp(-t))

#the convex cross-entropy cost function
def cross_entrypy(w):

    #compute sigmoid of model
    a = sigmoid(model (x,w))

    #compute cost of label 0 points
    ind = np.argwhere (y == 0) [:,1]
    cost = -np.sum(np.log(1 - a[:,ind]))

    #add cost on label 1 points
    ind = np.argwhere(y==1)[:,1]
    cost -= np.sum(np.log(a[:,ind]))

    #compute cross-entropy
    return cost/y.size

print(cross_entrypy([3,3]))

这是我的数据集

在此处输入图像描述

这是我收到的错误信息在此处输入图像描述

- 更新 -

在此处输入图像描述

这是数据集用于的练习题

数组维度

我不确定您的数据集的含义是什么,但x的形状为(11,2)w的形状为(1,)

错误来源

从您的屏幕截图来看,错误在np.dot(xT,w[1:])中。 由于维度不匹配,您不能对xTw[1:]进行点积。

可能的解决方案

  1. 只需在x = genfromtxt('problem1.csv', delimiter=',')之后添加x=x[0]x=x[1]行。
  2. 另一种解决方案是:将np.dot(xT,w[1:])更改为np.dot(x[0].T,w[1:])np.dot(x[1].T,w[1:])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM