Numpy - Part 01

Develhope
14 Oct 202225:30

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

00:00

πŸ“˜ 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.

05:03

πŸ”’ 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.

10:05

πŸ”„ 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.

15:05

πŸ”‘ 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.

20:06

πŸ” 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.

25:06

πŸŽ“ 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

Numpy is a fundamental package for scientific computing in Python. It provides support for arrays and matrices, along with a collection of mathematical functions to operate on these arrays. In the video, Numpy is introduced as a library that enables efficient manipulation of data, which is crucial for tasks such as linear regression and metrics calculation. The script mentions using Numpy for creating arrays and performing operations like reshaping and random number generation.

πŸ’‘Array

An array in the context of Numpy refers to a collection of elements, which can be numbers, characters, or boolean values, arranged in a grid of rows and columns. The video explains that unlike Python lists, Numpy arrays must contain elements of the same type, making them efficient for mathematical operations. The script uses the term 'array' to describe how data is structured and manipulated within Numpy.

πŸ’‘n-dimensional array

A term used in the video to describe the flexibility of Numpy arrays, where 'n' represents the number of dimensions an array can have. This could be one-dimensional (like a list), two-dimensional (like a matrix), or even higher-dimensional structures. The video emphasizes that Numpy arrays can be multi-dimensional, which is a powerful feature for handling complex data sets.

πŸ’‘Homogeneous

The video uses 'homogeneous' to describe the nature of Numpy arrays, meaning that all elements within an array must be of the same type. This is in contrast to Python lists, which can contain a mix of different data types. Homogeneity is important for the performance of mathematical operations on arrays, as it ensures consistency and efficiency.

πŸ’‘Shape

In the video, 'shape' refers to the dimensions of a Numpy array, specifying the number of rows and columns (or more generally, the number of elements in each dimension). Understanding the shape of an array is essential for manipulating and interpreting data correctly. The script mentions 'shape' when discussing how arrays can be reshaped into different forms.

πŸ’‘Reshape

Reshape is a Numpy function that changes the shape of an array without altering its data. The video script uses 'reshape' to demonstrate how to convert a one-dimensional array into a two-dimensional array (or vice versa), which is useful for tasks like transforming a list of numbers into a matrix form.

πŸ’‘Slice

Slicing in the context of the video refers to the operation of extracting a portion of an array. The script explains how Numpy allows for sophisticated slicing, including selecting specific rows, columns, or even every nth element. This is a powerful feature for accessing subsets of data within arrays.

πŸ’‘Iterate

Iteration in the video refers to the process of looping through the elements of an array. The script discusses how iteration can be done in various ways, such as looping through each element, each row, or each column. This is a common operation when processing arrays, allowing for element-wise operations or transformations.

πŸ’‘Broadcasting

Although not explicitly mentioned in the script, broadcasting is a concept closely related to Numpy arrays. It allows arithmetic operations between arrays of different shapes. For example, adding a scalar to an array where the scalar is automatically 'broadcast' to match the shape of the array. This is a powerful feature for concise and efficient code.

πŸ’‘Data manipulation

Data manipulation is a central theme of the video, referring to the various operations performed on data, such as creating, reshaping, and slicing arrays. The script provides examples of how Numpy facilitates these operations, which are essential for tasks like linear regression and metrics calculation.

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

play00:00

[Music]

play00:05

hello everyone and uh welcome to the

play00:09

Sweet the following uh of this lecture

play00:11

so uh after having seen uh how we can

play00:15

manipulate data how we can do with

play00:17

metrics Etc we're going to do some

play00:19

introduction to numpy what is nampai uh

play00:23

numpy is a library

play00:25

in Python so you sort of have it seen uh

play00:29

in the last chapter where I've used it

play00:31

uh but no I'm going to go more in

play00:33

details you remember when I was doing

play00:35

this reshape when I was creating npra I

play00:38

was already using numpy but I didn't

