Python Data Science Tutorial #2 - NumPy Arrays

NeuralNine · Beginner ·🛠️ AI Tools & Apps ·6y ago
Today we are starting out with the basics of NumPy and NumPy arrays. Website: https://www.neuralnine.com/ Instagram: https://www.instagram.com/neuralnine Twitter: https://twitter.com/neuralnine GitHub: https://github.com/NeuralNine Programming Books: https://www.neuralnine.com/books/ Outro Music From: https://www.bensound.com/ Subscribe and Like for more free content!

What You'll Learn

This video tutorial covers the basics of NumPy and NumPy arrays, including their creation, manipulation, and use in calculations, with tools such as NumPy, Pandas, and TensorFlow, and concepts like mathematics and algebra, linear algebra, vectors, and matrices.

Full Transcript

what is going on guys when welcome to the spies in tutorial series for data science and I'm very happy to introduce you to the first real episode where we get into data science because up until now everything that we've done with Python was either beginner stuff or some intermediate concepts like XML processing database access and these are fine but now we get into some really interesting with data science now in the last video we set up anaconda we set a Python now we have the environment that we need and in today's video we're really getting into data science we're starting with the real content and the first part here is to learn about numpy and numpy erase now numpy is a very essential library when it comes to data science and python the problem is though or it's not a problem actually but what you need to know about numpy is that it in and of itself is not enough for data science I mean you could do data science even without all these libraries but if you want to do some real easy data science in Python numpy is on a low level it's the basis for the higher level libraries like map table like pandas and later on also like tensorflow and SK learn and so on so all of these libraries or most of these libraries use numpy has a basis but nan PI in and of itself is just a library that allows us the efficient and effective processing of lists and erase and this is something that we need of course because everything that has to do with data science and machine learning has a lot to do with mathematics and algebra and linear algebra especially so we need matrices we need vectors and we need a way to really use these things and process these things in a very efficient way and this is what we use a numpy for numpy is the basis for all of it and since it's such an essential part of data science we're going to learn about it right in the beginning so let us get into the code so now the first thing that we need to do when we work with numpy is to import the module itself so we're going to say import numpy as and P and this SMP here just means that I'm giving it an alias so another name to use instead of numpy to make the code shorter and easier you don't have to do this but usually this is what you do when you work with numpy you give it an alias and P and this would be the same thing let's use a library that we know as saying import XML as I don't know X I mean X would be a quite stupid name but I could give any library any alias so I can also I don't have to give it an P I can also say my data science science library but this would be kind of kind of productive so we're just saying I'm play s and P this is also what you usually do and now we can use NP dot all the functions instead of using numpy dot all the functions and the first step here or the first thing that we're going to do with numpy is to create a basic array so a basic numpy array and an umpire array is pretty similar to a Python list so a Python list looks like this if you remember one two three four and an umpire array would actually just be NP dot array with a list in it so we pass a list to the array function so 1 2 3 4 for example and now this list gets converted by numpy into an array into a numpy array and then we can work with it and it's not it's it's pretty similar trellis but it works very differently because or focus and numpy is on mathematics on algebra on vectors and matrices so when you add to lists and python you get a new list with all the elements but when you add to erase and numpy you actually do calculations so let's create a second one of 5 6 7 8 and what I can now do of course is I can use them as or like simple numpy or like a basic Python list sorry so I can just say print a 0 and print B 1 and this will just access the individual elements so now to run the script for those of you are not familiar with Python I right-click the main py and then click on run made and then I don't get a new window but I get the output down here so one and six are the results to 0th index is 1 and the first index of B is 6 so this is what I get and of course what I can do with numpy is I can also create multi-dimensional arrays so I can say NP dot array and now instead of passing just a list I pass a list of Lists so the finish but the first list is the main list and then in this list I have other lists for example 1 2 3 4 5 6 7 8 9 and this would be a 3 by 3 matrix as you can see so I could print C and you would get what you would see that this is here a 3 by 3 matrix and to find out what shape an array has if I don't see it or if I don't know it I can just say print C dot shape so this would be as you can see 3 by 3 so this would be a 3 by 3 matrix and this would be a four dimensional vector so we could already go ahead and do some calculations with this so let's take two basic piping lists X 1 2 3 4 4 and y 5 6 7 8 and see what happens when we add X plus y as you can see what happens is I get a new list with all the elements however if this is a Python numpy array sorry if this is a numpy array what happens is I do some algebraic calculations here so I say ray and these are now two vectors these are 2 4 dimensional vectors and what I'm doing by adding them is I'm doing a vector addition I'm not just adding to erase together in in the sense that I had the elements together to form a new array I do some calculations with them so the result would be another four-dimensional vector with the results of the addition so 6 8 10 and because of one plus five to plus six and so on so this is basically what numpy arrays are so now let us take a look at the different ways to fill up numpy arrays because sometimes we want to create a numpy array that has some default values or have is already filled up with values but we don't want to do it manually because of course we could go ahead and say a equals NP dot array and then just say okay give me that shape and here I have a bunch of zeros and then I have a bunch of zeros and I want to fill up an array full of zeros here but I don't want to do it manually because maybe I want to have a 5 by 6 by 7 by 8 matrix and this would be kind of stupid to do that manually and for that we have a bunch of numpy functions for example I can just say a equals NP dot zeros and I can specify a shape here for example 5 by 7 by 3 and then I can just print a and of course this will create a huge array of the given dimensions full of zeros as you can see so I have let's take a look I have 5 main list years of 5 lists full off 7 rows with 3 elements each so this would be full of zeros 5 by 7 by 3 now of course I can do the same thing with once so this would fill up the whole array with once if I need it for some reason but I can also specify a value by saying NP dot fill a full I don't know I think full N P dot full then specify the shape and then as a second parameter specify the number for example 9 because we're in neural 9 here and then you see it creates everything filled with ninths now I could also go ahead and say n P dot empty and it would create me a numpy array with values that are not initialized now oftentimes these values are zero but they don't have to be so I can just say eight times sorry eight times four times two and when I say it's empty we have different values we have something like this we have some zeros but these values are basically not initialized they are not zero they are not some specific value they're just empty so we just created the structure but there are no values in there I mean there are values in there but these values are not initialized so if you just need an empty array use the empty function another very useful method is the random function so an p dot random dot random we need to use it two times and peed-on random dot random ah and then we specify a size for example five by five and then it fills up the array with random numbers from zero to one as you can see so these are all random values and the array is completely random this is especially useful when we want to train a model and we don't have what we don't want to have a biased input we just pass a vector or an array or a matrix full of random values so that we have a random starting point this is something that we're going to use in the future um yeah basically that's it I don't know maybe there are there are some other functions so for example nan but I don't think so but these are basically the functions that we can use to fill up a race and they make a lot of things easier because we don't have to always fill them up manually now let's look at two more very important and interesting functions in numpy sometimes when we create a race we want to have values of a range in it so for example I want to have five ten fifteen twenty twenty five and so on because maybe I have a mathematical function and I want to have the ax value so let's call this X right now I want to have an array of all the X values and I want to have them in a step size of five so actually let's start at 0 0 5 10 15 20 25 and so on up until 30 and this is what I want and maybe I want is up until I don't know a thousand or something so it would be quite ineffective to put all these values in manually because this would be my X values and my Y values shall then be I don't know x times 2 minus x squared or something like that this might be a function and we've seen that we can use mathematical functions on numpy arrays because we actually do the calculations with the vectors or with the lists instead of just adding them or duplicating them so this would be a valid function we could just say x times 2 minus x squared this would be a mathematical function and I could go ahead and print that one time is enough I could just print that and you will see the result of this function but instead of just doing this manually what we can do is we can use one of two functions we can use a range or a Lim space so a range would give me it would be a function that I have to specify a start value for and end value for and a step size so I say give me all the values from 0 up until a thousand with a step size of 5 this would be what I want so it would give me 0 5 10 15 20 25 and so on up until a thousand and then I could just go ahead and calculate the value so here I would have all the results for these values of my function of course I can also use a numpy function for that for example I can just say okay give me the sign of it so and P dot sign a mathematical function and peace on X and then I would get the value of that I can do everything with these values now another way to do that if I don't want to do it like that I can say linspace and say again minimum and maximum but now instead of saying I want to have a step size I say how many values do I want to create evenly distributed so instead of saying give me every value that is five larger I say give me a hundred values in between so I can just say give me a hundred values that's for an X to see what happens it gives me these values because these are the evenly distributed values from zero to a thousand if I want to have 100 of them I can also say give me 101 maybe and then it would change the values because then I would have 101 values evenly distributed between the minimum and the maximum so this would be another way to do that and this is usually how we create the ax values or all kinds of different input values because we just want to have a bunch of input values to calculate the output for so last but not least let's take a look at some of the attributes in a rake in half so if I create an P sorry a equals and P dot array and then I say I'll have a list of lists and then I have 1 2 3 in here 4 5 6 in here and then I have another list here and in this list I have 10 20 30 and 40 50 60 what I can add do is I can get some information about this array so for example I can just as we already did in the beginning I can just get the shape of it so 8 dot shape is one of the attributes that I might be interested in and in this case I have a 2 by 2 by 3 array so I have two lists in here two main lists then I have two lists this list or in these lists and then I have three values in each list so this would be two by two by three but maybe I want to know how many elements are in my array so a dot size would be a good indicator of that so 12 because I have six year and six year one thing that I would like to mention is that we don't need to always have the same structure for example I could also add a list of two values in here so I would then have two lists here and three lists here and one of the lists even has only two values in it of course what happens Dennis I cannot really print a shape in anymore because I don't have a shape it would say okay you have two main lists but basically that's it because in there you have a different amount of lists and a different amount of elements so I cannot give you a shape what I can do however is I can get the dimension off an array so I can get ending sorry not them and it would give me how deep an array goes in this case it would give one because I have only one dimension that is the same it would give me you have one dimension here but then already you have two elements here three elements here we cannot talk about dimensions but if I remove this it will say that I have three dimensions of course because I have the same values I have two then again two and then three each so when we're dealing with the same with the same shapes we have dimensions in this case three dimensions of course and another attribute that would be quite interesting is the data type attributes so in this case I have a bunch of integers in here but I can say a day type or D type sorry and will give me int 32 so an integer with 32 bits of course I can also specify a data type so at the end of the definition of Lists I can specify a type equals float for example then I would get the datatype float64 and if I print a as a whole array here it would give me the same numbers but in a float format so I have one point to point and so on and this is basically what we can do with the attributes we have the dimensions we have the size the size gives us the amount of elements that I mentioned it gives us information about how deep the array goes with with the same shape the shape gives us the shape obviously and the datatype gives us the datatype of the individual elements so this was the first part on numpy in the next video we're getting deeper into the material and I hope you liked today's video I hope you learned something if so hit the like button and feel free to ask any questions and give feedback in the comments section down below of course subscribe to this channel if you want to see more keep watching the videos we're getting deeper and deeper into data science as we progress as we go on and again thank you very much for watching see you in the next video and bye [Music]
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 24 of 60

1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video tutorial teaches the basics of NumPy and NumPy arrays, including their creation, manipulation, and use in calculations, and is a fundamental skill for data science and machine learning. The tutorial covers various concepts, including mathematics and algebra, linear algebra, vectors, and matrices. By the end of the tutorial, viewers will be able to create and manipulate NumPy arrays, perform calculations, and use them for vector operations.

Key Takeaways
  1. Import the NumPy module and give it an alias
  2. Create a basic NumPy array by passing a list to the array function
  3. Add two NumPy arrays for calculations
  4. Access individual elements of a NumPy array
  5. Create a NumPy array using np.array
  6. Use np.zeros to create an array with default values
  7. Use np.ones to create an array with default values
  8. Use np.full to create an array with default values
  9. Use np.empty to create an array with uninitialized values
  10. Use the empty function to create an empty array
💡 NumPy arrays are a fundamental data structure in data science and machine learning, and understanding how to create and manipulate them is crucial for working with data.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →