10 1-Numpy
10 1-Numpy
ipynb - Colab
keyboard_arrow_down Numpy
NumPy is a fundamental library for scientific computing in Python. It provides support for arrays and matrices, along with a collection of
mathematical functions to operate on these data structures. In this lesson, we will cover the basics of NumPy, focusing on arrays and
vectorized operations.
import numpy as np
[1 2 3 4 5]
<class 'numpy.ndarray'>
(5,)
## 1 d array
arr2=np.array([1,2,3,4,5])
arr2.reshape(1,5) ##1 row and 5 columns
array([[1, 2, 3, 4, 5]])
arr2=np.array([[1,2,3,4,5]])
arr2.shape
(1, 5)
## 2d array
arr2=np.array([[1,2,3,4,5],[2,3,4,5,6]])
print(arr2)
print(arr2.shape)
[[1 2 3 4 5]
[2 3 4 5 6]]
(2, 5)
np.arange(0,10,2).reshape(5,1)
array([[0],
[2],
[4],
[6],
[8]])
np.ones((3,4))
## identity matrix
np.eye(3)
https://colab.research.google.com/drive/1AA1Q9kcv9LO38_SNw9xX8JJfswMFCsjA#scrollTo=3c6il90RcPg1&printMode=true 1/4
7/18/24, 11:05 AM 10.1-numpy.ipynb - Colab
## Attributes of Numpy Array
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Array:\n", arr)
print("Shape:", arr.shape) # Output: (2, 3)
print("Number of dimensions:", arr.ndim) # Output: 2
print("Size (number of elements):", arr.size) # Output: 6
print("Data type:", arr.dtype) # Output: int32 (may vary based on platform)
print("Item size (in bytes):", arr.itemsize) # Output: 8 (may vary based on platform)
Array:
[[1 2 3]
[4 5 6]]
Shape: (2, 3)
Number of dimensions: 2
Size (number of elements): 6
Data type: int32
Item size (in bytes): 4
# Element-wise multiplication
print("Multiplication:", arr1 * arr2)
# Element-wise division
print("Division:", arr1 / arr2)
## Universal Function
arr=np.array([2,3,4,5,6])
## square root
print(np.sqrt(arr))
## Exponential
print(np.exp(arr))
## Sine
print(np.sin(arr))
## natural log
print(np.log(arr))
arr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print("Array : \n", arr)
Array :
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
print(arr[1:,1:3])
[[ 6 7]
[10 11]]
https://colab.research.google.com/drive/1AA1Q9kcv9LO38_SNw9xX8JJfswMFCsjA#scrollTo=3c6il90RcPg1&printMode=true 2/4
7/18/24, 11:05 AM 10.1-numpy.ipynb - Colab
print(arr[0][0])
print(arr[0:2,2:])
1
[[3 4]
[7 8]]
arr[1:,2:]
array([[ 7, 8],
[11, 12]])
[[100 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
arr[1:]=100
print(arr)
[[100 2 3 4]
[100 100 100 100]
[100 100 100 100]]
# Mean
mean = np.mean(data)
print("Mean:", mean)
# Median
median = np.median(data)
print("Median:", median)
# Standard deviation
std_dev = np.std(data)
print("Standard Deviation:", std_dev)
# Variance
variance = np.var(data)
print("Variance:", variance)
Mean: 5.5
Median: 5.5
Standard Deviation: 2.8722813232690143
Variance: 8.25
## Logical operation
data=np.array([1,2,3,4,5,6,7,8,9,10])
https://colab.research.google.com/drive/1AA1Q9kcv9LO38_SNw9xX8JJfswMFCsjA#scrollTo=3c6il90RcPg1&printMode=true 3/4
7/18/24, 11:05 AM 10.1-numpy.ipynb - Colab
array([5, 6, 7, 8])
https://colab.research.google.com/drive/1AA1Q9kcv9LO38_SNw9xX8JJfswMFCsjA#scrollTo=3c6il90RcPg1&printMode=true 4/4