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)