Intro To Numpy - Numpy For Machine Learning 1

Codemy.com
7 Jun 202212:31

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

00:00

💡 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.

05:01

🔢 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.

10:03

⚡ 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

NumPy is a library in Python used for numerical computations. In the video, the speaker emphasizes its importance for machine learning and data science because of its ability to handle large datasets efficiently. NumPy arrays are the core data structure, and the speaker explains how they are more powerful than Python lists due to their homogeneous nature and optimized performance for numerical tasks.

💡Array

An array in NumPy is a collection of elements, all of the same type, arranged in a grid-like format. The speaker compares arrays with Python lists, highlighting that arrays are more efficient for handling large datasets because they require less memory and perform computations faster. Arrays are fundamental in NumPy and machine learning, particularly when working with large amounts of data.

💡Python List

A Python list is a built-in data structure that can hold multiple data types, including strings, numbers, and other lists. The video contrasts Python lists with NumPy arrays, explaining that lists can be inefficient for large datasets because they allow mixed data types, which increases computational complexity and memory usage. The flexibility of lists becomes a disadvantage when dealing with massive amounts of data in machine learning.

💡Machine Learning

Machine learning is a field of study that focuses on using algorithms to allow computers to learn from and make predictions on data. The video introduces NumPy as a foundational tool for machine learning, as it is used to process and manipulate the vast amounts of numerical data that machine learning models rely on. Efficient data handling through NumPy arrays is critical in this domain.

💡Computational Efficiency

Computational efficiency refers to how quickly and effectively a program or algorithm performs a task. The speaker highlights how NumPy arrays are much more computationally efficient than Python lists, particularly when working with large datasets in machine learning. This efficiency is vital because machine learning models often involve processing millions or billions of data points.

💡Data Science

Data science involves analyzing and interpreting complex data to extract meaningful insights. NumPy is a key tool in data science because it provides fast, efficient array manipulations, which are essential when working with large datasets. In the video, the speaker emphasizes that understanding NumPy is crucial for anyone interested in pursuing data science.

💡N-dimensional Array (ndarray)

An N-dimensional array, or ndarray, is the core data structure of NumPy, allowing data to be stored in multiple dimensions (e.g., 1D, 2D, 3D, etc.). The speaker introduces this concept as being central to NumPy and explains that these arrays are powerful for machine learning and data science applications that require handling multi-dimensional datasets.

💡Pip Install

Pip is a package manager for Python that allows users to install and manage software packages. The video demonstrates how to install NumPy using the command 'pip install numpy,' which is necessary because NumPy does not come pre-installed with Python. This step ensures that users have the tools needed to work with large datasets in machine learning.

💡Memory Usage

Memory usage refers to the amount of RAM a program consumes while processing data. The speaker notes that NumPy arrays are more memory-efficient than Python lists because they store data in a more compact form. This efficiency is particularly important in machine learning, where datasets can be massive, and reducing memory usage can significantly speed up computations.

💡Shape

In NumPy, the shape of an array refers to its dimensions (e.g., the number of rows and columns in a 2D array). The video explains that using the 'shape' attribute allows users to understand the structure of an array. This is useful for machine learning, where data often comes in multi-dimensional forms, and understanding the shape is essential for properly processing the data.

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

play00:00

what's going on guys john alderhood from

play00:01

codingman.com and in this video we're

play00:03

gonna look at numpy for machine learning

play00:08

all right guys like i said in this video

play00:09

we're gonna start to look at numpy for

play00:11

machine learning before we get started

play00:12

if you like this video want to see more

play00:13

like it be sure to smash like button

play00:15

below subscribe to the channel give me a

play00:16

thumbs up for the youtube algorithm and

play00:18

check out codingme.com we have dozens of

play00:19

courses with thousands of videos that

play00:20

teach you to code use coupon code

play00:22

youtube50 to get 50 on memberships all

play00:24

my courses videos of books one time fee

play00:26

which is insanely cheap okay like i said

play00:27

in this video we're going to start to

play00:29

look at numpy and we're going to do a

play00:30

series of videos on numpy here because

play00:33

numpy is fundamental to machine learning

play00:36

data science all that good stuff and a

play00:38

lot of times people kind of just gloss

play00:40

over it you kind of think you know how

play00:42

to use it you use it for a few things

play00:44

but you don't really know what you're

play00:45

doing with it or whatever if you want to

