[go: up one dir, main page]

0% found this document useful (0 votes)
17 views8 pages

Numpy Notes

NumPy is an open-source Python library for numerical computing, providing support for large multidimensional arrays and a variety of mathematical functions. It is efficient in memory storage and performance, making it foundational for data science libraries like Pandas and Scikit-learn. The document covers installation, array creation, broadcasting, mathematical functions, and various array operations with examples.

Uploaded by

kishor.karnati82
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)
17 views8 pages

Numpy Notes

NumPy is an open-source Python library for numerical computing, providing support for large multidimensional arrays and a variety of mathematical functions. It is efficient in memory storage and performance, making it foundational for data science libraries like Pandas and Scikit-learn. The document covers installation, array creation, broadcasting, mathematical functions, and various array operations with examples.

Uploaded by

kishor.karnati82
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
You are on page 1/ 8

8/4/25, 3:38 PM NUmpy

What is NumPy?
NumPy (Numerical Python) is an open-source Python library used for numerical computing.
It provides support for large multidimensional arrays and matrices, along with a collection of
high-level mathematical functions to operate on these arrays.

📦 Installation
!pip install numpy

💡 Why Use NumPy?


1.Efficient memory storage and performance

2.Broad range of mathematical, logical, shape manipulation, sorting, selecting, I/O, and linear
algebra functions

3.Foundation for most Python data science libraries (e.g., Pandas, Scikit-learn)

✨ NumPy Basics
*** 1. Creating Arrays:

=> From List You can convert to the array: np.array([1,2,3])

=> Range Array np.arange(0,10,2)

=> 3X3 zero Matrix np.zeros((3, 3))

=> 2x2 Ones matrix np.ones((2, 2))

=> Identity matrix np.eye(3)

=> Evenly spaced numbers np.linspace(0, 1, 5) ***

🔁 Broadcasting
** Broadcasting is a technique that allows NumPy to perform arithmetic operations on arrays
of different shapes.**

file:///C:/Users/phars/Downloads/_NUmpy.html 1/8
8/4/25, 3:38 PM NUmpy

🧮 Mathematical Functions
** np.mean(arr) --- Mean

np.median(arr) ---Median

np.std(arr) ---Standard deviation

np.sum(arr) ---Sum

np.min(arr) ---Min value

np.max(arr) ---Max value

np.sqrt(arr) ---Square root

np.exp(arr) ---Exponential***

⚙️ Array Operations
** Stack vertically :

np.concatenate([a,b], axis=0)

*Vertical stacking:

np.vstack((a, b))

*Horizontal stacking:

np.hstack((a, b))

*Unique elements:

np.unique(arr)

*Sort array:

np.sort(arr)

*. Replacing Odd Values


arr[arr % 2 == 1] = -1

*. Normalize Array
file:///C:/Users/phars/Downloads/_NUmpy.html 2/8
8/4/25, 3:38 PM NUmpy

(arr - arr.min()) / (arr.max() - arr.min())

*. NaN Handling
np.nan_to_num(arr)

*.Intersections
np.intersect1d(arr1, arr2)

*.Random Arrays
np.random.rand(3, 3)
np.random.randint(1, 10, (3, 3))

In [ ]:

🧪 Master NumPy: Frequently Asked


Questions and Examples
In [15]: import numpy as np
a = ([1,2,3,4])
b = np.array(a)
max_number = np.max(b)
print(max_number)

In [17]: b

Out[17]: array([1, 2, 3, 4])

In [29]: np.arange(0, 20, 3) # [0 2 4 6 8]

Out[29]: array([ 0, 3, 6, 9, 12, 15, 18])

In [33]: np.linspace(0,1, 3)

Out[33]: array([0. , 0.5, 1. ])

In [49]: np.linspace(0, 1, 2, dtype=int)

Out[49]: array([0, 1])

file:///C:/Users/phars/Downloads/_NUmpy.html 3/8
8/4/25, 3:38 PM NUmpy

Que:1. Create a 1D NumPy array of


numbers from 0 to 9
In [57]: #Que:1. Create a 1D NumPy array of numbers from 0 to 9
import numpy as np
arr = np.arange(10)
print(arr)

[0 1 2 3 4 5 6 7 8 9]

2. Create a 3×3 array filled with zeros

In [61]: #que 2: 2. Create a 3×3 array filled with zeros


arr = np.zeros((3,3))
print(arr)

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

3. Create a 3×3 identity matrix

In [63]: #que 3: Create a 3×3 identity matrix


arr = np.eye(3)
print(arr)

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

4. Create a 5×5 matrix with values from 1 to 25

In [67]: #Que 4:Create a 5×5 matrix with values from 1 to 25


arr = np.arange(1,26).reshape(5,5)
print(arr)

[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]
[21 22 23 24 25]]

5. Convert a list to a NumPy array

In [71]: #Que 5: Convert a list to a NumPy array


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

[1 2 3 4 5]

