0% found this document useful (0 votes)
4 views

10 1-Numpy

Uploaded by

AB
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

10 1-Numpy

Uploaded by

AB
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

7/18/24, 11:05 AM 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.

!pip install numpy

Requirement already satisfied: numpy in e:\udemy final\python\venv\lib\site-packages (1.26.4)

import numpy as np

## create array using numpy


##create a 1D array
arr1=np.array([1,2,3,4,5])
print(arr1)
print(type(arr1))
print(arr1.shape)

[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))

array([[1., 1., 1., 1.],


[1., 1., 1., 1.],
[1., 1., 1., 1.]])

## identity matrix
np.eye(3)

array([[1., 0., 0.],


[0., 1., 0.],
[0., 0., 1.]])

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

### Numpy Vectorized Operation


arr1=np.array([1,2,3,4,5])
arr2=np.array([10,20,30,40,50])

### Element Wise addition


print("Addition:", arr1+arr2)

## Element Wise Substraction


print("Substraction:", arr1-arr2)

# Element-wise multiplication
print("Multiplication:", arr1 * arr2)

# Element-wise division
print("Division:", arr1 / arr2)

Addition: [11 22 33 44 55]


Substraction: [ -9 -18 -27 -36 -45]
Multiplication: [ 10 40 90 160 250]
Division: [0.1 0.1 0.1 0.1 0.1]

## 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))

[1.41421356 1.73205081 2. 2.23606798 2.44948974]


[ 7.3890561 20.08553692 54.59815003 148.4131591 403.42879349]
[ 0.90929743 0.14112001 -0.7568025 -0.95892427 -0.2794155 ]
[0.69314718 1.09861229 1.38629436 1.60943791 1.79175947]

## array slicing and Indexing

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]])

## Modify array elements


arr[0,0]=100
print(arr)

[[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]]

### statistical concepts--Normalization


##to have a mean of 0 and standard deviation of 1
data = np.array([1, 2, 3, 4, 5])

# Calculate the mean and standard deviation


mean = np.mean(data)
std_dev = np.std(data)

# Normalize the data


normalized_data = (data - mean) / std_dev
print("Normalized data:", normalized_data)

Normalized data: [-1.41421356 -0.70710678 0. 0.70710678 1.41421356]

data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# 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])

data[(data>=5) & (data<=8)]

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

You might also like