play00:40

want to spend too much time on this

play00:42

because the goal of this lecture before

play00:44

was the linear regression was the

play00:46

metrics uh and you see like usually most

play00:50

of the library are using arrays and

play00:52

they're not using list they're using

play00:54

this kind of arrays or matrixes to do

play00:56

operation

play00:58

to do the operation it requires uh this

play01:00

list Etc to be in this array shape why

play01:04

we're going to see because

play01:06

um

play01:06

this numpy object are somehow have any

play01:10

advantages or maybe could be

play01:12

disadvantages so at least doesn't have

play01:14

uh so we're going to do sort of like a

play01:16

deep dye into numpy so numpy are a way a

play01:20

library to be able to enable you to

play01:23

construct arrays so it looks a bit like

play01:26

a list so if you remember what we did on

play01:28

list it's going to look similar but

play01:30

there is stuff that you weren't able to

play01:31

do honest and that you're able not to do

play01:34

with like a numpy array so we're gonna

play01:36

go through the library so we're going to

play01:39

do some introduction we're going to see

play01:41

different operation we can do with numpy

play01:43

uh how we can create and manipulate

play01:46

array and uh you know Concan it function

play01:48

how we can create regenerate random

play01:51

numbers Etc so this is how what we're

play01:53

going to do with this introduction to

play01:55

numpy

play01:56

so numpy what is it uh numpy uh are

play02:00

working with a type of array that I call

play02:03

numpy array this numpy array are n

play02:06

dimensional array meaning the average

play02:08

shape of n

play02:10

so n is the shape of the array

play02:13

um we also need there is only one kind

play02:16

of items a race composed of you remember

play02:18

what we're doing list we're having

play02:20

sometimes string myths with number mixed

play02:22

with pulean mixed with function here in

play02:25

the NP and the NP array we need to have

play02:28

the same time so it has to be consistent

play02:31

so it is called a nobigenous collection

play02:34

of exactly the same data I can't have a

play02:38

string and then numbers so usually we

play02:40

will work with numpy number with like

play02:42

integer or float or binary usually we I

play02:46

mean else you just use the left right

play02:47

because the operation we're going to do

play02:49

on API array are more like math and

play02:51

function you know like operations let's

play02:53

say not ready like counting the number

play02:55

of string Etc

play02:56

uh ask for a list all the array in numpy

play03:00

or index starting at zero and then

play03:02

getting at M minus one or a minus one

play03:05

following the python conversion so it's

play03:07

like the less the first element is at

play03:09

position zero so second element is at

play03:10

position one the third element is as

play03:12

position two three

play03:14

so this is uh how how it works

play03:18

um so how does it look uh to use the

play03:21

clippery uh we need to import the

play03:23

library remember so we already did that

play03:25

before you know when we're very like

play03:26

importing uh the test test grid Etc uh

play03:31

so you know we're gonna uh look into

play03:34

um

play03:36

I think uh array so

play03:39

and then bye

play03:42

and then

play03:45

page uh so we're going to be there in

play03:48

numpy so we need to import so I can do

play03:50

important Empire so if I do important

play03:53

pie then if I do numpy dot I will see

play03:55

all the possible stuff I can do all the

play03:58

possible method or attributes uh but uh

play04:02

as I showed you before we like to use

play04:04

some uh nickname uh for our package so

play04:08

we will call it as NP so does it mean I

play04:11

am porting my new P package and no in my

play04:14

code I will refer to numpy as NP so now

play04:16

if I do numpy it has been imported but I

play04:19

can do NP Dot and I will get the same

play04:22

stuff right so I will say I have all

play04:24

kind of a different thing happening

play04:28

identity Etc zero like split no no no

play04:31

unraveling uh so this is uh quite nice

play04:35

uh to work with you know you don't need

play04:37

to rewrite numpy every time you can just

play04:39

write NP

play04:41

so this is going to be the nickname of

