0% found this document useful (0 votes)
9 views20 pages

Essential NumPy Operations Guide

Numpy programs

Uploaded by

sahityak66
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

Essential NumPy Operations Guide

Numpy programs

Uploaded by

sahityak66
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 1

**Question:** Create a 1D NumPy array with values from 0 to 9

**Code:**
import numpy as np
Arr = [Link]([0,1,2,3,4,5,6,7,8,9])
print(Arr)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Arr | ndarray | To store a 1D array of values from 0 to 9 |
Program 2

**Question:** Create a 3x3 matrix with all elements as 5

**Code:**
import numpy as np
Mat = [Link]((3, 3), 5)
print(Mat)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Mat | ndarray | To create a 3x3 matrix filled with 5s |
Program 3

**Question:** Generate a 5x5 identity matrix

**Code:**
import numpy as np
I = [Link](5)
print(I)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| I | ndarray | To generate a 5x5 identity matrix |
Program 4

**Question:** Create a 1D array of 10 evenly spaced values between 1 and 100

**Code:**
import numpy as np
Arr = [Link](1, 100, 10)
print(Arr)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Arr | ndarray | To store 10 evenly spaced values between 1 and 100 |
Program 5

**Question:** Generate a 4x4 matrix of random integers between 10 and 99

**Code:**
import numpy as np
RandMat = [Link](10, 100, size=(4,4))
print(RandMat)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| RandMat | ndarray | To generate a 4x4 matrix of random integers between
Program 6

**Question:** Reshape a 1D array of 12 elements into a 3x4 matrix

**Code:**
import numpy as np
Arr = [Link](12).reshape(3, 4)
print(Arr)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Arr | ndarray | To reshape a 1D array of 12 elements into a 3x4 matrix |
Program 7

**Question:** Slice and print the second row from a 3x3 matrix

**Code:**
import numpy as np
Mat = [Link]([[1,2,3],[4,5,6],[7,8,9]])
Row = Mat[1]
print(Row)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Row | ndarray | To store and print the second row of the matrix |
Program 8

**Question:** Transpose a given 2x5 matrix

**Code:**
import numpy as np
Mat = [Link]([[1,2,3,4,5],[6,7,8,9,10]])
T = Mat.T
print(T)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| T | ndarray | To transpose the 2x5 matrix |
Program 9

**Question:** Perform element-wise addition on two 3x3 matrices

**Code:**
import numpy as np
A = [Link]((3,3))
B = [Link]((3,3), 2)
C = A + B
print(C)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| C | ndarray | To store the result of element-wise addition |
Program 10

**Question:** Multiply each element of an array by 3 using broadcasting

**Code:**
import numpy as np
Arr = [Link]([1,2,3,4,5])
Result = Arr * 3
print(Result)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Result | ndarray | To store result of each element multiplied by 3 |
Program 11

**Question:** Find the maximum and minimum values in a random array of 15 elements

**Code:**
import numpy as np
Arr = [Link](0, 100, 15)
Max = [Link]()
Min = [Link]()
print('Max:', Max, 'Min:', Min)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Max, Min | int | To find maximum and minimum values |
Program 12

**Question:** Calculate the sum and mean of a 2D NumPy array

**Code:**
import numpy as np
Arr = [Link]([[1,2,3],[4,5,6]])
S = [Link]()
M = [Link]()
print('Sum:', S, 'Mean:', M)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| S, M | int, float | To calculate sum and mean of the array |
Program 13

**Question:** Replace all odd numbers in an array with -1

**Code:**
import numpy as np
Arr = [Link](10)
Arr[Arr % 2 == 1] = -1
print(Arr)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Arr | ndarray | To replace all odd numbers with -1 |
Program 14

**Question:** Extract all even numbers from a NumPy array

**Code:**
import numpy as np
Arr = [Link](20)
Even = Arr[Arr % 2 == 0]
print(Even)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Even | ndarray | To extract all even numbers |
Program 15

**Question:** Sort a NumPy array in ascending order

**Code:**
import numpy as np
Arr = [Link]([3, 1, 4, 2, 5])
[Link]()
print(Arr)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Arr | ndarray | To sort array in ascending order |
Program 16

**Question:** Create a 3x3 matrix and compute its determinant

**Code:**
import numpy as np
Mat = [Link]([[1,2,3],[0,1,4],[5,6,0]])
Det = [Link](Mat)
print('Determinant:', Det)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Det | float | To compute the determinant of the matrix |
Program 17

**Question:** Create two 1D arrays and find their dot product

**Code:**
import numpy as np
A = [Link]([1,2,3])
B = [Link]([4,5,6])
Dot = [Link](A,B)
print('Dot Product:', Dot)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Dot | int | To compute the dot product |
Program 18

**Question:** Flatten a 2D array into a 1D array

**Code:**
import numpy as np
Arr = [Link]([[1,2],[3,4]])
Flat = [Link]()
print(Flat)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| Flat | ndarray | To flatten a 2D array into 1D |
Program 19

**Question:** Stack two arrays vertically and horizontally

**Code:**
import numpy as np
A = [Link]([[1,2],[3,4]])
B = [Link]([[5,6],[7,8]])
V = [Link]((A,B))
H = [Link]((A,B))
print('Vertical Stack:\n', V)
print('Horizontal Stack:\n', H)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| V, H | ndarray | To vertically and horizontally stack arrays |
Program 20

**Question:** Find unique values and their counts in an array

**Code:**
import numpy as np
Arr = [Link]([1,2,2,3,3,3,4])
U, C = [Link](Arr, return_counts=True)
print('Unique:', U)
print('Counts:', C)

**Output:**

**Variable Description:**

| Name of the Variable | Data Type | Purpose |


|----------------------|-----------|---------|
| U, C | ndarray | To find unique values and their counts |

You might also like