play00:48

get into machine learning or data

play00:49

science or anything like that it's a

play00:51

really good idea to have a solid

play00:53

understanding of numpy and that's we're

play00:54

going to look at in this series of

play00:56

videos so in this video i just want to

play00:58

start to talk about what is numpy what

play01:00

do you use it for start to wrap our

play01:02

brains around exactly what it's all

play01:04

about so numpy very simply is just a

play01:07

special type of array in python and we

play01:10

have arrays or lists as python calls

play01:12

them so let's head over to our code i'm

play01:13

using the sublime text editor and the

play01:15

git bash terminal as always and as

play01:16

always you can find a link to the code

play01:17

in the pin comment section below as well

play01:19

as a link to the playlist with all the

play01:20

other numpy videos in this series so

play01:22

check that out if you haven't so far so

play01:24

i've got a file i'm just calling it

play01:25

intro.pi so let's come up here and let's

play01:27

just create a basic python list i'm

play01:30

going to call it list one right and we

play01:32

know python lists we can go you know

play01:34

something like that so if we wanted to

play01:36

then print out this list very simply we

play01:39

could do that save this head over to our

play01:40

terminal in my c num directory i just

play01:43

created a directory called num and let's

play01:45

just run python intro.pi and it just

play01:48

prints out the python list so we already

play01:50

know all about this we wanted to print

play01:51

out you know an item from this list

play01:55

we could say the first item list start

play01:57

at zero that would print out one if we

play01:59

came back over here ran this guy again

play02:02

it'd print out one right so that's just

play02:04

your basic python list now that's cool

play02:06

that's great for a lot of things

play02:08

but we can run into some problems using

play02:11

lists for large sets of data and the

play02:14

reason why

play02:16

is because lists can have lots of

play02:17

different data types in them so we can

play02:19

have you know john elder we can have a

play02:22

number 41 we can have another list we

play02:26

can have booleans true false whatever so

play02:28

if we print this guy list two you know

play02:31

we know what's going to happen here come

play02:32

back over here

play02:34

you know we've got strings we've got

play02:36

numbers we've got other lists we've got

play02:38

booleans and now this is really cool a

play02:40

lot of programming languages won't allow

play02:42

you to do this in an array but python

play02:44

does with lists and that's great but

play02:46

it's a problem when you start using

play02:48

massive amounts of data and machine

play02:50

learning is all about using massive

play02:52

amounts of data so the reason why it's a

play02:54

problem is because doing it like this is

play02:57

very slow it's computationally expensive

play03:01

right it costs a lot of memory and all

play03:03

that stuff to deal with these lists now

play03:06

when we're dealing with a little bitty

play03:08

list like this it's no big deal but when

play03:10

you're dealing with lists with millions

play03:12

of records or billions of records

play03:14

terabytes of records it becomes a lot

play03:17

harder to do it comes a lot slower takes

play03:20

a lot more time takes a lot more

play03:21

computational juju and all of that stuff

play03:24

so

play03:25

instead of using a list for data science

play03:27

and machine learning we use something

play03:28

called a numpy array and a numpy array

play03:30

is just a list

play03:32

but it's different right it's more

play03:34

powerful it has cooler things so we can

play03:36

come over here and we can import numpy

play03:38

so let's go import

play03:39

numpy

play03:41

and the convention is to import it as np

play03:43

and you'll see why in just a second now

play03:45

this does not come with python we need

play03:47

to install it so let's head back over to

play03:48

our terminal

play03:50

and let's go pip

play03:52

install numpy

play03:54

and i've already got it so it's saying

play03:55

hey you've already got it but it will

play03:57

install on your computer and that's all

play03:58

there is to it you're good to go

play04:00

so now let's head back over here and let

play04:02

me just kind of

play04:06

comment out that stuff here and numpy if

play04:08

you haven't figured it out yet stands

play04:10

for numeric python

play04:12

right we're creating numpy arrays which

play04:15

are just like lists only they're

play04:16

different couple of things to keep in

play04:17

mind

play04:18

anything in a numpy array has to be of

play04:20

the same data type right so they have to

play04:22

be all numbers or they have to be all

play04:24

booleans or they have to be all whatever

play04:25

they all have to be the same data type

play04:27

which is usually okay because in machine

play04:29

learning you're often dealing with

play04:30

numbers right so the data type of a

play04:33

