Numpy - Part 01
Summary
TLDRThis lecture introduces the NumPy library in Python, emphasizing its role in data manipulation and linear algebra. It covers creating and reshaping NumPy arrays, accessing elements through slicing, and iterating through arrays. The lecture highlights the efficiency of NumPy arrays compared to lists for mathematical operations, making it an essential tool for data science.
Takeaways
- đ Numpy is a fundamental library in Python for data manipulation, especially useful for working with arrays and matrices.
- đą Numpy arrays are n-dimensional, meaning they can have any number of dimensions, and they must contain items of the same type.
- đ The `reshape` function in numpy allows for the reshaping of arrays into different dimensions, which is not possible with standard Python lists.
- đ Numpy arrays can be created using a variety of methods, including `np.arange`, `np.tile`, `np.zeros`, and `np.ones`.
- đŸ Numpy arrays are indexed starting at zero, following the Python convention, and support slicing and advanced indexing.
- đ The slicing in numpy arrays is more powerful than lists, allowing for steps in slicing and the selection of entire rows or columns.
- đ Iterating through numpy arrays can be done with loops, and `enumerate` can be used to get both the index and value during iteration.
- đ The `np.arange` function in numpy is similar to the `range` function in Python, but returns a numpy array instead of a range object.
- đ Numpy arrays are more efficient for mathematical operations than lists, which is why they are widely used in scientific computing.
- đ The script emphasizes the importance of hands-on practice with numpy to fully understand its capabilities and advantages over native Python lists.
Q & A
What is NumPy?
-NumPy is a library in Python used for scientific computing. It allows for the creation and manipulation of arrays and matrices, which are essential for various mathematical and scientific operations.
Why is NumPy preferred over lists for mathematical operations?
-NumPy is preferred because it is designed for numerical operations and supports vectorization, which makes it faster and more efficient than lists for mathematical computations.
What is the significance of NumPy arrays being n-dimensional?
-NumPy arrays can be n-dimensional, meaning they can represent data in multiple dimensions (like vectors, matrices, or higher-dimensional arrays), which is useful for complex mathematical and scientific computations.
How do you import NumPy in Python?
-You can import NumPy in Python using the 'import' statement, often with a nickname 'np' for convenience, like so: 'import numpy as np'.
What is the difference between a list and a NumPy array?
-A NumPy array is a collection of elements of the same type, unlike a Python list which can contain elements of different types. NumPy arrays are also more memory efficient and support a large number of mathematical operations.
How can you create a NumPy array?
-You can create a NumPy array using the 'np.array()' function, providing a list or any iterable as an argument. Other functions like 'np.arange()', 'np.zeros()', and 'np.ones()' can also be used to create arrays with specific patterns.
What is the purpose of the 'reshape' method in NumPy?
-The 'reshape' method in NumPy is used to change the shape of an array without altering its data. This is useful for organizing data into a desired format for specific operations.
How does slicing work with NumPy arrays?
-Slicing in NumPy arrays works similarly to lists but with additional capabilities. You can use slicing to access rows, columns, or specific elements using indices. This includes using steps and negative indices to reverse through the array.
Can you iterate through a NumPy array?
-Yes, you can iterate through a NumPy array using loops. You can loop through each element, or use 'enumerate' to get both the index and the element during iteration.
What are some common operations you can perform with NumPy arrays?
-Common operations include mathematical computations like addition, subtraction, multiplication, and division, as well as more complex operations like matrix multiplication, statistical analysis, and random number generation.
Outlines
đ Introduction to Numpy
The speaker introduces the numpy library in Python, which was briefly used in the previous chapter. The focus of the lecture is on numpy's role in handling data with arrays and matrices, contrasting it with lists. Numpy arrays are multi-dimensional and require uniform data types, unlike lists which can be heterogeneous. The speaker mentions the benefits of using numpy arrays for mathematical operations and how they differ from lists. The lecture aims to delve deeper into numpy, covering array creation, manipulation, and operations like reshaping and generating random numbers.
đą Creating Numpy Arrays
The paragraph explains how to create numpy arrays. It discusses the use of 'np.array' to convert lists to numpy arrays and the importance of consistent data types within arrays. It also introduces 'np.arange' as a way to create arrays similar to Python's 'range' function, with examples of creating sequences with steps. The speaker further explains the default behavior of 'np.arange' and how it can be used to generate sequences with different steps and ranges. The versatility of creating numpy arrays with different methods is highlighted.
đ Reshaping and Copying Numpy Arrays
This section covers how to reshape numpy arrays using the 'reshape' method, allowing the transformation of a linear array into a matrix form with specified rows and columns. The importance of compatibility in the product of dimensions when reshaping is emphasized. The paragraph also explains how to create copies of numpy arrays to avoid modifying the original array, ensuring changes in one do not affect the other. The concept of 'np.tile' for repeating arrays and 'np.zeros' for creating arrays filled with zeros are introduced, along with their applications.
đ Accessing Elements in Numpy Arrays
The speaker discusses how to access elements in numpy arrays, drawing parallels with list slicing but highlighting the enhanced capabilities with numpy arrays. It explains indexing with positive and negative indices, selecting entire rows or columns, and using slicing to access specific elements. The paragraph also covers the use of commas and colons for more complex slicing operations, which were not possible with lists. The ability to iterate over arrays and access elements in a matrix-like format is emphasized.
đ Iterating Through Numpy Arrays
This part of the lecture explores iterating through numpy arrays, comparing it with iterating through lists. It explains how to loop through arrays using a simple for loop, accessing elements one by one. The paragraph also introduces the use of 'enumerate' to loop with index and value, allowing for more detailed iteration. The concept of looping through specific elements, such as every second element or specific rows and columns, is discussed, showcasing the flexibility and power of numpy arrays for data manipulation.
đ Encouraging Active Learning
The final paragraph serves as a call to action for the viewers to actively engage with the material. The speaker suggests pausing the video to practice the concepts discussed, emphasizing that passive viewing is not sufficient for learning. The importance of hands-on practice for better understanding and retention of numpy array operations is highlighted.
Mindmap
Keywords
đĄNumpy
đĄArray
đĄn-dimensional array
đĄHomogeneous
đĄShape
đĄReshape
đĄSlice
đĄIterate
đĄBroadcasting
đĄData manipulation
Highlights
Introduction to NumPy, a fundamental library for data manipulation in Python.
NumPy enables the creation of arrays, which are similar to lists but with additional capabilities.
NumPy arrays are n-dimensional, meaning they can have any shape defined by the number of dimensions (n).
NumPy arrays require uniform data types, meaning all elements must be of the same type.
Arrays in NumPy are indexed starting at zero, following Python's zero-based indexing.
NumPy can be imported with an alias 'np' for convenience.
Creating arrays in NumPy can be done in various ways, including from lists or using the 'np.array' function.
The 'np.arange' function is used to create arrays of sequentially increasing values, similar to Python's 'range'.
NumPy arrays can be reshaped into different dimensions using the 'reshape' method.
The 'copy' method in NumPy creates a deep copy of an array, allowing modifications without affecting the original.
The 'np.tile' function is used to repeat elements of an array, creating a new array.
Arrays can be initialized with zeros using 'np.zeros' or ones using 'np.ones'.
Accessing elements in NumPy arrays is done using slicing, which is more flexible than list slicing.
Negative indices can be used to access elements from the end of the array.
Iterating through NumPy arrays can be done using loops, similar to iterating through lists.
The 'enumerate' function can be used to loop through arrays with both index and value.
NumPy arrays support advanced slicing, allowing for complex data extraction.
The power of NumPy comes from its ability to handle data like matrices in mathematical notation.
Transcripts
[Music]
hello everyone and uh welcome to the
Sweet the following uh of this lecture
so uh after having seen uh how we can
manipulate data how we can do with
metrics Etc we're going to do some
introduction to numpy what is nampai uh
numpy is a library
in Python so you sort of have it seen uh
in the last chapter where I've used it
uh but no I'm going to go more in
details you remember when I was doing
this reshape when I was creating npra I
was already using numpy but I didn't
want to spend too much time on this
because the goal of this lecture before
was the linear regression was the
metrics uh and you see like usually most
of the library are using arrays and
they're not using list they're using
this kind of arrays or matrixes to do
operation
to do the operation it requires uh this
list Etc to be in this array shape why
we're going to see because
um
this numpy object are somehow have any
advantages or maybe could be
disadvantages so at least doesn't have
uh so we're going to do sort of like a
deep dye into numpy so numpy are a way a
library to be able to enable you to
construct arrays so it looks a bit like
a list so if you remember what we did on
list it's going to look similar but
there is stuff that you weren't able to
do honest and that you're able not to do
with like a numpy array so we're gonna
go through the library so we're going to
do some introduction we're going to see
different operation we can do with numpy
uh how we can create and manipulate
array and uh you know Concan it function
how we can create regenerate random
numbers Etc so this is how what we're
going to do with this introduction to
numpy
so numpy what is it uh numpy uh are
working with a type of array that I call
numpy array this numpy array are n
dimensional array meaning the average
shape of n
so n is the shape of the array
um we also need there is only one kind
of items a race composed of you remember
what we're doing list we're having
sometimes string myths with number mixed
with pulean mixed with function here in
the NP and the NP array we need to have
the same time so it has to be consistent
so it is called a nobigenous collection
of exactly the same data I can't have a
string and then numbers so usually we
will work with numpy number with like
integer or float or binary usually we I
mean else you just use the left right
because the operation we're going to do
on API array are more like math and
function you know like operations let's
say not ready like counting the number
of string Etc
uh ask for a list all the array in numpy
or index starting at zero and then
getting at M minus one or a minus one
following the python conversion so it's
like the less the first element is at
position zero so second element is at
position one the third element is as
position two three
so this is uh how how it works
um so how does it look uh to use the
clippery uh we need to import the
library remember so we already did that
before you know when we're very like
importing uh the test test grid Etc uh
so you know we're gonna uh look into
um
I think uh array so
and then bye
and then
page uh so we're going to be there in
numpy so we need to import so I can do
important Empire so if I do important
pie then if I do numpy dot I will see
all the possible stuff I can do all the
possible method or attributes uh but uh
as I showed you before we like to use
some uh nickname uh for our package so
we will call it as NP so does it mean I
am porting my new P package and no in my
code I will refer to numpy as NP so now
if I do numpy it has been imported but I
can do NP Dot and I will get the same
stuff right so I will say I have all
kind of a different thing happening
identity Etc zero like split no no no
unraveling uh so this is uh quite nice
uh to work with you know you don't need
to rewrite numpy every time you can just
write NP
so this is going to be the nickname of
all function number of our method
so the first question you can ask
yourself is how do we create an array
with this there are different ways Now
to create an array so we're going to go
through them so
um what we want to do is we want to
create an array
so I want to create an array uh so I can
do NP dots and then I can see array
so if I do this uh I can go into the
thing and it tells me uh I need some
argument right so I can do in puree of
zero and then if I have a b a either an
array of zero and then maybe I want to
put a list you know so I want to put the
left I want to put 0 1 2 3 4. and then
my a is going to be 0 1 2 3 4. if you
are modified to 1 a is going to be one
one two three four no if I can put a
string uh a is there but the D type
there are no all string so basically if
you put a string here because they all
have to be
um a string you know all these different
value has to be a string then a you know
a um
a of zero so the first element of zero
is going to be the type
of the first element is going to be a
string uh because it always had to be
the same so this one can be converted a
string you know this is possible but
sometimes it's not possible and here uh
you will say a true has been converted
to a string as well uh so that is uh
what's happening with this NP array
um so this is uh how it can work and
there is also other possibility to
create array there is something called
lprh
so if you remember the classes uh on the
four you remember her range was looking
so range was working like you know you
specify a range of five
um you know you were doing like
something like 4E in range or five and
you were doing uh Prince e
print it I'm in here
and really cool so it can be like one
out of five you know and you could do
one to five and then it will go from one
to five with steps two so it is the same
Principle as uh when you do NPR it if
you do NP dot a range of five it bit
work like this dot range function right
so NPR range of five
it will be the same as doing 4E
in range five
I do a
a equal
and we do a DOT open
e
so if I do this
if I do this and then I print my a I
will get 0 1 2 3 4. but the type of a
here is a list right because I created a
list and then I can do like you know
npra
NP array all right
of a and then I will get the same stuff
so instead of doing you know going
through my um group here I can just do
NPR render five and now if I do 0 5 or
if I do one or five
my result is the same as when I do range
105 and then I can also do with like a
step of two and I get one or three same
I feel happy uh why would you use a stop
sooner so let's say you know could be
that I want to get all number from 0 to
100 with step 10 so if I do this you
know we get 0 10 20 15. or I could also
get you know or maybe I want to compute
themselves and I will do minus one I won
with step of uh zero one
a minus one
arrange only
um here and I will get from menu zero
minus 10 and then so here we get your
numbers uh so if you do count if you do
length
of this
then you get 20 right because 1 minus
one 20 steps to one with step of zero so
uh you can also put 1.1 so you sure two
two half so last value we see if you
want
so this is uh how it works uh for uh
this NPR range so if you just put a
number if you don't put anything by
default uh doing NPR range of 15 if you
do NPR range of 15 It Is by default the
same as doing one fifteen and one a zero
fifteen and one
so it says like it's the same you know
of uh and doing just just NP arrange
of 15. so this will be equivalent if you
want to just like by default this value
and this value are zero and one
um then uh there is this composite that
we've seen a bit
um with
um with the thing is like this reshape
so here uh I have dot reshape and I can
reshape it in something else so Dr shape
I'm like how does it work you know so
let's say I have my Vector here of uh 14
value in it and I my vac
and I can do reshape
movie shape
so if I do reshape I can go into the
head you know and I need to puzzle shape
so I already know that land of egg
so lenovac is 15 you know but maybe I
want something uh that is only having
four like let's say 16 16 element so
it's a bit better and I want to reshape
something so I want I can put you know
uh eight and two
so basically when I will do Vector shape
of eight and two it means I would like
to get this the first element will be my
number of line and the second element
will be my number of columns so here I
will have an array so it would be an
array so it is instead of being you know
a line it will just be
in common so I could get a matrix I have
16 stuff so like that's why I choose 16
right I could also do four and four and
then I will get my Matrix of like four
lines and four colors uh but if vague is
like Shadow 14. you know here it's not
possible to resize something shape so
yeah this is compatible we get this fake
and orange and the stuff uh and we've
seen that if we put a size that is not
compatible then you know doesn't work uh
so this is it for the reshape then same
as for the list we get this copy right
so now I can do vague of copy
and then I get to copy so this copy is
basically creating an output so it is
creating an output meaning if I create a
new variable so vac of copy the
of copy the copy so if I dove copy I
call this then I can do modification in
vac so I could do work of zero I'll pick
up zero and I will put one let's say
then vague.copy is still equal to zero
one two three four and same I can do
modification uh in the web copy and
don't have problem in my Vig
copy is still equal to this one so I
modify one but not the other one or if
you want to you know have like twins
um I don't know why
another way to create arrays is this npt
type
so nptile uh works this way so tile is a
bit like you're gonna repeat something
so let's say you have an array of zero
and one and you want to repeat it five
times
uh so it means oh I have zero and one
and I just repeat it five times you know
uh so it's a beat you know sometime you
want uh an array of like I don't know
only the value one and you want to have
it ten times and you will repeat it 10
times or you know you have like um
Monday
you want to create you know dates or
something like that you will do Monday
Tuesday
when Wednesday
Thursday
and Friday so let's say you just do the
working days so here and you want to
repeat it 10 times you get all your
stuff and you repeat them and then you
can combine with your reshape right so
we can put them if you don't want a huge
column you can just do reshape uh one
two three four five and we get 10 so 10
and 5. and in that case you will get a
matrix with like your day uh per stuff
you can add Saturday name on Sunday
up and if I started doing Sunday for it
to be compatible here I want to have
seven and here you will see you know I
create an array with stuff and maybe
then I will fill it or combine this
metrics with other value so that's why
your nptel is doing it just is taking
what you got and it's uh basically
um
duplicating it in lines
so that's it for the nptital so you sort
of get basis and then you want to
duplicate Etc you just replicate and you
do with this nptel uh another thing to
create stuff is called np0 so if you do
np0
then you have to specify your shape so
if you put one it will just be uh one
but then if you put a shape like three
and two it will basically create an
array with this would be the number of
rows so three and two the number of
columns so it will create a naray that
is a bit like it's a matrix basically
and you with like three row in this
three row you will have
um your zeros and here you get your two
another one is NP dot ones so what is a
p dot once doing it's like the NP of
zero uh but basically it will fill it
with uh one instead so it's like three
and two so if you do three and two a
fake one so it's the same as before but
you just do this
sometimes it's a practical to have this
because you have your one and you just
multiply by four and you have your four
you know depend on the operation you
want to do sometimes you want to add a
constant so you just empty once of your
shape
uh so this is also working and then uh
we've seen NP type and Main P0 rows and
stuff
so the question you can ask yourself is
how do I access this value can I access
the rule raw can I access a colon Etc so
this is what we're going to see now we
were studying um list we remember we're
able to do some slicing so we were able
to do some slicing meaning uh if this is
my Vector of uh let's say this one with
javec so I have my VEC
and I'm able to do vague of zero you
know what I can also do vague of minus
one so V of minus one will be the last
value uh in my Vector back of zero will
be the first one so this is something we
can do right but the problem is now I
have a vague uh
I'm going to call it second and it's
gonna be veg dot reshape full form
uh maybe 2882
8. so here I'm reshaping
um my vac so it has a shape of uh
so here I got it here right so I got to
that race that is looking this way and
I'm like if I do VEC of zero it's zero
so as a reminder Vic
of uh Vector is equal to this so when I
select the first element it is the first
element in this list is basically the
first row then if I want to get the
second row it will be like this second
row and then this will be fourth row uh
if I want the first element into the
fourth row I will do four and zero so
it'll be one two three four zero so
basically this would be the number of
line and this will be the index in the
column meaning this is length zero this
is line one this is line two this is
column zero and this is colon one
so I can't sort of uh slice it and it
will not work you know it's a bit like
in a mattress you will write it like
this right so you remember fourth line
uh zero one two three four it's first
element so it's a bit as uh when we are
um in Matrix you know writing this if
you did some linear algebra it's how you
will write it so this is equivalent to
write uh to write it like this just in
numpy you can use a comma instead so you
can use comma instead and this was not
possible with the list of lists we were
having before right
because this is a possibility uh there
is another possibility here about
slicing that wasn't possible with less
and that is not possible so let's say
you want to select the Blue Line you
know you could do just uh select four so
here you will get the if line eight and
nine but you could also do do
um four like this this mean I want to
select the full element the fourth row
and all uh my line but I can also do it
another way I can select all a column so
I will do vague e back to and I will get
let's say I want the First Column so I
will do I want everything that is in
column zero and in that case here you
will see you get your first column I
repeat this double dots mean everything
you know so if I do vect two of
everything this is as a row so I get
every row and then for the colon I get
only the zero if I do veg of X
everything
and everything then I got the complete
Vector right but then I'm like oh I only
want the colon zero I can only get the
column zero if I want to get only the
column one then I get my colon one and
then I can get you know all the elements
um per space of two
uh so this is only possible this means I
start off zero and I get all my element
but I can also put them here
yeah so this will also work and you get
all your limits First Column and then uh
you get everything uh so this is also
another possibility and how this dot can
be used so it is like quite easy then to
just you know slice and use my Matrix
you know you want all your row uh this
is a row so we want the First Column so
here I Want My First Column but I only
want to get one four so I start with
zero and then I do two
meaning I will get only every two
elements so I start at zero and I go
with step of size two so I go with step
of size two one step of size two and
then at four step of size two I made an
eight step of size two and then at
twelve so basically we can use this
double dot for slicing you know to say
oh it's part two and then I can be oh I
want everything in the First Column if
then I put one here I will do the same
in the second color so in the second
column meaning I select four the lines I
only select zero line then the third
line then the Fifth Line and then the
seventh line and I get all the element
in the First Column so it's like a
Crossing you know okay all the element
here that also meets the line so it's
you know like a table and you're like
okay I want index three and index four
and you get this element that is how it
is uh working uh with this vector we do
see the power of it you know so the
power of it is just that I slice and I
can access any element quite simply
and same as before you know
um we can also get you know the the last
element so the last element would be the
immunity by minus one I can do minus two
and this will be the second last element
in My First Column uh the third last
element in the First Column is 11
etc etc so you can also use this like
minus three and you can also go backward
with like negative step Etc so all the
stuff we've seen with this negative step
going forward Etc with the list is also
applicable to this array so novelty is
you can use this like uh notation and
you can use this double dot or select
wool column uh line which was not
possible with
um numpy uh with just the list before so
this is a bit of the power uh because it
just look like Matrix
in math speaking way as that's why we
need these packages and why this
packages is I think are quite practical
uh then the next point is iterating so
we remember before when we've seen the
last we were able to iterate through
list right so when I was into Richard
fullest I will do for e in um and then I
was having your last
and I could do you know I print I plus
one
prints
I plus one
so if I do this you know I print my
elements uh and now I can also iterate
through my vect you know so I could do
for e in vic of E
20.
so I'm going to start with that you know
because vac is quite easy yeah I want to
print it
so I'm starting in Vegas if you do
remember it is something like this so
quite easy know what happened if I'm
um
if I'm looping in something that is
um with a shape you know it's not shape
so when you're looking uh through VEC of
e simply you will Loop through the line
so you will look through this element
this element this element is elements if
you want to Loop through the column you
know you will do a for G in I and then
you will print here so if you do this
then uh you will Loop you know you print
uh EEG
event
so here you see you go through your
first Vector you get one second Vector
you get one you get to two three two two
three three four five this is e and your
G is four so here you are able with this
G Loop you know all the value of G will
be the value
um that you get in your um in your in
your in your array uh then the opposite
Ed you know you can also use enumerate
so we should use the numerate in the
coffee this is going to be quite nice
because then we can print e and G
and then we just do print ENT
so the enumerate uh if I do print EEG is
something that do zero one two three
four five six seven so here I have my
line you know so every time I will be
able to do print Vic
e and then my I
so if I do this you know I'm putting my
e and my G and then I'm printing uh my
first line then I'm printing my second
line only accessing with the with the G
here so the G will be my line and then
this I will be the index so this is B
first line this with the I it equal to
one second line this is I equal to two
third line this is I equal to uh three
so fourth line six and seven
fourth line one two three four six and
seven so that's how it is working you
know so you have lots of different
possibilities you can also do some zip
or as well but I think enumerates with
like enumerate
combined with the slicing and getting
the value uh then it will be less
efficient to be able to like Loop in all
of your arrays and then you can also uh
if you want every second element to Loop
in like every second element or you know
something like that
and then you will only go in a one out
of two line you know so we'll go to the
first line Second Line the fourth line
so you see like the possibility are
endless for different combination so I
would invite you to try to do this by
yourself uh so you get more of a feeling
of uh how it is working uh what is its
elements because if I'm just showing it
to you on your passive it's quite
difficult to learn by yourself but just
pause the video take a minute do this
simple stuff it shouldn't take you so
much time but just so you're like okay I
see what I'm doing
foreign
[Music]
5.0 / 5 (0 votes)