Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Numpy infers dtype for array

Can anyone please help me to understand that from where does Numpy's array function infers data type.

I understand it basically infers from the kind of value that has been assigned to the array.

For Example:

> data = [1,2,3,4]
> arr = np.array(data)

So in the above lines the "arr" will have the dtype('int64') or dtype('int32').

What I am trying to understand is how does it decides whether to give it a int64 or a int32?

I understand that it might be a trivial question but I am just trying to understand that how does it work as I was recently asked this in an interview.

like image 601
bshah Avatar asked Mar 15 '23 03:03

bshah


1 Answers

Numeric data types include integers and floats.

If we have an array that contains both integers and floating point numbers, numpy will assign the entire array to the float data type so the decimal points are not lost.

An integer will never have a decimal point. So for example, 2.55 would be stored as 2

As mentioned by @unutbu int32 and int64 depends on the type of bit-machines you have, whether it is a 32 bit-machine or a 64 bit-machine

Strings, are values that contain numbers and/or characters. For example, a string might be a word, a sentence, or several sentences. The most general dtype=string will be assigned to your array if your array has mixed types (numbers and strings).

To have a complete detailed look, you can have a look at this website of scipy docs

like image 159
Srivatsan Avatar answered Mar 17 '23 16:03

Srivatsan