The `np.dot` resultant computation was automatically rounded
See original GitHub issueBy the code below, in the highlighted part as ======= Weird Behaviour Part =======
, I was expecting a vector containing not rounded numbers. However, as I tried three different ways in the code followed by the output on my terminal. I was getting the rounded number as the outcome of np.dot
API.
Reproducing code example:
import numpy as np
def get_samples():
"""
this prepares the dataset
"""
M = np.array([[0.8, 0.2],[0.3,0.7]])
B = np.array([[0.3,0.4,0.1,0.2],[0.2,0.2,0.3,0.3]])
p = np.array([0.4,0.6])
v = np.array([4,1,2])
return M, B, p, v
def dummy(M,p,B,v):
# ALPHA DYNAMIC calculate alpha
result = []
T = np.size(v)
for t in range(T):
if t == 0:
result.append(B[:, int(v[t])-1]*p)
else:
"""
======= Weird Behaviour Part =======
"""
# manually compute dot product
# output: 0.11800000000000002 0.14200000000000002
print(M[:,0]@result[-1], M[:,1]@result[-1])
# np.dot
# output: [0.1 0.15]
print(np.dot(M, result[-1]))
# pythonic way of np.dot
# output: [0.118 0.142]
print(sum([i*j for (i, j) in zip(M, result[-1])]))
result.append(sum([i*j for (i, j) in zip(M, result[-1])])*B[:, int(v[t])-1])
return np.array(result).T
if __name__ == '__main__':
M, B, p, v = get_samples()
print(dummy(M,p,B,v))
Error message:
No ERROR
Numpy/Python version information:
Python: 3.6.6 Numpy: ‘1.14.2’
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Numpy Dot, Explained - Sharp Sight
At a high level, Numpy dot computes the dot product of two Numpy arrays. ... Then, we take the resulting values, and sum...
Read more >Rounding errors: deal with operation on vectors with very ...
Let's say that we want to calculate the norm of one of these vectors (or the dot product between two of them). Also...
Read more >How to Round Numbers in Python
function that takes two numeric arguments, n and ndigits , and returns the number n rounded to ndigits . The ndigits argument defaults...
Read more >Math.Round Method (System) | Microsoft Learn
Rounds a value to the nearest integer or to the specified number of fractional digits.
Read more >2. Creating Numpy Arrays | Numerical Programming
NumPy tutorial: Creating basic array structures and manipulating arrays. ... the number of elements and creates the distance automatically.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You are computing the wrong things.
print(np.dot(M, result[-1]))
should beprint(np.dot(M.T, result[-1]))
in order to agree with the other computations. The “manual” computations are not matrix multiplication.Try