7

I have created a array by generating a group of random numbers and converted them into int type. However, i do not think my way below is efficient. Is there a best way to change the data type in a array?

# standard normal distributed random numbers
c=random.randn(5,5)
c

array([[-0.37644781, -0.81347483, -0.36895952, -2.68702544, -0.96780752],
   [ 0.05293328,  1.65260753,  0.55586611, -0.5786392 ,  0.50865003],
   [ 1.25508358,  0.51783276,  2.36435212, -0.23484705, -1.20999296],
   [ 2.07552172,  0.65506648,  0.10231436, -0.26046045,  0.40148111],
   [ 0.24864496, -1.8852587 , -2.51091886,  1.01106003,  1.53118353]])

d=array([[-0.37644781, -0.81347483, -0.36895952, -2.68702544, -0.96780752],
       [ 0.05293328,  1.65260753,  0.55586611, -0.5786392 ,  0.50865003],
       [ 1.25508358,  0.51783276,  2.36435212, -0.23484705, -1.20999296],
       [ 2.07552172,  0.65506648,  0.10231436, -0.26046045,  0.40148111],
       [ 0.24864496, -1.8852587 , -2.51091886,  1.01106003,  1.53118353]],dtype=int)

d
array([[ 0,  0,  0, -2,  0],
       [ 0,  1,  0,  0,  0],
       [ 1,  0,  2,  0, -1],
       [ 2,  0,  0,  0,  0],
       [ 0, -1, -2,  1,  1]])
2
  • Do you need them normally distributed? If not, np.random.random_integers(-2, 2, (5, 5)) or similar might do it. Commented Apr 24, 2015 at 10:46
  • I understand what you say, but my object is to create a array by using any way and change the data type in the array to another type,such as from int to float or from int to complex. Commented Apr 24, 2015 at 10:52

2 Answers 2

13

Either you find a way to get the output with the correct type or you use astype, see the docs, in order to change the type of an array

In your case the following example gives you an array of type np.int

c=random.randn(5,5).astype(np.int)
Sign up to request clarification or add additional context in comments.

5 Comments

Would this actually be more efficient? It performs the same steps, creating a float array then converting it to integer.
@jonrsharpe : I don't think so ... but you avoid copying the output into a new array by hand
@jonrsharpe: Even though the OP asked for "efficient", I suspect he doesn't really mean "efficient". You really think the cost of copying-and-casting a 5x5 NumPy array is a bottleneck in his program? Or that he finds manually printing out an array, copying and pasting and editing it is an inefficient way to code?
@abarnert frankly, I assumed that: 1. they meant what they said; and 2. this was just a small example to show the process. Who knows, though.
@jonrsharpe: I've gotten used to novices using the word "efficient" to mean… well, I'm never sure what, it's always vague, but "fast enough performance for my use case" or "algorithmically optimal performance" or anything remotely similar is rarely even close to whatever they're after…
2

Also: np.cast[int](c), which doesn't presume it's argument is a NumPy array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.