0% found this document useful (0 votes)
12 views23 pages

Grade 9, Week 1 - Numpy, 2D Arrays

Uploaded by

divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views23 pages

Grade 9, Week 1 - Numpy, 2D Arrays

Uploaded by

divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

KEYWORDS

print(“ “ )
{
NumPy

What is Data? 4 Creating a NumPy Array


1
Data can be a name, number, colors etc
Computer systems are designed to process this data.

2 Why use NumPy?


NumPy supports more mathematical operations
than Python data structure.

3 How to use NumPy?


We will import the NumPy library in Python.

Output
Changing a Python list to a NumPy Array

Output
Creating an array from user input

Create a code that will ask students to


enter their age and store their answers in
a NumPy array.

The number of elements needs to be specified at


the beginning. Then each element should be
entered one by one.
Creating an Array using a range of values

Create an array that carries values from 0 to 15 with the


SB Activity 1.1.1
step size 2.
Create an array containing odd numbers
between 1 and 100.

Output
Output

As you can see, an array that carries values from


0 to 15 with step size 2 was created using the
NumPy arange() function.
Creating a Two- Dimensional Array SB Activity 1.1.2
Use 1 table for this information instead of 2.
As of now, we have only worked with 1 Dimensional
Can you?
Array.

Single row and Single column

Now, let’s see 2 Dimensional Arrays.

An Array with multiple rows and columns is


a Two-Dimensional Array or a Matrix.
Scalar, Vector, Matrix and their difference
Accessing Data in an Array
This table stores the names of 5 students. Let’s create a numpy
Data in a NumPy array can be accessed using indices like
1 Python lists. The index is the address of the elements
array and print the name stored in the second index

Output
Activity

https://wordwall.net/resource/21225484
Accessing data in a 2D Array

In 2D arrays, elements can be accessed by


specifying the row and column indices.

Output
Accessing rows and Columns
Colon “:” is How to print the column:
Let’s look at the same example- used to select
Let’s try to store Duaa’s data in
the starting and
an array
ending values

0:4 will start


from index
0 and end
at index 4(4
is not
How to print the first row: included)

This means index


values (0,1,2,3)

Output
Assigning New values to elements

https://wordwall.net/resource/21250840
Recap

https://wordwall.net/resource/35312339
Reshaping an array

The shape of an array is the number of its Let’s reshape this


rows and columns. Rearranging the elements data to understand
it better
in an array is called reshaping .
The NumPy reshape function

Reshaping can be done using NumPy . It can


make the data more readable by humans.
Let’s look at an example

A weather station in the EXPO2020 area is


collecting the temperature data every six
hours using a temperature sensor . The first
reading was at 12:00 midnight . The collected
data over the past three days are [15, 12, 26,
Output
21, 16, 12, 27, 20, 16, 13, 29, 22] . Twelve data
points were collected
Reshaping an array

Since there are 24 hours in a day, We have used the


1
reshape() function
there will be four data points for
each day

2 Instead of having the three days


data in a single row, we need to
make a row for each day

Output
Transposing an array
The Transpose Function
Transpose changes each row to a
column and each column to a row.
a. Create a NumPy array for the information shown in the table above

Output
b. Reshape the array to (10,1) 10 rows, 1 column. Is this a better way to present the data?
This is not a good way to represent the data, as its names and seconds (10,1)
wouldn’t make sense.
c. Can you reshape the array to (4,3) 4 rows and 3 columns? If not, why
We cannot reshape the array to (4,3) for the same reason.
d. Reshape the elements of the array in a way that e. Transpose the array and write your observation on
makes the data easier to read (5 rows, 2 columns) the new shape
Recap

Data preparation technique - Match up (wordwall.net)

You might also like