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

NumPy Notebook

NumPy, short for 'Numerical Python', is a powerful library for numerical computations in Python, primarily used for handling large, multi-dimensional arrays and matrices. It offers a wide range of mathematical functions, array creation methods, and operations such as element-wise calculations, statistical functions, and matrix manipulations. The document provides detailed examples of importing NumPy, creating arrays, performing operations, and saving/loading data.

Uploaded by

jdjtdl5849
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)
3 views8 pages

NumPy Notebook

NumPy, short for 'Numerical Python', is a powerful library for numerical computations in Python, primarily used for handling large, multi-dimensional arrays and matrices. It offers a wide range of mathematical functions, array creation methods, and operations such as element-wise calculations, statistical functions, and matrix manipulations. The document provides detailed examples of importing NumPy, creating arrays, performing operations, and saving/loading data.

Uploaded by

jdjtdl5849
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

NumPy

NumPy is used for working with arrays.

NumPy is short for "Numerical Python".

• A smart Excel sheet inside Python where formulas apply instantly to all cells

What can you do with it?

Add, subtract, multiply entire datasets Find average, sum, max, etc. Work with matrices Handle
images and signals

Core Functionality:

• N-dimensional Arrays (ndarrays): NumPy's primary strength lies in its ability to


handle large, multi-dimensional arrays and matrices. These ndarrays are
significantly more efficient than standard Python lists for numerical computations.
They allow for fast and efficient operations on large datasets.

• Mathematical Functions: NumPy provides a vast collection of mathematical


functions for operating on these arrays. This includes functions for: Linear algebra,
Fourier transforms, Random number generation, Statistical operation.

1: Importing NumPy
# Cell 1: Importing NumPy
import numpy as np

2: Creating NumPy Arrays - 1D array


# Cell 2: Creating NumPy Arrays - 1D array
arr1 = [Link]([1, 2, 3, 4, 5])
print("1D Array:")
print(arr1)

1D Array:
[1 2 3 4 5]

3: Creating NumPy Arrays - 2D array


# Cell 3: Creating NumPy Arrays - 2D array
arr2 = [Link]([[1, 2, 3], [4, 5, 6]])
print("\n2D Array:")
print(arr2)

2D Array:
[[1 2 3]
[4 5 6]]

4: Creating NumPy Arrays - specific data types


# Cell 4: Creating NumPy Arrays - specific data types
arr3 = [Link]([1, 2, 3], dtype='float64')
print("\nArray with float64:")
print(arr3)

Array with float64:


[1. 2. 3.]

5: Creating arrays with built-in functions - zeros


# Cell 5: Creating arrays with built-in functions - zeros
zeros_arr = [Link]((3, 4))
print("\nArray of zeros:")
print(zeros_arr)

Array of zeros:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

6: Creating arrays with built-in functions - ones


# Cell 6: Creating arrays with built-in functions - ones
ones_arr = [Link]((2, 3))
print("\nArray of ones:")
print(ones_arr)

Array of ones:
[[1. 1. 1.]
[1. 1. 1.]]
7: Creating arrays with built-in functions - range
# Cell 7: Creating arrays with built-in functions - range
range_arr = [Link](0, 10, 2) # Start, stop, step
print("\nArray with range:")
print(range_arr)

Array with range:


[0 2 4 6 8]

8: Creating arrays with built-in functions -


linspace
# Cell 8: Creating arrays with built-in functions - linspace
linspace_arr = [Link](0, 1, 5) # Start, stop, number of elements
print("\nArray with linspace:")
print(linspace_arr)

Array with linspace:


[0. 0.25 0.5 0.75 1. ]

9: Creating arrays with built-in functions -


random
# Cell 9: Creating arrays with built-in functions - random
random_arr = [Link](2, 2) # Random values between 0 and 1
print("\nRandom array:")
print(random_arr)

Random array:
[[0.46690943 0.35342964]
[0.99872944 0.46351998]]

10: Array attributes - shape


# Cell 10: Array attributes - shape
print("\nShape of arr2:", [Link])

Shape of arr2: (2, 3)


11: Array attributes - data type
# Cell 11: Array attributes - data type
print("Data type of arr3:", [Link])

Data type of arr3: float64

12: Array attributes - number of dimensions


# Cell 12: Array attributes - number of dimensions
print("Number of dimensions of arr2:", [Link])

Number of dimensions of arr2: 2

13: Array attributes - size


# Cell 13: Array attributes - size
print("Size of arr2:", [Link])

Size of arr2: 6

14: Reshaping arrays


