Numpy Basic
Create a Numpy Array
import numpy as np
# create numpy array
a = np.array([2,3,4])
print ("***** a *****")
print ("value", a) # print array
print ("data type", a.dtype) # data type of array
print ("shape", a.shape) # size of array in each dimension(m, n)
print ("size", a.size) # total number of elements of the array
print ("itemsize", a.itemsize) # the size in bytes of each element of the array
print ("ndim", a.ndim)
print ("data", a.data) # buffer containing the actual elements of the array
print ("***** b *****") # number of axes (dimensions) of the array.
b = np.array([[1.2, 3.5, 5.1], [1.2, 3.5, 5.1]])
print ("value", b)
print ("data type", b.dtype)
print ("shape", b.shape)
print ("size", b.size)
print ("itemsize", b.itemsize)
print ("ndim", b.ndim)
print ("data", b.data)
Create Numpy Array of size (M, N)
# create numpy array of shape (3, 4) and all element are 0
arr = np.zeros((3,4))
print (arr)
Numpy Arrange (np.arrange)
# create number starting between 10 to 30 with difference 5
arr = np.arange(10, 30, 5)
print (arr) # 10, 15, 20, 25
# create 1d array
a = np.arange(6)
print (a) # create array 0, 1, 2, 3, 4, 5
# 2d array
b = np.arange(12).reshape(4,3)
print (b)
Basic Operator
a = np.array( [20,30,40,50])
b = np.arange(4) # [0, 1, 2, 3]
c = a - b # [20-0, 30-1, 40-2, 50-3] = [20, 29, 38, 47]
print (c) # add element of same index
c = a*b # multiply element of same index
print (c)
# matricx dot product
c = a.dot(b)
print (c) # matrix multiplication
</code></pre>
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Comments
Post a Comment