AnandViswanathan89
AnandViswanathan89

Reputation: 119

limiting float point array of arrays with less decimal points

Problem:

Our problem is while using machine learning algorithm like PLSA the huge float point values are taking a lot of time. Now, how can we reduce the float point precision to just 2 decimal places and do mathematical operations?

What we have:

Initialized with the following numpy command np.zeros([2,4,3],np.float)

ndarray: [[[ 0.09997559  0.          0.89990234]
  [ 0.          0.          0.        ]
  [ 0.          0.          0.        ]
  [ 0.          0.          0.        ]]

 [[ 0.          0.          0.        ]
  [ 0.30004883  0.30004883  0.30004883]
  [ 0.          0.          0.        ]
  [ 0.          0.          0.        ]]]


**What we needed:**

[[[ 0.1         0.          0.9]
  [ 0.          0.          0.        ]
  [ 0.          0.          0.        ]
  [ 0.          0.          0.        ]]

 [[ 0.          0.          0.        ]
  [ 0.3         0.3         0.3       ]
  [ 0.          0.          0.        ]
  [ 0.          0.          0.        ]]]

Upvotes: 0

Views: 324

Answers (1)

Lee
Lee

Reputation: 31070

You could use half precision floats to reduce memory usage (sign bit, 5 bits exponent, 10 bits mantissa). See this.

>>> b=np.zeros([2,4,3],np.float)
>>> b.nbytes
192
>>> c=np.zeros([2,4,3],np.float16)
>>> c.nbytes
48

Upvotes: 1

Related Questions