numpy is an nd array and that stands for

play04:37

in

play04:38

dimensional

play04:40

array so you know lists can have

play04:42

multi-dimensions so can numpy arrays so

play04:45

you might have one dimension you might

play04:46

have two dimensions you might have three

play04:48

dimensions you might have 100 dimensions

play04:50

you have n dimensions like in math when

play04:52

you don't know how many use n so n

play04:55

dimensional arrays that's a numpy array

play04:57

so let's just go ahead and create one of

play04:59

these real quick let's go i'm just going

play05:00

to call it mp1 short for numpy one i

play05:03

guess we can set this equal to a n p dot

play05:06

array and then inside of here we can

play05:08

create our numpy array now notice this

play05:10

np that's because we imported this as np

play05:14

so we have to sort of slap an mp in

play05:16

front of everything we want to create so

play05:18

we want to create a numpy array

play05:20

this is how we do it now inside of here

play05:22

lots of different ways you can do this

play05:23

we can go you know zero one two three

play05:26

four five six seven

play05:28

eight

play05:29

nine

play05:30

remember all arrays whether numpy or

play05:33

list or whatever they always start at

play05:35

zero so this is the zeroth item one two

play05:38

three four we have 10 items in here so

play05:40

if we want to print this out see what it

play05:41

looks like in p1 we can head back over

play05:44

here run this guy again and we can see

play05:46

it looks just like a regular python list

play05:48

so don't get weird about numpy arrays

play05:50

they're really just basically lists only

play05:52

they're more powerful now numpy itself

play05:54

doesn't just have arrays it has a whole

play05:57

sort of suite of math functions that you

play05:59

can do stuff to those arrays as well as

play06:01

other types of functions so let's spend

play06:03

the rest of this video just kind of

play06:04

familiarizing ourselves with some very

play06:06

basic sort of functions and things that

play06:08

come with numpy so first of all we can

play06:11

see the shape of our array so we can

play06:12

call mp1 dot shape right and this will

play06:16

tell us basically the number of elements

play06:18

in our array think of it as the lend

play06:20

function in python sort of right so if

play06:23

we do this print this guy real quick

play06:26

do that come back over here we see

play06:28

there's 10 items in this array and we

play06:30

know that's true one two three four five

play06:34

six seven eight nine

play06:36

ten right ten items so that's always

play06:39

interesting to be able to do so that's

play06:40

cool so there's lots of different ways

play06:42

you can create numpy arrays we can use

play06:44

the a range function if we don't wanna

play06:46

you know explicitly do something like

play06:48

this we could go np

play06:51

dot a range and we could say 10 right so

play06:54

here if we print this guy and p2

play06:58

save this run

play07:00

it we get the same thing we get a numpy

play07:04

array with 10 items in it just from zero

play07:06

to nine and that's cool well this is

play07:09

from zero to nine zero one two three we

play07:11

can tell this to step instead of doing

play07:13

every one like zero one two three we

play07:16

could say hey do it every two so two

play07:18

four six or every three three six nine

play07:20

whatever we want so we could just sort

play07:22

of modify this a little bit now let's

play07:24

see

play07:25

range here here let's say step so we go

play07:28

mp3

play07:29

equals an mp

play07:31

dot a range we could say from zero to

play07:34

ten but step

play07:36

two right so if we want to print this

play07:38

guy

play07:41

we can do that back over here

play07:45

run the sky again and here we see we've

play07:48

created an umpire array with steps of

play07:50

two so zero two four six eight right so

play07:53

that's helpful

play07:54

we can create an array of zeros right so

play07:57

let's go np4

play07:59

equals

play08:00

mp.zeros use the zeros function

play08:03

and again let's say we want 10 items

play08:06

right so we can print this guy and p4

play08:09

save this head back over here run this

play08:11

guy again so now you see we have ten

play08:13

zeros and they're actually zero point so

play08:16

it's not strictly an integer it's

play08:18

zero point this is not a separating

play08:21

factor it's not like a comma just think

play08:23

of these as zero point zero basically

play08:26

right so that's cool we're dealing with

play08:28

one-dimensional arrays here right you

play08:30

can do multi-dimensional arrays just as

play08:32

easily and numpy is great at

play08:34

multi-dimensional so let's go

play08:36

multi-dimensional

play08:38

zeros all right stick with the zero

play08:41

theme here so we go np dot zeros

