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

NumPy Basics: Vectors and Matrices

This document provides an introduction to the basics of NumPy, including how to create and manipulate vectors and matrices. It covers key operations such as finding maximum and minimum values, reshaping matrices, selecting elements, and generating random numbers. The document includes code snippets and outputs for practical understanding.
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)
6 views3 pages

NumPy Basics: Vectors and Matrices

This document provides an introduction to the basics of NumPy, including how to create and manipulate vectors and matrices. It covers key operations such as finding maximum and minimum values, reshaping matrices, selecting elements, and generating random numbers. The document includes code snippets and outputs for practical understanding.
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

SESSION1: BASICS OF NUMPY

Sl Markdown Code Output


no
1 LESSON 1: import numpy as np
BASICS
Load the
package
2 To create a vector_row = [Link]([1,2,3])
vector as a row print(vector_row)
and print the
same

3 To create a vector_column=[Link]([[1],[2],[3]])
vector as a print(vector_column)
column and
print the same

4 To create a matrix=[Link]([[1,2,3],[4,5,6],[7,8,9]])
matrix and print print(matrix)
the same
Understanding the matrix
5 Dimension print([Link])
To describe the
matrix

6 Shape print([Link])
To give the
number of rows
and columns of
the matrix
7 Size print([Link]) 9
To give the size
of the matrix
Number of
rows x No of
columns
8 To identify the print([Link])
data type

Basic calculations
9 To find the print([Link](matrix)) 9
maximum value
10 To find the print([Link](matrix)) 1
minimum value
11 To set the print([Link](matrix, axis=0))
maximum value
of each column
12 To set the print([Link](matrix, axis=1))
maximum value
of each row
13 To calculate print([Link](matrix)) 5
Average
14 Variance print([Link](matrix))
15 Standard print([Link](matrix))
deviation
16 Square root print([Link](matrix))

Reshaping the matrix


17 Simple reshape print([Link](9,1))

print([Link](3,3))

Selecting elements in a vector


Creating a vector_row =[Link]([1,2,3,4,5,6])
vector
18 Select 3rd print(vector_row[2]) 3
element in the
vector
19 Select all print(vector_row)
elements in the or
vector print(vector_row[:])
20 Select print(vector_row[:3])
everything up
to and including
the third
element
21 Select print(vector_row[3:])
everything after
the third
element
22 Select the last print(vector_row[-1]) 6
element
Slicing the matrix
Slicing the matrix

If we want the output to be the 2nd row, 3rd column Here, the array(1,2,3) is your index 0 and (4,5,6) is
index 1 and (7,8,9) is index 2 of the python numpy array. Similarly element in each row is denoted as 0,1,2
Therefore, we have printed the second element from the first index.

Remember - square brackets when you want to work with a condition

23 If we want the print(matrix[1,2]) 6


output to be the
2nd row, 3rd
column
24 Output to be the print(matrix[2,1]) 8
3nd row, 2nd
column
Transposing of a matrix
print(matrix)

25 Transpose of a print(matrix.T)
matrix

Generating random numbers


Set seed [Link](1)
26 Generate 3 print([Link](0,11,3))
random integers
between 1 and
10
27 Create a vector Z = [Link](10,50) [10 11 12 13 14 15 16 17 18 19
with values print(Z) 20 21 22 23 24 25 26 27 28 29
30 31 32 33
ranging from 10 34 35 36 37 38 39 40 41 42 43
to 49 44 45 46 47 48 49]

You might also like