numpy tutorial – slicing/stacking arrays, indexing with boolean arrays

only integer scalar arrays can be converted to a scalar index
This is a topic that many people are looking for. is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, would like to introduce to you numpy tutorial – slicing/stacking arrays, indexing with boolean arrays. Following along are instructions in the video below:

hello everyone this is a continuation of our numpy tutorials if you have not seen my first two tutorials then I would say pause right now go watch them first and then you can continue on this one heres the list of topics that we are going to cover in this tutorial okay lets first begin by indexing and slicing now you all know that when you have a normal Python list like this you can use the index range like this to get the list of elements so when you do n 0 to 2 what it will do is it will go from 0 to 1 so this is 0 1 2 it will not cover 2 but it will cover these two so thats what it does it also supports minus 1 index minus 1 means start from the end so it will print the very last element okay now num py array supports similar kind of slicing so here Im going to create num py array I will initialize it with same elements and if you do 0 to 2 here you see you get the same result as what you got with a normal Python list okay it also supports reverse index so this is the syntax is pretty similar to lists how you slice the list so if you know list slicing then you can use the same concept for number Y array as well now lets go through a multi-dimensional array so here lets say I have a multi-dimensional array so if you print it it looks like this now here when you do a 1/2 what will happen is you are going through the so here this is a row this is a column so this is zero through one row so you are going through this first row and you are looking at the second element so 0 1 2 so which is 3 okay so thats what it is printing when you do something like this what will happen is it will go through 0 2 second row ok now always when you are doing slicing the this particular index is not included so it will actually go from 0 to 1 so 0 through fostro and then it will print 2nd element so in these 2 rows the second element is 8 & 3 so thats what it is printing here you can also do a minus 1 oops so a minus 1 is the last element sebast element here is this last row so thats what it is printing and in that last row so always remember this is your row and after row you have called so lets say in column you want to print column 0 2 2 so 0 2 2 means 0 & 1 so its in the last row its printing 9 & 3 now lets say if you want to go through all the roll and print these 2 columns lets say you want to print 7 2 3 + 8 3 2 how will you do that so the syntax for going through all the rows is just a column so if you have just a column it will go through all the rows and then now you want to go through first row and second row so to include first you will say first now if I say second it will not include this guy here okay so I have to sit three here cook now lets see how you would I trade through an array so Im going to initialize the same error here and if you look at this array its sort of like a table you have rows in your columns now if you want to it–it through rows you will just say four row in E print row pretty straightforward okay and if you want to print in use your cells then again Row is also short of like a list so you can either through it and you can induce print in usual numbers okay now sometimes you want to flatten the list and print every cell so numpy why has this matter called flat you can do for cell in a dot flat frame cell what this is doing is it is just flattening this as as if its a single dimensional array and you can now I trait through the entire array next item is stacking two errors together so for example lets say you have these two arrays so Im what Im doing is Im initializing array with numbers 0 to 5 and Im reshaping it by 3 by 2 shape okay and then I have my B which is like this so if you print a and B it looks like this okay so you have got lets say two two-dimensional arrays now if you want to vertically stack them together just imagine you have put this array as if its a box and youre putting this over another box okay so num py supports that so in um py you will do NP dot V stack a and B okay I had a problem with the syntax you

the argument is double actually okay so you can see that it kind of stacked them together as if there were like two boxes and you just put one box over the other okay you can do same thing with horizontal stacking as well so lets say lets say these are two boxes and want to horizontally put them side-by-side you can do the same thing with a stack command which is horizontal stacking so you can see 0 1 3 so this was my first array and this is my second array this could be pretty useful feature based on what kind of requirement you have all right now what we are going to look at is the horizontal split split so I am again initializing one more array and this array is having two rows and 15 columns now if you want to split this array into lets say three different arrays so I want to just imagine I am vertically slicing them into three different equal sized arrays you can do that using at split command as split command is at numpy module it is not at individual array if you do that you can see that it kind of split into three different arrays now its kind of hard to visualize this so what Im going to do is Im going to store the result into a variable which is a list and just print those things one by one so you can see that now I kind of vertically slice these big array into three different areas by doing a split so three means divided into three different equal sized arrays okay now you can do the same thing with a vertical split here I can do a vertical split which is lets say I have only two rows so I could just divide it into two I can do make only two divisions out of it so if you do result 0 and result 1 they look like this so what I did is vertically I kind of cut them into two arrays so this the second argument tells you like how many partitions you want to make out of your original array which is here okay now another topic we want to cover is indexing with boolean arrays this is pretty powerful its kind of like non-intuitive if you have just used less then you will not never think that this kind of awesome feature exists in numpy Y so Im going to initialize one array and kind of reshape it two three four by the way this practice is quite popular whenever you want to initialize array with fixed set of numbers and then you want to kind of reshape it this is what you do this is better than kind of typing all these elements by hand you know so I kind of like it okay now I have got this number UI array all right now lets say I have this B variable and if I do a greater than four whats gonna happen is its gonna create another numpy array and its gonna store the result of this expression into individual elements so here 0 is less than 0 is not greater than 4 so its false whats this 11 is greater than 4 so at that location it is true okay so pretty cool I got now two arrays this one is numbers and this one is boolean now what you can do is something like this now you might not have seen this before okay so here this is array and the index of that array is array itself now lets see whats gonna happen when you do this okay cool so what it did is it looked at B array and then wherever it found true it returned those elements from this original array so you can see that 5 to 11 where true thats why you got 5 to 11 this is a cool way of extracting all the elements which are greater than 4 from your original array this is also useful if you want to replace those with a certain number so for example I can say e of B is equal to minus 1 and what will happen is any element that was greater than 4 you wanted to replace it with minus 1 and thats what it did so be it looks at all the true elements it returns those elements and all those elements are said to be minus 1 this is this could be really powerful based on what kind of application you are right okay so that was all about number Y or a functions I hope youre having fun time learning numpy why its pretty powerful numpy why arrays are very fast and memory efficient so if you are doing a lot of data manipulation and if you are looking for performance please use numpy why its very popular in data science and scientific community overall all right thank you for watching and if you like this video please give it a thumbs up if you have a question please post a comment below bye

numpy tutorial, numpy tutorial python, numpy tutorial for beginners, python numpy tutorial, python numpy, numpy array, numpy array tutorial, numpy array pyth…
Thank you for watching all the articles on the topic numpy tutorial – slicing/stacking arrays, indexing with boolean arrays. All shares of are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

Leave a Comment