question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The `np.dot` resultant computation was automatically rounded

See original GitHub issue

By 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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
charriscommented, Mar 3, 2019

You are computing the wrong things. print(np.dot(M, result[-1])) should be print(np.dot(M.T, result[-1])) in order to agree with the other computations. The “manual” computations are not matrix multiplication.

1reaction
charriscommented, Mar 3, 2019

Try

>>> np.set_printoptions(precision=30)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found