# How to include numpy library to your python programs.
import numpy as np
# check the numpy package/library version
print(np.__version__)
1.25.0
# defining a array in numpy
myarray = np.array(["a", "b","c","d"])
print(myarray)
# To check the type of data
print(type(myarray))
# Here ndarray mean n dimensional array
# Using 2 dimensional array sample
my2darray = np.array([["a", "b","c","d"], [1,2,3,4]])
print(my2darray)
print(type(my2darray))
# minimum dimensions , with ndmin we can defining minimum dimensions
that is required for ndarray.
myndminArr = np.array([["a", "b","c","d"], [1,2,3,4]], ndmin = 3)
print(myndminArr)
print(type(myndminArr))
# Lets see how to initialize arrays with some automatic default values
zeroarr = np.zeros((3,4)) #Create an array of zeros
onearr = np.ones((2,3,4),dtype=np.int16) #Create an array of ones
evenspaced = np.arange(10,25,5)#Create an array of evenly spaced
values (step value)
linespaced = np.linspace(0,2,12) #Create an array of evenlyspaced
values (number of elements)
constarr = np.full((2,2),7)#Create a constant array
# for above, (2,2) is the 2 dimension with 2 elements each and 7 is
constant value
matrixarr = np.eye(2) #Create a 2X2 identity matrix
emptyarr = np.empty((3,2)) #Create an empty array
print('zero array' , zeroarr)
print('array of ones' , onearr)
print('evenly spaced values array' , evenspaced)
print('evenly spaced values array with number of elements' ,
linespaced)
print("array of constants", constarr)
print("matrix array", matrixarr)
print("empty array", emptyarr)
# lets check the number of dimensions
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
# dtype parameter here we look at datatype. What kind of data type the
array elements should be
mydtypearr = np.array([1, 2, 3], dtype = 'S')
print(mydtypearr)
mydtypearrf = np.array([1, 2, 3], dtype = 'f')
print(mydtypearr)
arr = np.array([1,4,15,23], dtype='i8')
print(arr)
# use of astype functions to convert array from one data type to
another
# The astype() function creates a copy of the array, and allows you to
specify the data type as a parameter.
arr = np.array([1,4,15,23,3424324234], dtype='i8')
print(arr.dtype)
print(arr)
newarr = arr.astype('i4')
print(newarr.dtype)
print(newarr)
# size gives the total count of elements in the array
arr = np.array([[[1,3,4,5],[32,1,34,2]],[[6,6,7,3],[2,6,2,3]],
[[14,234,493,123],[342,124,394,184]]])
print(arr.size)
# len gives the total count of elements in the the outermost dimension
of the array
arr = np.array([[[1,3,4,5],[32,1,34,2],[4,2,1,0]],
[[6,6,7,3],[2,6,2,3],[8,9,3,1]],
[[6,6,7,3],[2,7,2,3],[8,9,3,0]],
[[6,4,7,1],[2,2,2,3],[8,9,3,3]],
[[6,7,7,2],[2,4,2,3],[8,9,3,2]]
])
print("length of arr:",len(arr))
arr1 = np.array([[[1,3,4,5],[32,1,34,2]],[[6,6,7,3],[2,6,2,3]],
[[14,234,493,123],[342,124,394,184]]])
print("length of arr1:", len(arr1))
length of arr: 5
length of arr1: 3
# # NumPy arrays have an attribute called shape that returns a
# tuple with each element of tuple having the length of that
dimension.
arr = np.array([[[1,3,4,5],[32,1,34,2],[6,0,7,3]],
[[6,6,7,3],[2,6,2,3],[2,6,2,1]]])
# here if you see, outermost dimension has 2 elements,
# the 2nd dimension has 3 elements and inner most dimension is 4
elements
print(arr.shape)
(2, 3, 4)
# Reshaping the array to 2-D, 3-D
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(2, 3, 2)
print(newarr)
# Note reshape(-1) and flatten helps to bring any n dimension array to
1 dimension array
y = newarr.reshape(-1) # reshapes to flat 1 D
print(y)
x = newarr.flatten()
print(x)
# Transpose of an array using .transpose() or .T
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a)
b = a.transpose()
print(b)
print(b.T)
# The copy method creates another copy of the data in memory and
# any changes made to the copy will not affect original array,
# as well as any changes made to the original array will not affect
the copy.
arr = np.array([1,2,8,24])
x = arr.copy()
x[0] = 23
print('Copied array x:' , x)
print('Original array arr:', arr)
arr[1] = 12
print('Copied array x:' , x)
print('Original array arr:', arr)
# The view does not own the data and any changes made to the view will
affect the original array,
# and any changes made to the original array will affect the view.
# using view method to point to the original array
y = arr.view()
y[3] = 14
print('viewed array y:' , y)
print('Original array arr:', arr)
arr[1] = 12
print('viewed array y:' , y)
print('Original array arr:', arr)
print("Base of the copied x array",x.base)
print("Base of the viewed y array",y.base)
#Every NumPy array has the attribute base that returns None if the
array owns the data.
np.random.random((2,2)) #Create an array with random values
# Random numbers that are generated algorithm based are psuedo random
numbers.
from numpy import random as npr
x = npr.randint(60) # parameter indicates a number to be generated
between 1 to 60
y = npr.rand() # FLOAT number between 0 and 1.
print(x)
print(y)
import numpy.random as npr
x = npr.randint(40, size=(3)) # size indicates number of random
numbers, returns an array
y = npr.rand(3) # parameter is the size indicating number of random
numbers returns an array
print(x)
print(y)
a = npr.randint(40, size = (2,3)) # parameter indicates a number to
be generated between 1 to 40
b = npr.rand(4,2) # FLOAT number between 0 and 1.parameter is the
size for 2D array
print(a)
print(b)
# use choice() for generating random number from an array of int
# power of python to use a function to generate an array and then pick
random from the same.
from numpy import random as npr
a = npr.choice((npr.randint(10000,size=20)),size=(6,3))
print(a)