Hang tight while we fetch the video data and transcripts. This only takes a moment.
Connecting to YouTube player…
Fetching transcript data…
We’ll display the transcript, summary, and all view options as soon as everything loads.
Next steps
Loading transcript tools…
16. Numpy 1D Arrays | Python Things | YouTubeToText
YouTube Transcript: 16. Numpy 1D Arrays
Skip watching entire videos - get the full transcript, search for keywords, and copy with one click.
Share:
Video Transcript
in this video we will be covering numpy
in 1d in particular nd arrays numpy is a
library for scientific computing it has
many useful functions there are many
other advantages like speed and memory
numpy is also the basis for pandas so
check out our pandas video in this video
we will be covering the basics and array
creation indexing and slicing basic
operations universal functions let's go
over how to create an umpire ray a
Python list is a container that allows
you to store and access data each
element is associated with an index we
can access each element using a square
bracket as follows a numpy array or ND
array is similar to a list it's usually
fixed in size and each element is of the
same type in this case integers we can
cast a list to a numpy array by first
importing numpy we then cast the list as
follows we can access the data via an
index as with the lists we can access
each element with an integer and a
square bracket the value of a is stored
as follows if we check the type of the
array we get num PI dot nd array as
numpy arrays contain data of the same
type we can use the attribute de type to
obtain the datatype of the arrays
elements in this case a 64-bit integer
let's review some basic array attributes
using the array a the attribute size is
the number of elements in the array as
there are five elements the result is 5
the next two attributes will make more
sense when we get to higher dimensions
but let's review them the attribute n
dim represents the number of array
dimensions or the rank of the array in
this case 1 the attribute shape is a
tuple of integers indicating the size of
the array in each dimension we can
create a numpy array with real numbers
when we check the type of the array we get
get
und array if we examine the attribute D
type we see float64 as the elements are
not integers there are many other attributes
attributes
check out numpy org let's review some
indexing and slicing methods we can
change the first element of the array to
a hundred as follows the arrays first
value is now a hundred we can change the
fifth element of the array as follows
the fifth element is now zero like lists
and tuples we can slice a numpy array
the elements of the array correspond to
the following index we can select the
elements from 1 to 3 and assign it to a
new numpy array d as follows the
elements in D correspond to the index
like lists we do not count the element
corresponding to the last index we can
assign the corresponding indices to new
values as follows the array C now has
new values see the labs or numpad org
for more examples of what you can do
with numpy numpy makes it easier to do
many operations that are commonly
performed in data science these same
operations are usually computationally
faster and require less memory in numpy
compared to regular Python let's review
some of these operations on
one-dimensional arrays we will look at
many of the operations in the context of
Euclidean vectors to make things more
interesting vector addition is a widely
used operation in data science consider
the vector U with two elements the
elements are distinguished by the
different colors similarly consider the
vector V with two components in vector
addition we create a new vector in this
case zette the first component of Zed is
the addition of the first component of
vectors U and V similarly the second
component is the sum of the second
components of U and V this new vector
Zed is now a linear combination of the
vector U and V representing vector
addition with line segments or arrows is
helpful the first vector is represented
in red the vector will point in the direction
direction
of the two components the first
component of the vector is one as a
result the arrow is offset one unit from
the origin in the horizontal direction
the second component is zero we
represent this component in the vertical
direction as this component is 0 the
vector does not point in the horizontal
direction we represent the second vector
in blue the first component is zero
therefore the error does not point to
the horizontal direction the second
component is 1 as a result the vector
points in the vertical direction 1 unit
when we add the vector U and V we get
the new vector Z we add the first
component this corresponds to the
horizontal direction we also add the
second component it's helpful to use the
tip to tail method when adding vectors
placing the tail of a vector V on the
tip of vector u the new vector Z is
constructed by connecting the base of
the first vector U with the tail of the
second V the following three lines of
code will add the two lists and place
the result in the list Zed we can also
perform vector addition with one line of
numpy code it would require multiple
lines to perform vector subtraction on
two lists as shown on the right side of
the screen in addition the numpy code
will run much faster this is important
if you have lots of data we can also
perform vector subtraction by changing
the addition sign to a subtraction sign
it would require multiple lines to
perform vector subtraction on two lists
as shown on the right side of the screen
vector multiplication with a scalar is
another commonly performed operation
consider the vector Y each component is
specified by different color we simply
multiply the vector by a scalar value in
this case 2 each component of the vector
is multiplied by 2 in this case each
component is doubled we can use the line
segments or arrows to visualize what's
going on the original vector Y is in
purple after multiplying it by a scalar
value of to the vector is stretched out
by two units as shown in red
the new vector is twice as long in each
direction vector multiplication with a
scalar only requires one line of code
using them pi it would require multiple
lines to perform the same task as shown
with Python lists as shown on the right
side of the screen in addition the
operation would also be much slower
Hadamard product is another widely used
operation in data science consider the
following two vectors U and V the
Hadamard product of u and v is a new
vector z the first component of z is the
product of the first element of U and V
similarly the second component is the
product of the second element of U and V
the resultant vector consists of the
entry wise product of U and V we can
also perform Hadamard product with one
line of code in numpy it would require
multiple lines to perform how to mark
product on two lists as shown on the
right side of the screen the dot product
is another widely used operation in data
science consider the vector U and V the
dot product is a single number given by
the following term and represents how
similar two vectors are we multiply the
first component from V and U we then
multiply the second component and add
the result together the result is a
number that represents how similar the
two vectors are we can also perform dot
product using the numpy function dot and
assign it with the variable result as
follows consider the array u the array
contains the following elements if we
add a scalar value to the array numpy
will add that value to each element this
property is known as broadcasting a
universal function is a function that
operates on nd arrays we can apply a
universal function to a numpy array
consider the arrays a we can calculate
the mean or average value of all the
elements in a using the method mean this
corresponds to the average of all the
elements in this case the result is zero
there are many other functions for
example consider the numpy arrays
we can find the maximum value using the
method five we see the largest value is
five therefore the method max returns a
five we can use numpy to create
functions that map numpy raised to new
numpy arrays let's implement some code
on the left side of the screen and use
the right side of the screen to
demonstrate what's going on we can
access the value of pi in numpy as
follows we can create the following
numpy array in radians this array
corresponds to the following vector we
can apply the function sine to the array
X and assign the values to the array Y
this applies the sine function to each
element in the array this corresponds to
applying the sine function to each
component of the vector the result is a
new array Y where each value corresponds
to a sine function being applied to each
element in the array X a useful function
for plotting mathematical functions is
line space line space returns evenly
spaced numbers over specified interval
we specify the starting point of the
sequence the ending point of the
sequence the parameter num indicates the
number of samples to generate in this
case 5 the space between samples is 1 if
we change the parameter num to 9 we get
9 evenly spaced numbers over the
interval from negative 2 to 2 the result
is the difference between subsequent
samples is 0.5 as opposed to 1 as before
we can use the function line space to
generate 100 evenly spaced samples from
the interval 0 to 2 pi we can use the
numpy function sign to map the array X
to a new array Y we can import the
library PI plot as P LT to help us plot
the function as we are using a Jupiter
notebook we use the command mat plot Lib
in line to display the plot the
following command plots a graph the
first input corresponds to the values
for the horizontal or x-axis the second input
input
responds to the values for the vertical
or y-axis there's a lot more you can do
with numpy check out the labs numpy org
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.