# Cell 14: Reshaping arrays
reshaped_arr = [Link](5, 1)
print("\nReshaped array:")
print(reshaped_arr)

Reshaped array:
[[1]
[2]
[3]
[4]
[5]]

15: Array indexing and slicing - accessing


elements
# Cell 15: Array indexing and slicing - accessing elements
print("\nElement at index 0 of arr1:", arr1[0])
print("Element at row 1, column 2 of arr2:", arr2[1, 2])
Element at index 0 of arr1: 1
Element at row 1, column 2 of arr2: 6

16: Array indexing and slicing - slicing 1D array


# Cell 16: Array indexing and slicing - slicing 1D array
sliced_arr = arr1[1:4]
print("\nSliced array:", sliced_arr)

Sliced array: [2 3 4]

17: Array indexing and slicing - slicing 2D array


# Cell 17: Array indexing and slicing - slicing 2D array
sliced_2d = arr2[0:2, 1:3]
print("\nSliced 2D array:\n", sliced_2d)

Sliced 2D array:
[[2 3]
[5 6]]

18: Array indexing and slicing - boolean indexing


# Cell 18: Array indexing and slicing - boolean indexing
bool_arr = arr1 > 2
print("\nBoolean array:", bool_arr)
print("Elements greater than 2:", arr1[bool_arr])

Boolean array: [False False True True True]


Elements greater than 2: [3 4 5]

19: NumPy operations - element-wise


operations
# Cell 19: NumPy operations - element-wise operations
arr_a = [Link]([1, 2, 3])
arr_b = [Link]([4, 5, 6])
print("\nAddition:", arr_a + arr_b)
print("Subtraction:", arr_a - arr_b)
print("Multiplication:", arr_a * arr_b)
print("Division:", arr_a / arr_b)

Addition: [5 7 9]
Subtraction: [-3 -3 -3]
Multiplication: [ 4 10 18]
Division: [0.25 0.4 0.5 ]

20: NumPy operations - scalar operations


# Cell 20: NumPy operations - scalar operations
print("\nScalar addition:", arr_a + 10)
print("Scalar multiplication:", arr_a * 2)

Scalar addition: [11 12 13]


Scalar multiplication: [2 4 6]

21: NumPy functions - summation


# Cell 21: NumPy functions - summation
print("\nSum of arr1:", [Link](arr1))

Sum of arr1: 15

22: NumPy functions - mean


# Cell 22: NumPy functions - mean
print("Mean of arr1:", [Link](arr1))

Mean of arr1: 3.0

23: NumPy functions - standard deviation


# Cell 23: NumPy functions - standard deviation
print("Standard deviation of arr1:", [Link](arr1))

Standard deviation of arr1: 1.4142135623730951


24: NumPy functions - maximum and minimum
# Cell 24: NumPy functions - maximum and minimum
print("Maximum of arr1:", [Link](arr1))
print("Minimum of arr1:", [Link](arr1))

Maximum of arr1: 5
Minimum of arr1: 1

25: NumPy functions - dot product


# Cell 25: NumPy functions - dot product
dot_product = [Link](arr_a, arr_b)
print("\nDot product:", dot_product)

Dot product: 32

26: NumPy functions - matrix multiplication


# Cell 26: NumPy functions - matrix multiplication
matrix_a = [Link]([[1, 2], [3, 4]])
matrix_b = [Link]([[5, 6], [7, 8]])

matrix_product = [Link](matrix_a, matrix_b)


print("\nMatrix multiplication:\n", matrix_product)

Matrix multiplication:
[[19 22]
[43 50]]

27: NumPy functions - transpose


# Cell 27: NumPy functions - transpose
print("\nTranspose of matrix_a:\n", matrix_a.T)

Transpose of matrix_a:
[[1 3]
[2 4]]
28: Saving and loading arrays - save
# Cell 28: Saving and loading arrays - save
[Link]('my_array.npy', arr1)

29: Saving and loading arrays - load


# Cell 29: Saving and loading arrays - load
loaded_arr = [Link]('my_array.npy')
print("\nLoaded array:", loaded_arr)

Loaded array: [1 2 3 4 5]

30: Saving and loading arrays - savetxt


# Cell 30: Saving and loading arrays - savetxt
[Link]("my_array.txt",arr1,delimiter=",")

31: Saving and loading arrays - loadtxt


# Cell 31: Saving and loading arrays - loadtxt
loaded_txt_arr = [Link]("my_array.txt",delimiter=",")
print("\nLoaded array from text file:", loaded_txt_arr)

Loaded array from text file: [1. 2. 3. 4. 5.]

You might also like