play04:44

all function number of our method

play04:46

so the first question you can ask

play04:48

yourself is how do we create an array

play04:51

with this there are different ways Now

play04:53

to create an array so we're going to go

play04:56

through them so

play04:58

um what we want to do is we want to

play04:59

create an array

play05:02

so I want to create an array uh so I can

play05:05

do NP dots and then I can see array

play05:09

so if I do this uh I can go into the

play05:13

thing and it tells me uh I need some

play05:15

argument right so I can do in puree of

play05:17

zero and then if I have a b a either an

play05:23

array of zero and then maybe I want to

play05:26

put a list you know so I want to put the

play05:27

left I want to put 0 1 2 3 4. and then

play05:31

my a is going to be 0 1 2 3 4. if you

play05:33

are modified to 1 a is going to be one

play05:36

one two three four no if I can put a

play05:38

string uh a is there but the D type

play05:41

there are no all string so basically if

play05:44

you put a string here because they all

play05:46

have to be

play05:48

um a string you know all these different

play05:50

value has to be a string then a you know

play05:53

a um

play05:55

a of zero so the first element of zero

play05:57

is going to be the type

play06:00

of the first element is going to be a

play06:03

string uh because it always had to be

play06:05

the same so this one can be converted a

play06:07

string you know this is possible but

play06:10

sometimes it's not possible and here uh

play06:12

you will say a true has been converted

play06:15

to a string as well uh so that is uh

play06:17

what's happening with this NP array

play06:20

um so this is uh how it can work and

play06:24

there is also other possibility to

play06:26

create array there is something called

play06:28

lprh

play06:30

so if you remember the classes uh on the

play06:34

four you remember her range was looking

play06:36

so range was working like you know you

play06:39

specify a range of five

play06:41

um you know you were doing like

play06:43

something like 4E in range or five and

play06:46

you were doing uh Prince e

play06:49

print it I'm in here

play06:55

and really cool so it can be like one

play06:57

out of five you know and you could do

play06:58

one to five and then it will go from one

play07:01

to five with steps two so it is the same

play07:03

Principle as uh when you do NPR it if

play07:06

you do NP dot a range of five it bit

play07:09

work like this dot range function right

play07:12

so NPR range of five

play07:15

it will be the same as doing 4E

play07:19

in range five

play07:22

I do a

play07:25

a equal

play07:27

and we do a DOT open

play07:34

e

play07:35

so if I do this

play07:39

if I do this and then I print my a I

play07:41

will get 0 1 2 3 4. but the type of a

play07:44

here is a list right because I created a

play07:46

list and then I can do like you know

play07:48

npra

play07:50

NP array all right

play07:53

of a and then I will get the same stuff

play07:57

so instead of doing you know going

play07:59

through my um group here I can just do

play08:02

NPR render five and now if I do 0 5 or

play08:06

if I do one or five

play08:08

my result is the same as when I do range

play08:11

105 and then I can also do with like a

play08:14

step of two and I get one or three same

play08:17

I feel happy uh why would you use a stop

play08:20

sooner so let's say you know could be

play08:22

that I want to get all number from 0 to

play08:25

100 with step 10 so if I do this you

play08:29

know we get 0 10 20 15. or I could also

play08:32

get you know or maybe I want to compute

play08:35

themselves and I will do minus one I won

play08:37

with step of uh zero one

play08:42

a minus one

play08:45

arrange only

play08:48

um here and I will get from menu zero

play08:49

minus 10 and then so here we get your

play08:52

numbers uh so if you do count if you do

play08:55

length

play08:57

of this

play08:58

then you get 20 right because 1 minus

play09:00

one 20 steps to one with step of zero so

play09:04

uh you can also put 1.1 so you sure two

play09:06

two half so last value we see if you

play09:08

want

play09:09

so this is uh how it works uh for uh

play09:12

this NPR range so if you just put a

play09:14

