Numpy For Beginners(Part 1)
Promise Arowosafe
Healthcare Analyst || I help healthcare organizations use data to improve patient care, reduce cost and make better decisions || Excel ||Tableau || SQL || Python
Hello Everyone,
I’m Promise, a medical doctor transitioning into the world of data analytics. As I continue my learning journey, I’ve decided to share my notes with you all. My hope is that these insights will be helpful, especially for those who are also navigating the exciting field of data science.
I’ve been learning Python and recently reached the topic of NumPy. Since this is where my journey took an interesting turn, I thought it would be a great place to start sharing my notes.
So let's get into the meat of the matter:
What is NumPy?
NumPy is a Python library that stands for Numerical Python. It’s a powerful package designed to handle numerical operations efficiently. Created by Travis Oliphant, NumPy is a foundational tool in the Python data science ecosystem.
Why Do We Need NumPy?
In Python, we use variables to store data. However, a variable can only hold a single data point (or value). To store multiple data points, we often use Python lists. The challenge with lists is that they aren’t always the best choice when dealing with large datasets or performing complex numerical operations.
This is where NumPy comes in. Here’s why NumPy is preferred over lists:
How to Create a NumPy Array
To have access to numpy, you have to first import the numpy module, which is done with the code:
Import numpy as np
(While it's not compulsory to use the alias np, it is universally accepted so that everyone can understand what is being referred to)
To create a numpy array, you then use the array function in the numpy module
i.e ?np.array()
E.g
array1= np.array([7, 14, 21, 28, 35])
Here array1 represents the variable that stores the numpy array being created
Did you notice you need to use a square bracket? Why? That’s because you’re passing a list of values to the array() function.
The above is an example of a 1 dimensional(1D) array. It contains just one list
You can also create multidimensional array
?Let’s see how to create that:
-2D array:
This is like creating a list of list i.e, a list that contains other lists.
This means there is a master list which now contains other lists separated by a comma
Using a real world example, if you have a list of items in a grocery store grouped by category and each category is a different list
Imagine this as numbers because of course numpy is numerical
-In order to create a 2D array there has to be a square bracket that encloses the other lists and a comma separating each
To check the dimension of your array, use the property?? .ndim
This shows us that it is a 2D array.
Additionally if you want to see the shape of the array (number of rows and columns, you use the? property .shape
To see the size(i.e number of elements in the array), you use the property? .size
3D Arrays
A 3D array takes things a step further.
It is like a list in a list in another list ( a list that contains a list of list or a 2D array inside another list, if that makes sense)
Going back to our grocery store example: If you have a list that contains the list of items in different grocery stores by category
Here, the first level represents different stores, the second level represents categories (fruits/vegetables), and the third level contains actual items.
Of course, numpy deals with numbers and this is just an explanation of how the dimension works
To create a 3D array, there has to be
Other ways of creating an array
1.An array that contains zeros
You use np.zeros, then include the shape(i.e number of rows and columns you are interested in)
E.g
One dimensional:
领英推荐
2 Dimensional:
This created? a 2D array with 4 rows and 6 columns, however you can notice that it created them as float.
To confirm this , you can use the .dtype property
To create an array with int data type instead, you write dtype = int, while creating the array as seen below
Well, I tried to experiment,I noticed that this only works for zeros(np.zeros) and ones(np.ones), meaning you can’t create an array that contains 2, 3 , 4 or other digits this way just 0 and 1. However, there is another function that can be used to create this which will be discussed next
?
3Dimensional:
Here you have to specify the number of lists, the number of rows and the number of columns
To create a 3D list containing 1 with 3 set of lists, 2 rows and 3 columns per list
2. To create an array with fixed values other than 0 and 1, we can use the np.full function, indicating the shape and the number interested in
E.g np.full([2,3], 42])
This creates a 2D array with 2 rows and 3 columns with the number 42
?
3. You can also create a range of numbers using the arange function
E.g To create a numpy array with numbers ranging from 1 to 28 (note that 28 will not be a part of this result, it will stop at 27, don’t ask me why, that’s how it was programmed I guess!!)
This is a 1D array, however, you can convert it to either a 2D or any other dimension with the condition that the shape must align to fit the dimension you want to create .
?How do you confirm this?
E.g for 2D, you can do 3 multiplied by 9, gives you 27 which is 3 rows and 9 columns
However, you cant reshape it into a 2D array with 2 rows and 9 columns because this will only fit in 18 values instead of the required 27 and will therefore throw an error
To reshape it into a 3D array , you can do 3 x 3 x 3, which will give 3 sets of lists with 3 columns and 3 rows each
The simple trick is to do a multiplication:
Our array contains 27 values:
3 x 9 = 27, it can be shaped into a 2D array with 2 rows and 9 columns
2 x 9 = 18, it cant be shaped into a 2D array with 2 rows and 9 columns
3 x 3 x 3 = 27, it can be shaped into a 3D array with 3 sets of list with 3 rows and 3 columns each
?
4. ?You can also create a range of numbers with equal intervals between them using the arrange function but you’ll need to specify the interval
E.g a range of 10 to 90 with an interval of 5
5.Also, another way of creating a numpy array is by using the linspace function, this helps create a range of a specific number with equal intervals
E.g If you want to create a range between 1 to 10, and you want this range to contain 8 numbers with equal intervals.
Definitely the answer will be float values because you can't get an equal interval of 8 numbers between 1 and 10 as integers
To do this, in addition to the range of numbers, you also include how many values you are interested in, in this case, its 8 values.
If you've followed along to this point, I really hope this was worth your time and you benefitted one or two things from this read
This is just the beginning of my python notes journey, kindly follow along for more
Also, i'll appreciate your comments as well as feedbacks (It's a journey of learning and growth!)
Data Analyst || Geologist || Community Manager
1 个月Promise Arowosafe, This is a great read on NumPy! It is a perfect refresher on what I have learned so far. Thank you for taking the time to put this together.
I help hospitals and operating rooms enhance patient care through data analytics | Experienced perioperative Nurse & Healthcare Analyst | Python, SQL, Power BI, Excel
1 个月Promise Arowosafe , I just finished reading the article and I must say it is a well detailed and informative piece. Your explanation of the concepts was clear and concise, and the examples you provided were helpful in illustrating the points. I particularly appreciated learning about the .ndim property, which I hadn't known about before. It's incredibly useful to know how to easily check the dimension of an array! Thank you for taking the time to write such a comprehensive article. It's clear that you're passionate about the subject and committed to sharing your knowledge with others.