Visit Flyai. club to create your AI project with one click

Basic Introduction to Python Numpy Ndarray 1

The data type of ndarray

Dtype (data type) is a special object that contains the information required by NDARRay to interpret a block of memory as a particular data type

Dtype is one of the sources of NumPy’s flexibility in interacting with other systems. In most cases, they map directly to the corresponding machine representation, making it easier to “read and write binary data streams on disk” and “integrate low-level language code (such as C, Fortran).” Numeric dTypes are named the same way: a type name (such as float or int) followed by a number to indicate the bit length of each element. Standard double-precision floating-point values (float objects in Python) take up 8 bytes (64-bit). Therefore, this type is called float64 in NumPy. Table 4-2 lists all the data types supported by NumPy.

It doesn’t matter if you can’t remember these NumPy dtypes, especially for novices. It’s usually just a matter of knowing whether the rough type of data you’re dealing with is floating point, complex, integer, Boolean, string, or plain Old Python objects. When you need to control how data is stored in memory and on disk (especially for large data sets), you need to know how to control the type of storage.

An array can be explicitly converted from one dType to another via ndarray’s Astype method:

Integers are converted to floating point numbers. If a floating point number is converted to an integer, the decimal part will be truncated and deleted:

If an array of strings represents all numbers, astype can also be used to convert it to numeric form:

Note: Be careful when using numpy.string_ because numpy string data is of fixed size and will not warn when interception occurs. Pandas provides more convenient ways to handle non-numerical data. If the conversion fails for any reason (such as a string that cannot be converted to FLOAT64), a ValueError is raised.

Numpy array operations

Arrays are important because they allow you to perform batch operations on data without writing loops. NumPy users call this vectorization. Any arithmetic operation between arrays of equal size applies the operation to the element level:

Comparisons between arrays of the same size produce booleans:

The operation between arrays of different sizes is called broadcasting

Beginner tutorials on Jupyter Notebook – The basics of mesmerizing operations

Basic Tutorials on Jupyter Notebook – Use the shortcut keys

Basic introduction to Numpy ndarray for Python 1

Click on theRead the original, hands-on operation

— the End —