number if you don't put anything by

play09:17

default uh doing NPR range of 15 if you

play09:20

do NPR range of 15 It Is by default the

play09:23

same as doing one fifteen and one a zero

play09:27

fifteen and one

play09:28

so it says like it's the same you know

play09:31

of uh and doing just just NP arrange

play09:36

of 15. so this will be equivalent if you

play09:40

want to just like by default this value

play09:42

and this value are zero and one

play09:44

um then uh there is this composite that

play09:47

we've seen a bit

play09:48

um with

play09:50

um with the thing is like this reshape

play09:53

so here uh I have dot reshape and I can

play09:56

reshape it in something else so Dr shape

play09:59

I'm like how does it work you know so

play10:01

let's say I have my Vector here of uh 14

play10:05

value in it and I my vac

play10:08

and I can do reshape

play10:11

movie shape

play10:12

so if I do reshape I can go into the

play10:14

head you know and I need to puzzle shape

play10:16

so I already know that land of egg

play10:21

so lenovac is 15 you know but maybe I

play10:23

want something uh that is only having

play10:25

four like let's say 16 16 element so

play10:28

it's a bit better and I want to reshape

play10:30

something so I want I can put you know

play10:32

uh eight and two

play10:35

so basically when I will do Vector shape

play10:38

of eight and two it means I would like

play10:40

to get this the first element will be my

play10:43

number of line and the second element

play10:46

will be my number of columns so here I

play10:48

will have an array so it would be an

play10:51

array so it is instead of being you know

play10:53

a line it will just be

play10:56

in common so I could get a matrix I have

play10:59

16 stuff so like that's why I choose 16

play11:01

right I could also do four and four and

play11:04

then I will get my Matrix of like four

play11:06

lines and four colors uh but if vague is

play11:09

like Shadow 14. you know here it's not

play11:12

possible to resize something shape so

play11:14

yeah this is compatible we get this fake

play11:16

and orange and the stuff uh and we've

play11:19

seen that if we put a size that is not

play11:21

compatible then you know doesn't work uh

play11:24

so this is it for the reshape then same

play11:27

as for the list we get this copy right

play11:29

so now I can do vague of copy

play11:32

and then I get to copy so this copy is

play11:35

basically creating an output so it is

play11:38

creating an output meaning if I create a

play11:42

new variable so vac of copy the

play11:46

of copy the copy so if I dove copy I

play11:50

call this then I can do modification in

play11:52

vac so I could do work of zero I'll pick

play11:56

up zero and I will put one let's say

play11:59

then vague.copy is still equal to zero

play12:02

one two three four and same I can do

play12:04

modification uh in the web copy and

play12:08

don't have problem in my Vig

play12:13

copy is still equal to this one so I

play12:16

modify one but not the other one or if

play12:19

you want to you know have like twins

play12:22

um I don't know why

play12:23

another way to create arrays is this npt

play12:27

type

play12:28

so nptile uh works this way so tile is a

play12:32

bit like you're gonna repeat something

play12:34

so let's say you have an array of zero

play12:38

and one and you want to repeat it five

play12:40

times

play12:41

uh so it means oh I have zero and one

play12:43

and I just repeat it five times you know

play12:46

uh so it's a beat you know sometime you

play12:48

want uh an array of like I don't know

play12:50

only the value one and you want to have

play12:53

it ten times and you will repeat it 10

play12:55

times or you know you have like um

play12:58

Monday

play13:02

you want to create you know dates or

play13:04

something like that you will do Monday

play13:06

Tuesday

play13:08

when Wednesday

play13:11

Thursday

play13:15

and Friday so let's say you just do the

play13:18

working days so here and you want to

play13:20

repeat it 10 times you get all your

play13:22

stuff and you repeat them and then you

play13:25

can combine with your reshape right so

play13:27

we can put them if you don't want a huge

play13:30

column you can just do reshape uh one

