Learn NumPy in Detail

What is NumPy: NumPy is a core library for numerical computing in Python. NumPy is also useful in scientific computing, machine learning, and data science.


pip install numpy

import numpy as np

print(np.__version__)

arr = [1,2,3,4,5,6,7]
print(arr)
type(arr)

arr = [1,'c',2,2.2]
print(arr)
type(arr)

import numpy as np

x = np.array([1, 2, 3, 4]) #OK

x = np.array((1,2,3,4)) #OK

x = np.array([[1,2],[3,4]]) #OK

# x = np.array(1,2,3,4)# Not OK

x = np.array([[1,2],[3,4]])
type(x)

print(x.ndim)

x = np.array([0, 1, 2, 3], dtype = 'f')
print(type(x[0]))
print(x)

x = np.array([[0, 1],[2, 3]])
print(x.itemsize) 
print(x.size) 
print(x.shape)
print(x[0])
print(type(x[0][0]))
print(type(x))

print("Byte: ", x.itemsize*x.size)
print("%d bytes" %(x.itemsize*x.size)) 

array = np.arange(30, 40) #by default 1 step
print(array)

array = np.arange(30, 41, 2) #Jump by 2 steps 
print(array)

array = np.arange(1, 21).reshape(2,10,1)
print(array)

array_2D = np.identity(3)
print(array_2D)
print(array_2D.ndim)
print(array_2D.shape)
print(array_2D.size)

m = np.arange(10, 22).reshape((3, 4))
print(m) 

print(m.shape)

x = np.eye(6)
print(x)

x = np.ones((3, 4))
y = np.zeros((3, 4))
print(x)
print(y)

print(type(x[0][0]))

z = np.full((3,4), 5, dtype = 'f')
print(z)

# Checking if none of the elements in the array 'x' is zero
x = [1,1,1,2,2,2,0,4]
print(np.all(x))

# Compare two np arrays
x = np.array([3, 5])
y = np.array([2, 5])

print(np.greater(x, y))
print(np.greater_equal(x, y))
print(np.less(x, y))
print(np.less_equal(x, y)) 

# np.sum() and np.sort()
x = np.array([[0, 1], [2, 3]])

print(x)

print("Sum of all elements:")
print(np.sum(x))

print("Sum of each column:")
print(np.sum(x, axis=0))

print("Sum of each row:")
print(np.sum(x, axis=1)) 

x = np.array([[3, 2],[1, 0]])
print(x)
print(np.sort(x))

# np.multiply() and np.dot()
# Creating two NumPy arrays 'nums1' and 'nums2' containing 2x3 matrices
nums1 = np.array([[2, 5, 2],[1, 5, 5]])

nums2 = np.array([[5, 3, 4],[3, 2, 5]])

print("Array1:") 
print(nums1)

print("Array2:") 
print(nums2)

print("\nMultiply arrays of same size element-by-element:")
print(np.multiply(nums1, nums2)) 

nums1 = np.array([[2, 5, 2],
              [1, 5, 5]])

nums2 = np.array([[5, 3],
              [3, 2],[1,2]])

print(np.dot(nums1, nums2))

import matplotlib.pyplot as plt

binary_image = np.array([[0, 1, 0, 1, 0, 1, 0, 1],
			[1, 0, 1, 0, 1, 0, 1, 0],
       		        [0, 1, 0, 1, 0, 1, 0, 1],
			[1, 0, 1, 0, 1, 0, 1, 0],
			[0, 1, 0, 1, 0, 1, 0, 1],
			[1, 0, 1, 0, 1, 0, 1, 0],
			[0, 1, 0, 1, 0, 1, 0, 1]])
print(binary_image)

plt.imshow(binary_image, cmap='gray')
plt.title('Binary Image')
plt.axis('off')  # Hide axes

plt.savefig("Data/" + "BinaryImage.png") #it should come before plt.show()
plt.show()


# From an image to an array
from PIL import Image
image = Image.open('Data/' + 'BinaryImage.png')
image_array = np.array(image)

plt.imshow(image_array, cmap='gray')
plt.axis('off')  # Hide axes
plt.show()


Leave a Reply

Your email address will not be published. Required fields are marked *