play08:45

and use double parenthesis here so we

play08:46

start out how many dimensions do we want

play08:48

let's say

play08:49

two dimensions and how many items in

play08:52

each dimension let's say 10 right stick

play08:54

with the 10. and so let's print out this

play08:56

guy

play08:58

save this run it

play09:02

you can see here we have two dimensions

play09:05

right here's the first one there's ten

play09:08

zeros in it here's the second one ten

play09:10

zeros in it you can see they're

play09:12

separated just by their brackets right

play09:15

pretty cool pretty easy say we don't

play09:17

want to use zeros right

play09:20

we can use the full function so let's go

play09:22

in p6 equals np.full

play09:26

all right so let's say we want 10 items

play09:29

and what do we want them to be well we

play09:30

don't want them to be zeros let's have

play09:32

them be sixes right or whatever right so

play09:35

we can print np six save this guy run it

play09:40

and you can see now we have

play09:42

an array of sixes right

play09:44

very cool we could do multi-dimensional

play09:49

full so let's go mp7 equals mp.full

play09:54

and let's say we want two dimensions

play09:56

with 10 elements in each dimension and

play09:58

again let's say we want to fill it with

play09:59

sixes right so let's print

play10:02

np7 save this guy head back over here

play10:05

run it again

play10:08

and here we have two dimensions of sixes

play10:11

right there's the first one there's the

play10:13

second one so just a few functions to

play10:16

play around with just to sort of

play10:17

familiarize ourselves with numpy we can

play10:20

actually convert

play10:22

python lists

play10:23

to numpy

play10:25

so let's create a list i don't know my

play10:28

list

play10:29

set that equal to a python list of one

play10:31

two three four five whatever now if we

play10:34

wanted to convert that into a numpy

play10:36

array we could let's go mp8

play10:39

equals and np dot array and all we have

play10:42

to do is just pass in my list

play10:45

numpy will convert it for us and if we

play10:47

print out

play10:49

np8

play10:51

we can see

play10:53

it's going to look just like a regular

play10:54

list but rest sure that has been

play10:56

converted to a numpy

play10:58

array so accessing specific elements out

play11:01

of a numpy array is exactly the same as

play11:04

accessing any element out of any python

play11:06

list so we could print np8 and let's say

play11:10

we want the zeroth item we just pop in a

play11:12

zero there and that will print out one

play11:14

because that's the zeroth item we save

play11:17

this and run it we see sure enough it

play11:19

just prints out one so this is just a

play11:21

very brief introduction to numpy i don't

play11:23

want to overwhelm you in this first

play11:25

video there's all kinds of cool things

play11:27

you could do with numpy specifically for

play11:29

machine learning and we're going to

play11:30

start to learn all about all of those

play11:32

things in the coming videos in this

play11:33

playlist so if you guys are interested

play11:35

in this let me know i'll continue on and

play11:38

my goal is to start to learn more about

play11:39

numpy and then sort of progress to other

play11:42

machine learning things like pandas

play11:44

scikit-learn all that stuff and then

play11:46

slowly build up a skill set for you guys

play11:48

for basic machine learning things and

play11:50

then we can get into algorithms and all

play11:52

that cool stuff but we need the basics

play11:54

first we need to understand this

play11:56

you know kind of boring stuff numpy

play11:58

arrays this is not exciting it's a

play12:00

little bit boring but it could be kind

play12:01

of fun too so stick with me on this

play12:03

we'll learn this stuff we'll knock this

play12:04

stuff out and then we'll move into more

play12:06

interesting things but uh should be fun

play12:09

so that's all for this video if you

play12:10

liked it be sure to smash like button

play12:11

below subscribe to the channel give me a

play12:13

thumbs up for the youtube algorithm and

play12:14

check out kodamy.com you can use coupon

play12:16

code youtube50 to get 50 off membership

play12:18

so that's access to all my courses over

play12:20

50 courses thousands of videos and the

play12:22

pdfs of all my best selling coding books

play12:24

join over 150 000 students learn to

play12:25

coach just like you my name is john

play12:27

elder from codingme.com and i'll see the

play12:28

next video

Rate This

5.0 / 5 (0 votes)

Связанные теги
NumPy BasicsMachine LearningData SciencePython ArraysPython TutorialCodingNumPy ArraysProgramming TipsPython ListsTech Education
Вам нужно краткое изложение на английском?