play13:33

two three four five and we get 10 so 10

play13:36

and 5. and in that case you will get a

play13:38

matrix with like your day uh per stuff

play13:41

you can add Saturday name on Sunday

play13:43

up and if I started doing Sunday for it

play13:45

to be compatible here I want to have

play13:47

seven and here you will see you know I

play13:50

create an array with stuff and maybe

play13:51

then I will fill it or combine this

play13:53

metrics with other value so that's why

play13:55

your nptel is doing it just is taking

play13:57

what you got and it's uh basically

play14:01

um

play14:02

duplicating it in lines

play14:04

so that's it for the nptital so you sort

play14:06

of get basis and then you want to

play14:07

duplicate Etc you just replicate and you

play14:10

do with this nptel uh another thing to

play14:13

create stuff is called np0 so if you do

play14:16

np0

play14:17

then you have to specify your shape so

play14:19

if you put one it will just be uh one

play14:22

but then if you put a shape like three

play14:25

and two it will basically create an

play14:27

array with this would be the number of

play14:29

rows so three and two the number of

play14:32

columns so it will create a naray that

play14:34

is a bit like it's a matrix basically

play14:36

and you with like three row in this

play14:38

three row you will have

play14:41

um your zeros and here you get your two

play14:44

another one is NP dot ones so what is a

play14:47

p dot once doing it's like the NP of

play14:50

zero uh but basically it will fill it

play14:53

with uh one instead so it's like three

play14:56

and two so if you do three and two a

play14:58

fake one so it's the same as before but

play15:00

you just do this

play15:01

sometimes it's a practical to have this

play15:03

because you have your one and you just

play15:05

multiply by four and you have your four

play15:07

you know depend on the operation you

play15:08

want to do sometimes you want to add a

play15:09

constant so you just empty once of your

play15:12

shape

play15:13

uh so this is also working and then uh

play15:16

we've seen NP type and Main P0 rows and

play15:19

stuff

play15:19

so the question you can ask yourself is

play15:22

how do I access this value can I access

play15:25

the rule raw can I access a colon Etc so

play15:28

this is what we're going to see now we

play15:30

were studying um list we remember we're

play15:34

able to do some slicing so we were able

play15:37

to do some slicing meaning uh if this is

play15:40

my Vector of uh let's say this one with

play15:44

javec so I have my VEC

play15:47

and I'm able to do vague of zero you

play15:50

know what I can also do vague of minus

play15:53

one so V of minus one will be the last

play15:55

value uh in my Vector back of zero will

play15:58

be the first one so this is something we

play16:00

can do right but the problem is now I

play16:02

have a vague uh

play16:05

I'm going to call it second and it's

play16:07

gonna be veg dot reshape full form

play16:10

uh maybe 2882

play16:14

8. so here I'm reshaping

play16:17

um my vac so it has a shape of uh

play16:22

so here I got it here right so I got to

play16:24

that race that is looking this way and

play16:26

I'm like if I do VEC of zero it's zero

play16:30

so as a reminder Vic

play16:34

of uh Vector is equal to this so when I

play16:37

select the first element it is the first

play16:39

element in this list is basically the

play16:41

first row then if I want to get the

play16:43

second row it will be like this second

play16:45

row and then this will be fourth row uh

play16:48

if I want the first element into the

play16:51

fourth row I will do four and zero so

play16:54

it'll be one two three four zero so

play16:56

basically this would be the number of

play16:58

line and this will be the index in the

play17:00

column meaning this is length zero this

play17:02

is line one this is line two this is

play17:04

column zero and this is colon one

play17:07

so I can't sort of uh slice it and it

play17:09

will not work you know it's a bit like

play17:11

in a mattress you will write it like

play17:14

this right so you remember fourth line

play17:16

uh zero one two three four it's first

play17:19

element so it's a bit as uh when we are

play17:22

um in Matrix you know writing this if

play17:24

