0% found this document useful (0 votes)
13 views3 pages

Numpy - Ipynb - Colab

The document provides a tutorial on using the NumPy library in Python, covering array creation, properties, indexing, manipulation, statistical functions, and saving/loading data. It includes examples of creating arrays from lists, generating zeroes and ones, reshaping, transposing, and performing statistical calculations. Additionally, it demonstrates how to save and load arrays in binary and text formats.
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)
13 views3 pages

Numpy - Ipynb - Colab

The document provides a tutorial on using the NumPy library in Python, covering array creation, properties, indexing, manipulation, statistical functions, and saving/loading data. It includes examples of creating arrays from lists, generating zeroes and ones, reshaping, transposing, and performing statistical calculations. Additionally, it demonstrates how to save and load arrays in binary and text formats.
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/ 3

8/8/25, 12:57 PM Numpy.

ipynb - Colab

pip install numpy

Requirement already satisfied: numpy in /usr/local/lib/python3.11/dist-packages (2.0.2)

import numpy as np

Array Creation:

# From Python lists/tuples:


np.array([1, 2, 3])

array([1, 2, 3])

# Zeroes/Ones/Full arrays:
np.zeros((2,3))

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


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

np.ones((3,4))

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


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

np.full((3,4), 7)

array([[7, 7, 7, 7],
[7, 7, 7, 7],
[7, 7, 7, 7]])

# Identity matrix: elements along a specified diagonal are ones, and all other elements are zeros.
np.eye(4)

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


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

# Random arrays:
np.random.rand(3,5)

array([[0.17013604, 0.77243789, 0.88120893, 0.6383317 , 0.71216972],


[0.6949926 , 0.20434138, 0.64083103, 0.3415514 , 0.56946825],
[0.50101267, 0.52807082, 0.59232464, 0.08855356, 0.87369082]])

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

Array Properties and Information:

# Shape:
arr.shape

(2, 3)

# Data type:
arr.dtype

dtype('int64')

# Number of dimensions:
arr.ndim

https://colab.research.google.com/drive/1D_4APF6gv2CfJiIxSf7EL16XbilGvjmp#scrollTo=WlnpvK5Yg8aH&printMode=true 1/3
8/8/25, 12:57 PM Numpy.ipynb - Colab
# Size:
arr.size

Indexing, Slicing, and Subsetting:

# Single element:
arr

array([[1, 2, 3],
[4, 5, 6]])

# Slicing:
arr[0:2, 1:3]

array([[2, 3],
[5, 6]])

# Boolean indexing:
arr[arr > 5]

array([6])

Array Manipulation:

# Reshaping:
arr.reshape((3, 2))

array([[1, 2],
[3, 4],
[5, 6]])

# Transposing:
arr.transpose() # or arr.T

array([[1, 4],
[2, 5],
[3, 6]])

# Concatenation:
arr1 = np.array([1,2])
arr2 = np.array([3,4])
np.concatenate((arr1, arr2))

array([1, 2, 3, 4])

# Splitting:
np.split(arr, 2)

[array([[1, 2, 3]]), array([[4, 5, 6]])]

Statistical Functions:

#Mean: sum of all el /no of val


np.mean(arr)

np.float64(3.5)

# Median: middle val in dataset


np.median(arr)

np.float64(3.5)

# Standard deviation:
np.std(arr)

np.float64(1.707825127659933)

https://colab.research.google.com/drive/1D_4APF6gv2CfJiIxSf7EL16XbilGvjmp#scrollTo=WlnpvK5Yg8aH&printMode=true 2/3
8/8/25, 12:57 PM Numpy.ipynb - Colab
# Sum:
np.sum(arr)

np.int64(21)

Saving and Loading:

#Binary format:
np.save('filename.npy', arr)

np.load('filename.npy')

array([[1, 2, 3],
[4, 5, 6]])

#Text file:
np.savetxt('filename.txt', arr)

np.loadtxt('filename.txt')

array([[1., 2., 3.],


[4., 5., 6.]])

https://colab.research.google.com/drive/1D_4APF6gv2CfJiIxSf7EL16XbilGvjmp#scrollTo=WlnpvK5Yg8aH&printMode=true 3/3

You might also like