Intro To Numpy - Numpy For Machine Learning 1
Summary
TLDRIn this video, John Elder from CodingMe.com introduces the basics of NumPy, a fundamental library for machine learning and data science. He explains the limitations of Python lists for handling large datasets and how NumPy arrays solve these issues by offering a more efficient, homogeneous data structure. John demonstrates how to create and manipulate NumPy arrays, covering basic functions such as `shape`, `arange`, `zeros`, and `full`. He also outlines the importance of learning these foundational skills for progressing into more advanced topics like pandas and machine learning algorithms.
Takeaways
- đ Numpy is fundamental to machine learning and data science, helping manage large data sets efficiently.
- đĄ Python lists are versatile but become inefficient with large data sets due to varying data types and memory usage.
- đ Numpy arrays are like Python lists but optimized for performance, especially with large, numerical datasets.
- đŠ Numpy arrays require all elements to be of the same data type, making them more efficient for machine learning tasks.
- đą Numpy stands for 'Numeric Python,' and its arrays are called 'ndarrays,' representing n-dimensional arrays.
- đ» To use Numpy, you must install it via pip (`pip install numpy`) and import it as `import numpy as np`.
- đ§ You can create arrays with functions like `np.array`, `np.arange`, `np.zeros`, and `np.full` for different types of data initialization.
- 𧟠Numpy arrays support multi-dimensional data structures, useful for more complex data analysis in machine learning.
- ⥠Numpy arrays offer various mathematical operations and functions for data manipulation, like `shape` to get array size.
- đ Converting Python lists to Numpy arrays is straightforward using `np.array()`.
Q & A
What is NumPy and why is it important for machine learning?
-NumPy is a powerful library in Python used for working with arrays, which is fundamental for handling large datasets in machine learning. It's designed for numerical operations and is optimized for efficiency, making it essential when dealing with large amounts of data.
Why are Python lists not ideal for machine learning tasks?
-Python lists can store different data types, which can lead to inefficiency when working with large datasets. Lists are computationally expensive in terms of memory and processing speed, especially when handling millions or billions of data points, which is common in machine learning.
How does a NumPy array differ from a Python list?
-Unlike Python lists, NumPy arrays require all elements to be of the same data type, which allows for more efficient processing. NumPy arrays are optimized for numerical operations and can handle multi-dimensional data.
How can you create a NumPy array from a Python list?
-You can convert a Python list to a NumPy array by using the `numpy.array()` function and passing the list as an argument. For example: `np.array([1, 2, 3])` converts a Python list to a NumPy array.
What is the function of the `np.shape` method in NumPy?
-The `np.shape` method in NumPy is used to return the dimensions of the array. It provides information on how many elements exist in each dimension of the array.
How can you generate an array of zeros in NumPy?
-To generate an array filled with zeros in NumPy, you can use the `np.zeros()` function, passing the number of elements or dimensions as an argument. For example, `np.zeros(10)` creates a one-dimensional array with 10 zeros.
What does the `np.arange()` function do?
-The `np.arange()` function creates a NumPy array with a sequence of numbers within a specified range. You can also define the step size for the range. For example, `np.arange(0, 10, 2)` creates an array with values `[0, 2, 4, 6, 8]`.
What is an N-dimensional array in NumPy?
-An N-dimensional array (ndarray) in NumPy refers to an array that can have any number of dimensions, not limited to one or two. This is particularly useful for machine learning and data science, where multi-dimensional data is common.
How can you create multi-dimensional arrays in NumPy?
-You can create multi-dimensional arrays in NumPy by passing a tuple representing the dimensions to functions like `np.zeros()` or `np.full()`. For example, `np.zeros((2, 10))` creates a 2D array with two rows and 10 columns filled with zeros.
What are some basic functions provided by NumPy for array manipulation?
-NumPy provides many functions for array manipulation, such as `np.zeros()` to create arrays of zeros, `np.full()` to create arrays filled with a specific value, `np.shape` to get the dimensions, and `np.arange()` to create sequences of numbers.
Outlines
đĄ Introduction to NumPy for Machine Learning
In this video, John Alderhood from Codingman.com introduces NumPy, an essential library for machine learning and data science. He emphasizes the importance of understanding NumPy for handling large datasets efficiently, as lists in Python can be slow and memory-intensive. John also promotes Codingman.com courses and offers a 50% discount with a special coupon code.
đą Introduction to Python Lists and Their Limitations
John demonstrates how Python lists work, creating a simple list and explaining how they can contain different data types such as strings, numbers, and booleans. He explains the inefficiency of using lists for large datasets in machine learning due to their slow speed and high memory usage, which makes NumPy a better alternative.
⥠Transitioning to NumPy Arrays for Efficient Data Handling
John introduces NumPy arrays as a powerful alternative to Python lists, explaining that all elements in a NumPy array must be of the same data type. He shows how to install NumPy and create an array. He emphasizes that NumPy arrays are more efficient for handling large datasets, especially in machine learning, as they reduce computational overhead.
đ Exploring Basic NumPy Functions
In this section, John demonstrates basic functions in NumPy such as checking the shape of an array and creating arrays using the `arange()` and `zeros()` functions. He explains the utility of these functions in creating arrays with specific values or ranges, as well as the ability to generate multi-dimensional arrays.
đ Creating Multi-Dimensional Arrays in NumPy
John expands on multi-dimensional arrays, showing how to create arrays with multiple dimensions using the `zeros()` and `full()` functions. He demonstrates how to customize the dimensions and fill the arrays with specific values, illustrating the flexibility and power of NumPy for handling complex data structures.
đ Converting Python Lists to NumPy Arrays
John explains how to convert regular Python lists into NumPy arrays, making it easier to work with data in machine learning projects. He highlights that accessing elements from NumPy arrays works just like accessing elements from Python lists, making the transition between the two simple and intuitive.
đ Preview of Upcoming NumPy Tutorials
John wraps up by encouraging viewers to stick with the series, promising more advanced topics such as using Pandas and Scikit-learn. He emphasizes the importance of mastering NumPy for machine learning and encourages viewers to stay tuned for future videos that will build on these foundational concepts.
Mindmap
Keywords
đĄNumPy
đĄArray
đĄPython List
đĄMachine Learning
đĄComputational Efficiency
đĄData Science
đĄN-dimensional Array (ndarray)
đĄPip Install
đĄMemory Usage
đĄShape
Highlights
Introduction to using NumPy for machine learning, essential for handling large datasets.
Explanation of the importance of understanding NumPy for data science and machine learning.
Lists in Python can hold different data types, which becomes computationally expensive with large datasets.
NumPy arrays are more efficient for handling massive datasets as they enforce uniform data types.
NumPy arrays are faster and more memory-efficient compared to Python lists when dealing with large-scale data.
NumPy stands for Numeric Python and deals primarily with arrays.
The basic data type in NumPy is called 'ndarray,' which can have multiple dimensions.
Demonstration of creating basic NumPy arrays and how they differ from Python lists.
Explanation of how NumPy arrays only allow one data type (e.g., all numbers, booleans).
Introduction to NumPy's 'shape' function, which returns the number of elements in an array.
Using the 'arange' function in NumPy to create arrays with custom step sizes (e.g., 0, 2, 4...).
Creation of arrays filled with zeros using the 'zeros' function.
Demonstration of multi-dimensional arrays with NumPy.
Conversion of Python lists to NumPy arrays for optimized data handling.
Explanation of accessing elements from NumPy arrays, similar to Python lists.
Transcripts
what's going on guys john alderhood from
codingman.com and in this video we're
gonna look at numpy for machine learning
all right guys like i said in this video
we're gonna start to look at numpy for
machine learning before we get started
if you like this video want to see more
like it be sure to smash like button
below subscribe to the channel give me a
thumbs up for the youtube algorithm and
check out codingme.com we have dozens of
courses with thousands of videos that
teach you to code use coupon code
youtube50 to get 50 on memberships all
my courses videos of books one time fee
which is insanely cheap okay like i said
in this video we're going to start to
look at numpy and we're going to do a
series of videos on numpy here because
numpy is fundamental to machine learning
data science all that good stuff and a
lot of times people kind of just gloss
over it you kind of think you know how
to use it you use it for a few things
but you don't really know what you're
doing with it or whatever if you want to
get into machine learning or data
science or anything like that it's a
really good idea to have a solid
understanding of numpy and that's we're
going to look at in this series of
videos so in this video i just want to
start to talk about what is numpy what
do you use it for start to wrap our
brains around exactly what it's all
about so numpy very simply is just a
special type of array in python and we
have arrays or lists as python calls
them so let's head over to our code i'm
using the sublime text editor and the
git bash terminal as always and as
always you can find a link to the code
in the pin comment section below as well
as a link to the playlist with all the
other numpy videos in this series so
check that out if you haven't so far so
i've got a file i'm just calling it
intro.pi so let's come up here and let's
just create a basic python list i'm
going to call it list one right and we
know python lists we can go you know
something like that so if we wanted to
then print out this list very simply we
could do that save this head over to our
terminal in my c num directory i just
created a directory called num and let's
just run python intro.pi and it just
prints out the python list so we already
know all about this we wanted to print
out you know an item from this list
we could say the first item list start
at zero that would print out one if we
came back over here ran this guy again
it'd print out one right so that's just
your basic python list now that's cool
that's great for a lot of things
but we can run into some problems using
lists for large sets of data and the
reason why
is because lists can have lots of
different data types in them so we can
have you know john elder we can have a
number 41 we can have another list we
can have booleans true false whatever so
if we print this guy list two you know
we know what's going to happen here come
back over here
you know we've got strings we've got
numbers we've got other lists we've got
booleans and now this is really cool a
lot of programming languages won't allow
you to do this in an array but python
does with lists and that's great but
it's a problem when you start using
massive amounts of data and machine
learning is all about using massive
amounts of data so the reason why it's a
problem is because doing it like this is
very slow it's computationally expensive
right it costs a lot of memory and all
that stuff to deal with these lists now
when we're dealing with a little bitty
list like this it's no big deal but when
you're dealing with lists with millions
of records or billions of records
terabytes of records it becomes a lot
harder to do it comes a lot slower takes
a lot more time takes a lot more
computational juju and all of that stuff
so
instead of using a list for data science
and machine learning we use something
called a numpy array and a numpy array
is just a list
but it's different right it's more
powerful it has cooler things so we can
come over here and we can import numpy
so let's go import
numpy
and the convention is to import it as np
and you'll see why in just a second now
this does not come with python we need
to install it so let's head back over to
our terminal
and let's go pip
install numpy
and i've already got it so it's saying
hey you've already got it but it will
install on your computer and that's all
there is to it you're good to go
so now let's head back over here and let
me just kind of
comment out that stuff here and numpy if
you haven't figured it out yet stands
for numeric python
right we're creating numpy arrays which
are just like lists only they're
different couple of things to keep in
mind
anything in a numpy array has to be of
the same data type right so they have to
be all numbers or they have to be all
booleans or they have to be all whatever
they all have to be the same data type
which is usually okay because in machine
learning you're often dealing with
numbers right so the data type of a
numpy is an nd array and that stands for
in
dimensional
array so you know lists can have
multi-dimensions so can numpy arrays so
you might have one dimension you might
have two dimensions you might have three
dimensions you might have 100 dimensions
you have n dimensions like in math when
you don't know how many use n so n
dimensional arrays that's a numpy array
so let's just go ahead and create one of
these real quick let's go i'm just going
to call it mp1 short for numpy one i
guess we can set this equal to a n p dot
array and then inside of here we can
create our numpy array now notice this
np that's because we imported this as np
so we have to sort of slap an mp in
front of everything we want to create so
we want to create a numpy array
this is how we do it now inside of here
lots of different ways you can do this
we can go you know zero one two three
four five six seven
eight
nine
remember all arrays whether numpy or
list or whatever they always start at
zero so this is the zeroth item one two
three four we have 10 items in here so
if we want to print this out see what it
looks like in p1 we can head back over
here run this guy again and we can see
it looks just like a regular python list
so don't get weird about numpy arrays
they're really just basically lists only
they're more powerful now numpy itself
doesn't just have arrays it has a whole
sort of suite of math functions that you
can do stuff to those arrays as well as
other types of functions so let's spend
the rest of this video just kind of
familiarizing ourselves with some very
basic sort of functions and things that
come with numpy so first of all we can
see the shape of our array so we can
call mp1 dot shape right and this will
tell us basically the number of elements
in our array think of it as the lend
function in python sort of right so if
we do this print this guy real quick
do that come back over here we see
there's 10 items in this array and we
know that's true one two three four five
six seven eight nine
ten right ten items so that's always
interesting to be able to do so that's
cool so there's lots of different ways
you can create numpy arrays we can use
the a range function if we don't wanna
you know explicitly do something like
this we could go np
dot a range and we could say 10 right so
here if we print this guy and p2
save this run
it we get the same thing we get a numpy
array with 10 items in it just from zero
to nine and that's cool well this is
from zero to nine zero one two three we
can tell this to step instead of doing
every one like zero one two three we
could say hey do it every two so two
four six or every three three six nine
whatever we want so we could just sort
of modify this a little bit now let's
see
range here here let's say step so we go
mp3
equals an mp
dot a range we could say from zero to
ten but step
two right so if we want to print this
guy
we can do that back over here
run the sky again and here we see we've
created an umpire array with steps of
two so zero two four six eight right so
that's helpful
we can create an array of zeros right so
let's go np4
equals
mp.zeros use the zeros function
and again let's say we want 10 items
right so we can print this guy and p4
save this head back over here run this
guy again so now you see we have ten
zeros and they're actually zero point so
it's not strictly an integer it's
zero point this is not a separating
factor it's not like a comma just think
of these as zero point zero basically
right so that's cool we're dealing with
one-dimensional arrays here right you
can do multi-dimensional arrays just as
easily and numpy is great at
multi-dimensional so let's go
multi-dimensional
zeros all right stick with the zero
theme here so we go np dot zeros
and use double parenthesis here so we
start out how many dimensions do we want
let's say
two dimensions and how many items in
each dimension let's say 10 right stick
with the 10. and so let's print out this
guy
save this run it
you can see here we have two dimensions
right here's the first one there's ten
zeros in it here's the second one ten
zeros in it you can see they're
separated just by their brackets right
pretty cool pretty easy say we don't
want to use zeros right
we can use the full function so let's go
in p6 equals np.full
all right so let's say we want 10 items
and what do we want them to be well we
don't want them to be zeros let's have
them be sixes right or whatever right so
we can print np six save this guy run it
and you can see now we have
an array of sixes right
very cool we could do multi-dimensional
full so let's go mp7 equals mp.full
and let's say we want two dimensions
with 10 elements in each dimension and
again let's say we want to fill it with
sixes right so let's print
np7 save this guy head back over here
run it again
and here we have two dimensions of sixes
right there's the first one there's the
second one so just a few functions to
play around with just to sort of
familiarize ourselves with numpy we can
actually convert
python lists
to numpy
so let's create a list i don't know my
list
set that equal to a python list of one
two three four five whatever now if we
wanted to convert that into a numpy
array we could let's go mp8
equals and np dot array and all we have
to do is just pass in my list
numpy will convert it for us and if we
print out
np8
we can see
it's going to look just like a regular
list but rest sure that has been
converted to a numpy
array so accessing specific elements out
of a numpy array is exactly the same as
accessing any element out of any python
list so we could print np8 and let's say
we want the zeroth item we just pop in a
zero there and that will print out one
because that's the zeroth item we save
this and run it we see sure enough it
just prints out one so this is just a
very brief introduction to numpy i don't
want to overwhelm you in this first
video there's all kinds of cool things
you could do with numpy specifically for
machine learning and we're going to
start to learn all about all of those
things in the coming videos in this
playlist so if you guys are interested
in this let me know i'll continue on and
my goal is to start to learn more about
numpy and then sort of progress to other
machine learning things like pandas
scikit-learn all that stuff and then
slowly build up a skill set for you guys
for basic machine learning things and
then we can get into algorithms and all
that cool stuff but we need the basics
first we need to understand this
you know kind of boring stuff numpy
arrays this is not exciting it's a
little bit boring but it could be kind
of fun too so stick with me on this
we'll learn this stuff we'll knock this
stuff out and then we'll move into more
interesting things but uh should be fun
so that's all for this video if you
liked it be sure to smash like button
below subscribe to the channel give me a
thumbs up for the youtube algorithm and
check out kodamy.com you can use coupon
code youtube50 to get 50 off membership
so that's access to all my courses over
50 courses thousands of videos and the
pdfs of all my best selling coding books
join over 150 000 students learn to
coach just like you my name is john
elder from codingme.com and i'll see the
next video
5.0 / 5 (0 votes)