file:///C:/Users/phars/Downloads/_NUmpy.html 4/8
8/4/25, 3:38 PM NUmpy

6. Replace all odd numbers in a NumPy array with -1

In [83]: #Que 6: Replace all odd numbers in a NumPy array with -1


arr = np.arange(10)
arr[arr % 2 == 1] = -1
print(arr)

[ 0 -1 2 -1 4 -1 6 -1 8 -1]

7. Reshape a 1D array to a 2D array of shape (2, 5)

In [85]: #Que 7:Reshape a 1D array to a 2D array of shape (2, 5)


arr = np.arange(10).reshape(2,5)
print(arr)

[[0 1 2 3 4]
[5 6 7 8 9]]

8. Find the maximum and minimum of a NumPy array

In [97]: #Que 8:Find the maximum and minimum of a NumPy array


arr = np.array([2,3,5,7])
min_num = np.min(arr)
max_num = np.max(arr)
print("min_num:", min_num)
print("max_num:" , max_num)

min_num: 2
max_num: 7

9. Normalize a NumPy array (scale between 0 and 1)

In [100… #Que 9 :Normalize a NumPy array (scale between 0 and 1)


arr = np.array([1,2,3,4,5])
normalized = (arr - np.min(arr)) / (np.max(arr) - np.min(arr))
print(normalized)

[0. 0.25 0.5 0.75 1. ]

10. Get the common elements between two NumPy arrays

In [104… #Que 10:Get the common elements between two NumPy arrays
a = np.array([1,2,3,4])
b = np.array([3,4,5,6])
print(np.intersect1d(a, b))

[3 4]

11. Create a 10x10 array with random values and find the mean of each row

file:///C:/Users/phars/Downloads/_NUmpy.html 5/8
8/4/25, 3:38 PM NUmpy

In [118… #Que 11:Create a 10x10 array with random values and find the mean of each row
arr = np.random.rand(10 , 10)
row_mean = arr.mean(axis =1)
print(row_mean)

[0.42406955 0.4810178 0.45424484 0.50222871 0.41546408 0.66914788


0.3661636 0.55412753 0.30162397 0.55905859]

In [116… #Que : write a code using numpy function to multiply both array.
a = np.array([2,3,4])
b = np.array([2,3,4])
print(np.dot(a,b))

29

12. Reverse a NumPy array

In [120… #Que 12: Reverse a NumPy array


arr = np.array([1,2,3,4])
print(arr[::-1])

[4 3 2 1]

13. Find the position of the maximum element in a 2D array

In [122… #Que 13:Find the position of the maximum element in a 2D array


arr = np.random.randint(1,100 ,(3,3))
print(np.argmax(arr))
print(np.unravel_index(np.argmax(arr),arr.shape))

7
(np.int64(2), np.int64(1))

14. Replace NaN values with zero

In [124… #Que 14:Replace NaN values with zero


arr = np.array([1,2,np.nan , 4])
arr = np.nan_to_num(arr)
print(arr)

[1. 2. 0. 4.]

15. Flatten a multi-dimensional array

In [126… #Que 15:Flatten a multi-dimensional array


arr = np.array([[1,2] , [3,4]])
flat = arr.flatten()
print(flat)

[1 2 3 4]

16. Create a checkerboard pattern (8x8)

file:///C:/Users/phars/Downloads/_NUmpy.html 6/8
8/4/25, 3:38 PM NUmpy

In [128… #Que 16:Create a checkerboard pattern (8x8)


arr = np.zeros((8,8))
arr[1::2 , ::2] = 1
arr[::2 , 1::2] = 1
print(arr)

[[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.]
[1. 0. 1. 0. 1. 0. 1. 0.]]

17. Create a 100x100 matrix and compute the sum of all values

In [130… #Que 17:Create a 100x100 matrix and compute the sum of all values
arr = np.random.rand(100 , 100)
total = np.sum(arr)
print(total)

4974.671920129873

18. Extract all numbers between 30 and 70 in a NumPy array

In [132… #Que 18: Extract all numbers between 30 and 70 in a NumPy array
arr = np.arange(100)
print(arr[(arr > 30) & (arr <70)])

[31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69]

19. Convert a float array to integer without roundin

In [134… #Que 19:Convert a float array to integer without roundin


arr = np.array([1.5 , 2.3 , 3.9])
print(arr.astype(int))

[1 2 3]

20. Stack two arrays vertically and horizontally

In [136… #Que 20:Stack two arrays vertically and horizontally


a = np.array([[1,2] , [3,4]])
b = np.array([[5,6] , [7,8]])
print(np.vstack((a , b)))
print(np.hstack((a,b)))

file:///C:/Users/phars/Downloads/_NUmpy.html 7/8
8/4/25, 3:38 PM NUmpy

[[1 2]
[3 4]
[5 6]
[7 8]]
[[1 2 5 6]
[3 4 7 8]]

In [ ]:

file:///C:/Users/phars/Downloads/_NUmpy.html 8/8

You might also like