you did some linear algebra it's how you

play17:27

will write it so this is equivalent to

play17:29

write uh to write it like this just in

play17:32

numpy you can use a comma instead so you

play17:35

can use comma instead and this was not

play17:37

possible with the list of lists we were

play17:39

having before right

play17:40

because this is a possibility uh there

play17:43

is another possibility here about

play17:44

slicing that wasn't possible with less

play17:48

and that is not possible so let's say

play17:51

you want to select the Blue Line you

play17:53

know you could do just uh select four so

play17:55

here you will get the if line eight and

play17:57

nine but you could also do do

play18:00

um four like this this mean I want to

play18:03

select the full element the fourth row

play18:06

and all uh my line but I can also do it

play18:10

another way I can select all a column so

play18:12

I will do vague e back to and I will get

play18:15

let's say I want the First Column so I

play18:17

will do I want everything that is in

play18:19

column zero and in that case here you

play18:22

will see you get your first column I

play18:24

repeat this double dots mean everything

play18:27

you know so if I do vect two of

play18:30

everything this is as a row so I get

play18:33

every row and then for the colon I get

play18:36

only the zero if I do veg of X

play18:39

everything

play18:41

and everything then I got the complete

play18:43

Vector right but then I'm like oh I only

play18:46

want the colon zero I can only get the

play18:48

column zero if I want to get only the

play18:50

column one then I get my colon one and

play18:52

then I can get you know all the elements

play18:55

um per space of two

play18:57

uh so this is only possible this means I

play19:00

start off zero and I get all my element

play19:01

but I can also put them here

play19:10

yeah so this will also work and you get

play19:12

all your limits First Column and then uh

play19:15

you get everything uh so this is also

play19:17

another possibility and how this dot can

play19:20

be used so it is like quite easy then to

play19:23

just you know slice and use my Matrix

play19:25

you know you want all your row uh this

play19:27

is a row so we want the First Column so

play19:31

here I Want My First Column but I only

play19:33

want to get one four so I start with

play19:35

zero and then I do two

play19:37

meaning I will get only every two

play19:39

elements so I start at zero and I go

play19:42

with step of size two so I go with step

play19:45

of size two one step of size two and

play19:48

then at four step of size two I made an

play19:50

eight step of size two and then at

play19:52

twelve so basically we can use this

play19:54

double dot for slicing you know to say

play19:56

oh it's part two and then I can be oh I

play19:59

want everything in the First Column if

play20:01

then I put one here I will do the same

play20:03

in the second color so in the second

play20:05

column meaning I select four the lines I

play20:08

only select zero line then the third

play20:12

line then the Fifth Line and then the

play20:14

seventh line and I get all the element

play20:17

in the First Column so it's like a

play20:19

Crossing you know okay all the element

play20:20

here that also meets the line so it's

play20:23

you know like a table and you're like

play20:24

okay I want index three and index four

play20:26

and you get this element that is how it

play20:29

is uh working uh with this vector we do

play20:33

see the power of it you know so the

play20:35

power of it is just that I slice and I

play20:38

can access any element quite simply

play20:40

and same as before you know

play20:43

um we can also get you know the the last

play20:45

element so the last element would be the

play20:47

immunity by minus one I can do minus two

play20:50

and this will be the second last element

play20:51

in My First Column uh the third last

play20:54

element in the First Column is 11

play20:57

etc etc so you can also use this like

play20:59

minus three and you can also go backward

play21:02

with like negative step Etc so all the

play21:04

stuff we've seen with this negative step

play21:06

going forward Etc with the list is also

play21:09

applicable to this array so novelty is

play21:11

you can use this like uh notation and

play21:14

you can use this double dot or select

play21:16

wool column uh line which was not

play21:20

possible with

play21:21

um numpy uh with just the list before so

play21:25

this is a bit of the power uh because it

play21:28

just look like Matrix

play21:30

