which float precision are numpy arrays by default? - numpy

I wonder which format floats are in numpy array by default.
(or do they even get converted when declaring a np.array? if so how about python lists?)
e.g. float16,float32 or float64?

float64. You can check it like
>>> np.array([1, 2]).dtype
dtype('int64')
>>> np.array([1., 2]).dtype
dtype('float64')

If you dont specify the data type when you create the array then numpy will infer the type, from the docs
dtypedata-type, optional - The desired data-type for the array. If not given, then the type will be determined as the minimum type
required to hold the objects in the sequence

Related

How to declare a pandas dtype constant

I need to synthesize an array of pandas datatypes (due to situational handling of empty/null columns). How can I create a pandas.dtype constant for example on
pd.dtype('int64')
Can it be a categorical dtype? If so then you can do it with Pandas.CategoricalDtype. See https://pandas.pydata.org/docs/reference/api/pandas.CategoricalDtype.html.

Why are numpy array called homogeneous?

Why are numpy arrays called homogeneous when you can have elements of different type in the same numpy array like this?
np.array([1,2,3,4,"a"])
I understand that I cannot perform some types of broadcasting operations like I cannot perform
np1*4 here and it results in an error.
but my question really is when it can have elements of different types, why it is called homogeneous?
Numpy automatically converts them to most applicable datatype.
e.g.,
>>> np.array([1,2,3,4,"a"]).dtype.type
numpy.str_
In short this means all elements are of string.
>>> np.array([1,2,3,4]).dtype.type
numpy.int64

Related Numpy array typcasting

I have the below python code:
a = np.array([1, 2, '3'])
print(a)
output:
['1' '2' '3']
My question is, why all elements are converted into strings?
I know that in numpy array, if the array consist of different elements it will be typecasted. But on what basis it will be typecasted?
This is fairly well explained in the numpy.array documentation (highlighting is mine):
numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
[…]
dtype: data-type, optional
The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence.
An integer can always be converted to string, the other way around it not always possible (e.g., a cannot be converted to string).
This is the same if you mix floats and integers, the array will be casted as float.

Only size 1 arrays can be converted to python scalars

I created a 3 dimensional object using numpy.random module such as
import numpy as np
b = np.random.randn(4,4,3)
Why can't we cast type float to b?
TypeError
actual code
You can't float(b) because b isn't a number, it's a multidimensional array/matrix. If you're trying to convert every element to a Python float, that's a bad idea because numpy numbers are more precise, but if you really want to do that for whatever reason, you can do b.tolist(), which returns a Python list of floats. However, I don't believe you can have a numpy matrix of native Python types because that doesn't make any sense.

array passing between numpy and cython

I would like to pass an numpy array to cython. The Cython C type should be float. Which numpy type do I have to choose. When I choose float or np.float, then its actually a C double.
You want np.float32. This is a 32-bit C float.