Numpy is a large module in Python programming. It handles mainly numeric data analysis, and is also the basis of another large module, Pandas in Python. The heart of Numpy is data object type ndarray. Numpy array stores same type of data in an array. An array usually has dimension (how many axes, or rank), its data type (integer, float, string, or more), and shape (size for each axis in its rank). Numpy array can be created using array() function. Next code example show how to create simple one-dimensional array with Numpy in Python from list as input.
#import Numpy module
import numpy as np
#create a one-dimensional array, from a list of 5 items
a1 = np.array([7, 210, 23,53,99])
#show the array
a1
#output
array([ 7, 210, 23, 53, 99])
#type of array object
type(a1)
#output
numpy.ndarray
#type of data in the array
a1.dtype
#output
dtype('int32')
#dimension of array
a1.ndim
#output
1
#how many elements in total stored in the array
a1.size
#output
5
#size of each axis of the dimension
a1.shape
#output
(5,)
Numpy ndarray can be built with more than one dimension. The next example shows creation of a two-dimensional array,
#a two-dimensional array creation
a2 = np.array([[1, 3,5], [2, 4,6]])
#show the element type of array
a2.dtype
#output
dtype('int32')
#dimension of the array
a2.ndim
#output
2
#size, or total elements of the array
a2.size
6
#shape of the array
a2.shape
#output
(2, 3)
0 Comments