Python problem: Valueerror:operands could not being broadcast together with shapes (100,3)

Source: Internet
Author: User

Background: Datamatrix is a list of (100,3), Labelmat is a list of (1,100), weights is an array of (3,1), as shown in the following code:

>>> Import Types
>>> type (Datamatrix)
<type ' list ' >
>>> type (Labelmat)
<type ' list ' >
>>> type (weights)
<type ' Numpy.ndarray ' >

My Code:

>>> 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):
File "<stdin>", line 2, in <module>
Valueerror:operands could not being broadcast together with Shapes (100,3) (3,1)

Explain:

I have the problem is that the size of the Datamatrix,weights (100,3) (3,1), is the <type ' list ' >, <numpy.ndarray> type, not the <matrix> type, Direct product C = A*b, followed by the above error, the reason is that the array size "inconsistent", the solution without the "*" symbol, using the dot () function in NumPy, you can achieve two-dimensional array of the product, or the array type into a matrix type, using "*" multiplied by the following:

The first method:

>>> 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):
File "<stdin>", line 4, in <module>
Attributeerror: ' List ' object has no attribute ' transpose '

Analysis: This time there is no last error, but this time the error is that ' list ' has no ' transpose ' transpose function, we know only the matrix has transpose. So in the second way, Datamatrix,weights are converted directly to matrices, the code is as follows:

The second method:

>>> 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
...
>>>
This time there was no mistake and solved the problem just now.

Python problem: Valueerror:operands could not being broadcast together with shapes (100,3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.