in math speaking way as that's why we

play21:33

need these packages and why this

play21:34

packages is I think are quite practical

play21:37

uh then the next point is iterating so

play21:40

we remember before when we've seen the

play21:42

last we were able to iterate through

play21:45

list right so when I was into Richard

play21:47

fullest I will do for e in um and then I

play21:50

was having your last

play21:52

and I could do you know I print I plus

play21:55

one

play21:56

prints

play21:58

I plus one

play22:00

so if I do this you know I print my

play22:01

elements uh and now I can also iterate

play22:04

through my vect you know so I could do

play22:06

for e in vic of E

play22:11

20.

play22:12

so I'm going to start with that you know

play22:14

because vac is quite easy yeah I want to

play22:17

print it

play22:18

so I'm starting in Vegas if you do

play22:21

remember it is something like this so

play22:24

quite easy know what happened if I'm

play22:26

um

play22:28

if I'm looping in something that is

play22:31

um with a shape you know it's not shape

play22:33

so when you're looking uh through VEC of

play22:36

e simply you will Loop through the line

play22:39

so you will look through this element

play22:41

this element this element is elements if

play22:43

you want to Loop through the column you

play22:45

know you will do a for G in I and then

play22:49

you will print here so if you do this

play22:52

then uh you will Loop you know you print

play22:55

uh EEG

play22:59

event

play23:03

so here you see you go through your

play23:04

first Vector you get one second Vector

play23:07

you get one you get to two three two two

play23:09

three three four five this is e and your

play23:12

G is four so here you are able with this

play23:14

G Loop you know all the value of G will

play23:16

be the value

play23:18

um that you get in your um in your in

play23:21

your in your array uh then the opposite

play23:24

Ed you know you can also use enumerate

play23:28

so we should use the numerate in the

play23:30

coffee this is going to be quite nice

play23:32

because then we can print e and G

play23:36

and then we just do print ENT

play23:39

so the enumerate uh if I do print EEG is

play23:43

something that do zero one two three

play23:44

four five six seven so here I have my

play23:46

line you know so every time I will be

play23:49

able to do print Vic

play23:52

e and then my I

play23:55

so if I do this you know I'm putting my

play23:57

e and my G and then I'm printing uh my

play24:00

first line then I'm printing my second

play24:02

line only accessing with the with the G

play24:04

here so the G will be my line and then

play24:08

this I will be the index so this is B

play24:10

first line this with the I it equal to

play24:12

one second line this is I equal to two

play24:15

third line this is I equal to uh three

play24:19

so fourth line six and seven

play24:21

fourth line one two three four six and

play24:24

seven so that's how it is working you

play24:26

know so you have lots of different

play24:27

possibilities you can also do some zip

play24:29

or as well but I think enumerates with

play24:32

like enumerate

play24:34

combined with the slicing and getting

play24:36

the value uh then it will be less

play24:39

efficient to be able to like Loop in all

play24:42

of your arrays and then you can also uh

play24:45

if you want every second element to Loop

play24:47

in like every second element or you know

play24:49

something like that

play24:50

and then you will only go in a one out

play24:52

of two line you know so we'll go to the

play24:54

first line Second Line the fourth line

play24:55

so you see like the possibility are

play24:58

endless for different combination so I

play25:01

would invite you to try to do this by

play25:03

yourself uh so you get more of a feeling

play25:06

of uh how it is working uh what is its

play25:09

elements because if I'm just showing it

play25:11

to you on your passive it's quite

play25:13

difficult to learn by yourself but just

play25:16

pause the video take a minute do this

play25:18

simple stuff it shouldn't take you so

play25:20

much time but just so you're like okay I

play25:22

see what I'm doing

play25:24

foreign

play25:26

[Music]

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
NumPy TutorialData SciencePython LibraryArray ManipulationCoding BasicsData StructuresTechnical LearningMachine LearningLinear RegressionMath Functions