Numpy Intro.

Posted under » Python Data Analysis on 14 June 2023

NumPy, which stands for Numerical Python, is a library consisting of multidimensional array objects and a collection of routines for processing those arrays. An array is a central data structure of the NumPy library. An array is a grid of values and it contains information about the raw data, how to locate an element, and how to interpret an element.

When you install Pandas, it will install Numpy too. I see Pandas as something like excel, and Numpy more like matrix multi dimensional data.

The most important object defined in NumPy is an N-dimensional array type called ndarray.

It has a grid of elements that can be indexed in various ways. The elements are all of the same type, referred to as the array dtype.

An array can be indexed by a tuple of nonnegative integers, by booleans, by another array, or by integers.

The rank of the array is the number of dimensions. The shape of the array is a tuple of integers giving the size of the array along each dimension.

One way we can initialize NumPy arrays is from Python lists, using nested lists for two- or higher-dimensional data. In NumPy, dimensions are called axes.

import numpy as np 
# 1D
a = np.array([1,2,3]) 
print (a)

# 2D or 2 axes
b = np.array([[1, 2], [3, 4]]) 
print (b)

We can access the elements in the array using square brackets. When you’re accessing elements, remember that indexing in NumPy starts at 0. That means that if you want to access the first element in your array, you’ll be accessing element “0”.

a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a[0])

[1 2 3 4]

Any item extracted from ndarray object (by slicing) is represented by a Python object of a type. The following diagram shows a relationship between ndarray, data type object (dtype) and array scalar type

NumPy supports a greater variety of numerical types. Eg scalar types.

They are bool_, int_, intc, intp, int8, int16, int32, int64, uint8, uint16, uint32, uint64, float_, float16, float32, float64, complex_, complex64 and complex128. While the default data type is floating point (np.float64), you can explicitly specify which data type you want using the dtype keyword.

The following examples define a structured data type called student with a string field 'name', an integer field 'age' and a float field 'marks'. This dtype is applied to ndarray object.

import numpy as np 
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) 
print (student)

[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')]

Numpy Arrays

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data