10 NumPy Tips and Tricks You Should Know!
8:19

10 NumPy Tips and Tricks You Should Know!

AssemblyAI 20.08.2022 3 717 просмотров 153 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
10 NumPy Tips and Tricks You Should Know! Get your Free Token for AssemblyAI Speech-To-Text API 👇 https://www.assemblyai.com/?utm_source=youtube&utm_medium=referral&utm_campaign=yt_pat_57 ▬▬▬▬▬▬▬▬▬▬▬▬ CONNECT ▬▬▬▬▬▬▬▬▬▬▬▬ 🖥️ Website: https://www.assemblyai.com 🐦 Twitter: https://twitter.com/AssemblyAI 🦾 Discord: https://discord.gg/Cd8MyVJAXd ▶️ Subscribe: https://www.youtube.com/c/AssemblyAI?sub_confirmation=1 🔥 We're hiring! Check our open roles: https://www.assemblyai.com/careers ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ Timeline: 00:00 Intro 00:30 Broadcasting 01:46 Comparison operator 02:40 Boolean Indexing 03:22 Tilde 03:43 Fancy Indexing 04:48 Sort 2 arrays 05:31 Change row/column positions 06:18 Get unique values 06:38 Check if 2 arrays are equal 07:20 ufunc.at #Python #numpy

Оглавление (11 сегментов)

Intro

hi everyone it's patrick from assembly ai in this video i show you 10 numpy tips and tricks to improve your performance so without further ado let's get started right now so before we start with any of the tips i want you to remember one concept we want to avoid for loops as much as possible with numpy because for loops are slow and numpy has much faster ways to do this in a vectorized way so most of the tips i show you now revolve around modifying reshaping and extracting data from numpy arrays in a vectorized way let's start the first tip is broadcasting and for

Broadcasting

this we have to understand that numpy operations are performed element wise for example if we multiply two arrays with each other then we see that one is multiplied by two by zero and three by two so in theory they should have the same shape but if we multiply an array with only a number then this also works so here we see that each element is multiplied by one hundred so this already applies broadcasting under the hood which means that the smaller array or in this case only one value is broadcast across the larger array so that the shapes again match so in theory this would look like 100 100 and this is an important concept that we should understand this might get clearer if we look at a second example in this example we want to add these values to all the rows of this array so in theory it should have this shape but we can only use the smaller version with only one row and then this is broadcast so it is extended to all the other rows and if we run this then we see this produces the same result so this is an important concept that we should understand the

Comparison operator

second tip is to use comparisons like equal greater or smaller for numpy arrays so if we apply this to a python list in this case the equal operator then we see we get only one value true or false but if we apply equal to two numpy arrays then as i said this is performed element wise so we get an array of the same shape and here it compared each of these elements one by one so we see a use case for this in the next tip but by the way we can also apply this to only one value then again this applies broadcasting under the hood so now each element is compared with a2 and one cool tip if we want to get only the indices of the true values we can use numpy flat non-zero and then we see we get index one so here and index seven here so let's see for what we can use this the third tip is boolean

Boolean Indexing

indexing this is a technique to extract only values where a condition is true so we've just seen these comparison operators in this case we check a greater two and then we get an array of the same shape where we compared each element and checked if it's greater than two and now we can use this as an index so we can say a and then access this boolean indices and now if we run this we get only the values that are greater than two and we can of course do this in only one line so this is much shorter we can say a and then access a greater two and then we get only these values so this is a cool tip to extract only the values that we want the next tip is to

Tilde

use the tilt operator as negation operator so this is the same example as before but now we apply the tilt operator in the indexing and then we get the opposite so here we get only the values one and two of course here we could have set a smaller than two as condition but it's still nice to know about this tilt operator if we want the

Fancy Indexing

opposite the next tip is called fancy indexing where we can access multiple indices at once so we've just seen boolean indexing where we used an array of the same shape with values true or false and now as an index we use a list or a numpy array with numbers so these are the indices we want to extract and if we run this we see we get the value at index 1 3 and 5. and this is another way to extract only the values that we want and as a use case we can again combine this with a condition in this case we want to extract only the even indices by saying a modulo 2 equals zero and then we can use the numpy flat non-zero method that i've already showed you or another way of doing this is to say numpy arc where and then call flatten and then we can print this and then we can use this as an index and then we get only the even values so here these are the indices where we have even values and these are the even values yeah so this is how fancy indexing works the next tip shows

Sort 2 arrays

you how to sort two arrays with the same rule with arcsort and fancy indexing so let's say we have some scores and then we have corresponding names and now we want to sort the names according to the scores in increasing order from lowest to highest for this we can use the built in arc sort function and apply it to the scores this returns the indices that would sort this array so if we apply the indices to the scores with fancy indexing then this gives us the scores in increasing order and then we can apply the same indices to the names and then we get the names in the corresponding order also from lowest to highest the next tip shows you how you

Change row/column positions

can use a list to change the positions of rows or columns again with fancy indexing combined with slicing so here we have this two dimensional array and then we define the new position indices as list and then we access it like this so we say a and access the new position indices for the first dimension so here we change the rows and then we say comma colon to keep all the columns and then if we run this then this reorders the rows and in a similar way we do this for the columns so here we say colon first and then comma and then the new indices for the new columns and then if we save and run this then we see we now reordered the columns the next

Get unique values

tip is a handy function to get the unique values of an array so we can say numpy unique and give it the array and this will return the unique values in a sorted order and this is for example helpful in a classification problem with multiple classes so these are our target classes and then we want to see only the unique class labels with the next tip

Check if 2 arrays are equal

you can check if two arrays are equal within a tolerance this is for example very helpful when we compare arrays with prediction values for a machine learning models for example here we have the actual labels and then the predictions and then there's a small difference so a slight floating point error that we can ignore but if we use equal we've seen this before this will compare it one by one and it's also not ignoring this small difference so instead we can use numpy all close and now in this case this gives us true because it's ignoring this tolerance and of course we can specify the tolerance as an additional parameter so yeah this is super helpful to check if two arrays are equal and the last tip

ufunc.at

is an expert tip with a function that not a lot of people know about and this is ufunk at with this we can perform unbuffered in place operations and ufunc can be many different functions for example it can be at and then we give it the array then the indices where we want to apply this and then the value we want to add so in this case it adds 100 at index 0 then at index one and then two times at index two so this is the result a different function would be for example numpy negative at and then again the array and the indices so this will give us negative values at index 0 and at index 1. so this is a super cool tip to do in place operations very fast alright so that wraps up our 10 numpy tips and tricks let me know in the comments what's your favorite tip if you enjoyed the video please leave us a thumbs up and consider subscribing to our channel for more python and machine learning content and then i hope to see you in the next video bye

Другие видео автора — AssemblyAI

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник