python问题:ValueError: operands could not be broadcast together with shapes (100,3) (3,1)
来源:互联网 发布:计算机端口号 编辑:程序博客网 时间:2024/05/21 14:43
原文链接:http://www.mamicode.com/info-detail-1072145.html
背景:dataMatrix是(100,3)的列表,labelMat是(1,100)的列表,weights是(3,1)的数组,属性如下代码所示:
>>> import types>>> type(dataMatrix)<type ‘list‘>>>> type(labelMat)<type ‘list‘>>>> type(weights)<type ‘numpy.ndarray‘>
我的代码:
>>> dataMatrix=dataArr>>> labelMat=labelMat.transpose()>>> m,n=shape(dataMatrix)>>> alpha=0.001>>> maxCycles=500>>> weights=ones((n,1))>>> for k in range(maxCycles):... h=logRegres.sigmoid(dataMatrix*weights)... error=(labelMat-h)... weights=weights+alpha*dataMatrix.transpose()*error错误信息:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: operands could not be broadcast together with shapes (100,3) (3,1)
解释:
本人出现的问题是,dataMatrix,weights的大小分别为(100,3) (3,1), 是<type ‘list‘>、<numpy.ndarray>类型,而不是<matrix>类型,直接进行乘积C = A*B, 之后,提示上述错误,原因是数组大小“不一致”, 解决方案,不用"*"符号,使用numpy中的dot()函数,可以实现两个二维数组的乘积,或者将数组类型转化为矩阵类型,使用"*"相乘,具体如下:
第一种方法:
>>> dataMatrix=dataArr
>>> labelMat=labelMat.transpose()
>>> m,n=shape(dataMatrix)
>>> alpha=0.001
>>> maxCycles=500
>>> weights=ones((n,1))
>>> for k in range(maxCycles):
... h=logRegres.sigmoid(dot(dataMatrix,weights))
... error=(labelMat-h)
... weights=weights+alpha*dataMatrix.transpose()*error
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
AttributeError: ‘list‘ object has no attribute ‘transpose‘
分析:这次没有出现上次的错误,但是这次出现的错误是指‘list‘没有‘transpose‘转置功能,我们知道只有矩阵才有转置。所以用第二种方法,直接将dataMatrix,weights都转换为矩阵,代码如下:
第二种方法:
>>> dataMatrix=mat(dataArr)
>>> labelMat=mat(labelMat)
>>> m,n=shape(dataMatrix)
>>> alpha=0.001
>>> maxCycles=500
>>> weights=ones((n,1))
>>> for k in range(maxCycles):
... h=logRegres.sigmoid(dataMatrix*weights)
... error=(labelMat-h)
... weights=weights+alpha*dataMatrix.transpose()*error
...
>>>
这次没有出现错误,解决了刚才的问题。
- python问题:ValueError: operands could not be broadcast together with shapes (100,3) (3,1)
- Faster RCNN训练出现问题:ValueError: operands could not be broadcast together with shapes
- ValueError: operands could not be broadcast together with shapes (3000,20) (20,20)
- operands could not be broadcast together with shapes
- python读取删除重写json以及遇见问题ValueError: No JSON object could be decoded
- ValueError: could not broadcast input array from shape (300,2,300,1) into shape (300,2,1,1)
- builtins.ValueError: could not convert string to float:的问题
- ValueError: Shapes (40, 4, 4, 1) and (?, 8, 8, 1) are not compatible
- Python中用json.loads解码字符串出错:ValueError: No JSON object could be decoded
- python flask api get时ValueError("No JSON object could be decoded")
- Python中用json.loads解码字符串出错:ValueError: No JSON object could be decoded
- Python中用json.loads解码字符串出错:ValueError: No JSON object could be decoded
- “File could not be opened”的问题
- 项目部署问题-could not be redeployed
- MBean with the same name could not be unregisted
- String could not be serialized with encoding: 4
- A server with the specified hostname could not be found.
- Keras VGG16中ValueError: filter must not be larger than the input问题的解决
- Martin Fowler 微服务全文翻译
- 洛谷P1040 加分二叉树(NOIP2003)
- 2.1 Linux驱动设备模型
- BZOJ 1682 [Usaco2005 Mar] 最小生成树 解题报告
- opencv 06 行人检测(peopledetect) vs2015
- python问题:ValueError: operands could not be broadcast together with shapes (100,3) (3,1)
- 对于Hibernate一些介绍(新手见谅)
- js日期格式转换
- The third
- 背包九讲
- SDUT 2125 数据结构实验之串二:字符串匹配
- 版本控制概念
- ubuntu16 php 以root 权限运行shell命令
- Javascript中函数声明