如何修复在 python 中读取 CSV 文件时形状未对齐的问题
我正在尝试从 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]))
,这是我的数据集,看起来像
--update--
这是数据集用于的练习题
I'm trying to import a dataset from CSV file in Python, but it showed an error of "shapes not aligned". I wonder if anyone knows how to solve this.
here is my code
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]))
here is my dataset looks like
This is the error message I received
--update--
This is the practice question where the dataset is use for
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数组维度
我不确定您的数据集的含义是什么,但是
x
的形状为(11,2)
,w
的形状为 <代码>(1,)。错误来源
从您的屏幕截图来看,错误位于
np.dot(xT,w[1:])
中。由于维度不匹配,您无法对xT
和w[1:]
进行点积。可能的解决方案
x = genfromtxt('problem1.csv',分隔符=',')
。np.dot(xT,w[1:])
更改为np.dot(x[0].T,w[1:])
,或np.dot(x[1].T,w[1:])
。Array dimension
I am not sure what the meaning of your dataset is, but
x
has shape(11,2)
,w
has shape of(1,)
.Source of error
From your screenshot, the error is in
np.dot(x.T,w[1:])
. You cannot do dot product onx.T
andw[1:]
, because of the dimensionality mismatch.Possible solutions
x=x[0]
orx=x[1]
, right afterx = genfromtxt('problem1.csv', delimiter=',')
.np.dot(x.T,w[1:])
tonp.dot(x[0].T,w[1:])
, ornp.dot(x[1].T,w[1:])
.