Reputation: 1
Bear with me as I'm not very experienced with coding, but an example of my problem is as follows:
I define a matrix V as below and then attempt to scale the first row by a factor equal to the first entry in V:
V=np.array([[3,1],[1,3]])
V[0]=V[0]/V[0,0]
print(V)
Output:
[[1 0]
[1 3]]
For some reason, Python has rounded the scaled entries down to the nearest whole number. Another example:
V=np.array([[3,1],[1,3]])
print(V[0]*1.8)
Output:
[5.4 1.8]
There seems to be no issue, but when I assign the first row of V equal to the scaled version the same problem arises.
V=np.array([[3,1],[1,3]])
V[0]=V[0]*1.8
Output:
[[5 1]
[1 3]]
Expected output:
[[5.4 1.8],
[1 3]]
Clearly, the output should have [5.4 1.8]
as the first row, but Python has rounded each entry down to the nearest whole number.
Why is Python doing this? What am I doing wrong?
Problem outlined completely above, not much to say here.
Thanks,
James
Upvotes: 0
Views: 37
Reputation: 487
This is because your numpy array is int
. ALL elements in the numpy array must be the same type. So when you multiply by a float
your array, the array remains as an int
(no decimals)
You need to convert your array to float
first in order to get the output you expect.
V=np.array([[3,1],[1,3]]).astype('float')
V[0]=V[0]*1.8
Upvotes: 2