2

In my code, I randomly generate indices of a large numpy array using the code

import numpy as np
random = np.random.RandomState(seed=1000)

r = 140000
c = 30000
k = 210000000
flat_indx = random.choice(xrange(r*c),k,replace=False)

But it returns floating number and because of that my code for unravel fails

Indx = np.unravel_index(flat_indx,(U,I))

I checked the type of the returned array and it is

type 'numpy.float64'

thought U and I are np.unit64.

This is what I got when I tried it on python shell

> random.choice(xrange(140000*30000),10,replace=False) array([2.63189959e+09, 9.52388615e+08, 2.79972090e+09, 1.66392341e+09,
>        3.96565768e+09, 4.18275392e+09, 2.18902320e+07, 2.54430836e+09,
>        1.52407003e+08, 3.36787629e+09])

How can I get it corrected ?

Shew
  • 1,557
  • 1
  • 21
  • 36
  • Have you tried `.astype(int)`? – Aryerez Nov 12 '19 at 12:12
  • 1
    it is not giving you floating numbers. The numbers are of the order of `1e9` – learner Nov 12 '19 at 12:22
  • @learner, then why unravel_index gives me error about floating number converting to int ? – Shew Nov 12 '19 at 18:35
  • @Shew it is mostly because the datatype int overflows and can't accommodate the value. Python internally converts it into float to store such huge values. You can refer [here](https://stackoverflow.com/questions/2104884/how-does-python-manage-int-and-long) for more information – learner Nov 12 '19 at 18:40
  • I assume you're on Windows. What version of NumPy are you using? – Mark Dickinson Nov 12 '19 at 21:07
  • @MarkDickinson I am on Linux, and I mostly work on archlinux – Shew Nov 13 '19 at 04:28

0 Answers0