Day 1: Numpy in Nutshell (Part I)

·

2 min read

Welcome and congrats upon joining this journey to learn machine learning !!

NumPy is like your best buddy when you're dealing with lots of numbers in Python. It helps you do all sorts of cool stuff with them, from basic math to fancy linear algebra and statistics. So, whether you're crunching data, tinkering with machine learning, or just playing around with numbers, NumPy's got your back!

What you will learn today:

  • Creating one and two dimensional array in Numpy

  • Creating Numpy array of zeroes

  • Creating Numpy array of ones

  • Creating array of any number

  • Creating identity matrix

1] Creating one dimensional array in Numpy :

Step 1 : Import Numpy Library

import numpy as np

This command imports Numpy library as np . This mean instead of writing complete spelling of Numpy we can write as np

Step 2:

one_dimensional_array = np.array([1,2,3,4,5])
print(one_dimensional_array)

Output we get is :

[1 2 3 4 5]

A one-dimensional array is like a list of items, where each item has a unique number called an index. This list is stored in a single line, and you can access the items by using their index.

2] Creating Numpy array of zeroes :

Step 1 : Import Numpy Library

Step 2 :

null_matrix = np.zeros((4,5))
print(null_matrix)

-> np.zeros((4,5)) creates a 4 X 5 matrix with all elements 0

Output we get :

[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]

3] Creating Numpy array of ones

Step 1 : Import Numpy Library

Step 2 :

matrix_with_ones = np.ones((3,3))
print(matrix_with_ones)

-> The command np.ones((3,3)) creates a matrix of order 3X3 with all elements as 1

Output we get:

[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]

4]Creating array of any number

Step 1 : Import Numpy Library

Step 2 :

matrix_with_5 = np.full((5,4),5)
print(matrix_with_5)

->The command np.full((5,4),5) creates a matrix of order 5X4 with all values as 5

Output we get:

[[5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]]

5]Creating identity matrix:

Step 1 : Import Numpy Library

Step 2 :

identity_matrix = np.eye(5)
print(identity_matrix)

->The command np.eye(5) create a identity matrix of order 5X5

Output we get:

[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]

STAY TUNED !!

Did you find this article valuable?

Support Learn By Reading by becoming a sponsor. Any amount is appreciated!