1. Creating and Accessing NumPy Arrays

# numpy arrays import and create array
import numpy as np
a = np.array([1,2,3])
# type of obect
print(type(a))
#access array
print(a[0],a[1],a[2], sep=",")
#check shape
print(a.shape)
# change value
a[0] = 5
print(a)
#create 2d array and check type
b = np.array([[1,2,3],[4,5,6]])
print(type(b))
# check shape
print(b.shape)
# access array elements of ndarray
print(b[0,0],b[1,0], sep=",")
#complete array
print(b)
# numpy function to create array
c = np.zeros((2,2))
print(c)

d = np.ones((2,2))
print(d)

e = np.full((2,2),7)
print(e)

f = np.eye(3) #identity matrix
print(f)

f = np.random.random((2,2))
print(f)

g = np.random.randint((1,1,1,1,1,1),500)
print(g)
#Creating Arrays from Sub-classes
h = np.array(np.mat('1 2; 3 4'), subok = False)
print(h)
i = np.array(np.mat('1 2; 3 4'), subok = True)
print(i)
# Array indexing
j = np.arange(10)
print(j)
print(j[2:6])
k = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(k)
print(k[3:])
print(k[:2])
print(k[::2])
l = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(l[...,0])
print(l[...,1:])
print(l[1,...])
print(l[1:,...])
#integer indexing
m=np.array([[1,2],[3,4],[5,6]])
n=m[[0,1,2],[0,1,0]]
print(n)
#Boolean Indexing
a = np.array([[0,1,2],[3,4,5],[6,7,8],[9,10,11]])
print(a[a>5])

Post a Comment

0 Comments