Numpy for beginners (Part 2)
Promise Arowosafe
Healthcare Analyst || I help healthcare organizations use data to improve patient care, reduce cost and make better decisions || Excel ||Tableau || SQL || Python
Welcome back guys,
In the last notes, I discussed different ways of creating an array as well as how to reshape a 1D array to multidimensional array.
I’ll be continuing from there today
How about if we want to change the shape of a multidimensional array to a 1D array, how do we go about that?
There are 2 function we can use for this:
1)??????? np.flatten() (a befitting name since we want to flatten the array)
This is a 2D array using the flatten function, we can convert it into a 1D array
2)?? np.ravel() : This also converts a multidimensional array into a 1D array
What’s the difference between both?
Flatten creates a copy of the same array as a 1D array whereas ravel creates a view i.e it shows you the same 2D array but in a 1D format
If you modify the array from ravel(), it also affects the original array, whereas changes to flatten() do not affect the original array.
Using a real world example:
Imagine you have a spreadsheet with student scores in multiple subjects:
If you use flatten(), it’s like copying all the scores into a new separate list. Even if you change the new list, your original spreadsheet remains the same.
If you use ravel(), it’s like applying a filter in Excel to show all the scores in a single column. The values are the same, and if you edit them, the original spreadsheet changes too.
Indexing and Slicing
In order to fetch values in our array, we need to use their? indexes
For 1D array, this is similar to indexing in python lists
The first value is index 0, the 2nd is index of 1 and so on
In order to select the value 3 which has an index of 2 from the above array, we use
arr1[2],
?Also, note that indexes are always used with a square bracket
This is for 1D array, however, In a 2D array, things get a bit different,
Therefore if I want to select the values in the 2nd row, knowing that the index of the 2nd row is 1, I’ll use the code
?arr2[1]
Now to the columns, similarly the index for the first column is 0, 2nd column is 1, 3rd column is 2 and it continues.
Therefore to select a value 2 which is in the 2nd row, 1st column:
I’ll first select the 2nd row using index of 1, then select the first column using index of 0
This can also be done using just one square bracket but separating the indexes with a comma
3D Indexing
For a 3D array, each 2D array in it gets an index which also starts from 0
The first 2D array has an index of 0, and this is now further indexed like the 2D indexing described above
To select the first 2 rows(the first 2D array), we use the index of 0
And to then select the value we are interested in which is number 7 in index 1:
This is how 3D indexing works
?
Slicing
This helps us select a range of values from an array
For slicing, you have to use a column when selecting the range of values you are interested in
E.g
arr2[:]
Using this selects all the values in the array
Since, you did not specify any particular value, this is saying, you want to start at the beginning and stop at the end
However, if you want a specific range, e.g the first and the second row, then you have to specify it
arr2[0:2] index 2 is used here instead of 1 because the last index usually does not include itself but the index before it, so it basically starts from values at index 0 and end at index 1
If you only want the last 2 values in the first row (8,9), then you have to first select the first row by using the code
arr2[0:1]
Then to select the columns you are interested in, you use a comma to separate the row from the column. Since the values we want is 8 and 9 with 8 being in index of 1, we indicate index of 1 and can then use the column : to select to the end
In this format
Let’s try the same thing with a slightly bigger array
To select all the values in the second and third row, we’ll be starting from index 1 and ending at index 3(since we know it will actually end at 2 and not include 3)
Now to include the 3rd, 4th and 5th? columns , we,ll first separate the index with a comma, then include the index for the column which would be index 2:5
Another thing that we can do is to skip some columns or rows
E.g if we want to incrementally skip 1 row and 1 column, we can apply a step. How is this done?
By inserting a column : after you have selected the rows or columns and indicating how many steps(rows or columns) you want to skip
E.g
In Python, there is already a step of 1 automatically which means it just moves(steps) over to the next row or column without skipping over any value
0:4:2 à This selects the rows from index 0 to 3, while skipping(stepping over) a row
0:8:2 à This selects the columns from index 0 to 7, while skipping(stepping over) a column
?
Aggregation in Numpy
Aggregations which includes sum, min, max, product, mean,standard deviation, variance can be performed using numpy
We'll be using this array(arr3) for the aggregations
The aggregation functions include:
Sum:
Minimum
Maximum:
Standard deviation:
Variance:
Also you can perform aggregate functions on 2 or more arrays and numpy does this by doing a row level calculation
For example:
Looking at this numpy array
Therefore arr1 + arr2 will do a sum of the first row in arr1 and the first row in arr2, the second row in arr1 and the second row in arr2, and this continues:
Sum:
Subtraction:
Multiplication:
This is a summary of my learnings on numpy, I hope my notes also was beneficial to you.
Next, i will be doing some minor projects to solidify my learnings, please do well to follow along
Thank you
Attended Kabale University
2 天前Is it because am using vs code?
Attended Kabale University
2 天前When I was installing pip and numpy I met this obstacle when opening my first code where could be the problem