Numpy Tutorial Python - Array Creation

Tech With Tim · Beginner ·⚡ Algorithms & Data Structures ·7y ago

Key Takeaways

The video demonstrates the use of the NumPy library in Python to create arrays, including methods such as np.zeros, np.ones, np.arange, np.linspace, np.full, np.identity, and np.random. It covers the creation of arrays with different shapes, data types, and values.

Full Transcript

[Music] hey guys and welcome to the second video in my numpy array toriel so in today's video I'm gonna be going all over all of the different ways that you can create an array and this is just gonna be without using like text files and i/o and stuff I'm gonna do that in a future video this is just gonna be ways that you can do this with straight code so nothing like reading in or reading out but I will be doing that in future videos so let's go ahead and get started so numpy has a lot of different ways or numpy sorry that you can actually create arrays that's really useful and it makes it a lot faster than doing like embedded for loops with regular lists and Python so the first way is we have this thing called NP dot zeroes and what we can do here is this is just gonna create us a list or sorry an array with the given shape so in this case 2 3 so 2 rows 3 columns of all zeros so if I run this and I print X to the screen you can see that we've gotten all zeros here and the default data type for this is a float now if I wanted to change this so that it's not a float and it actually is an int I believe I can do something like D type equals int and then if I click press X here and print this to the screen you can see that we get integers instead of floats so D type equals and you can change that to flow you can change that to string however you like there to get that so the next one it's gonna code with this it's a very similar is NP dot one's same thing we give it a shape let's just give it like 4/5 for an example here click that X and you can see again we get a array filled with ones of the given shape for 5 now we can also give a shape it's multiple dimensions like 4 or 5 3 print that to the screen and you can see we get a much longer array but just showing you that you can go ahead and do that the next method we're going to use here is actually really useful and it's numpy dot arrange and i believe it's actually a range not like as in two separate words so I'll print it out here or type it so we get a range like this and the way that this worked is just like the typical range function it's best shown with an example so I'll do that right now but it works in the same way as the standard Python range function so you see if I print this I get X then down here we get array and we get going up to 10 but not including 10 starting at 0 and array containing those numbers which again is really useful now what we can do here is we can treat this like a typical range so I do something like 5 10 and we print this out so the screen there you can see we start at 5 and go to 10 but do not include 10 so it's the same format as a typical range function start stop end or start stop step sorry so I go for example let's do 1 to 11 step by 2 and then if I print this is screen you see we get 1 3 5 7 9 like that so that's really useful and we can also do it with decimals as well so I'm just gonna bring an example I have on another thing here just to say a bit of typing so we get em Peter Ainge 2 3 go up by 0.1 print this then you can see that we get starting at 2 and then up to 2.9 okay the next one is similar but this one is gonna do a bit of math for you which actually saves us some time so this one is called linspace so I'm just gonna say x equals NP the linspace and in here we're gonna type 2 value so we're gonna start out what we want to stop at and then what to go up I believe something like that so let's just give an example here because I think I actually butchered that description and 6 okay so let's just print this the screen to see exactly what says okay so what we do here is we say we want to start at one point oh we want to go to 4.0 and we want to have sorry 6 different values in heart rate so what this does is it saves us from doing a bit of math and it's going to find out what we need to go up by to get 6 different values ranging from 1 to 4 so you can see we have one one point six two point two two point eight so on and we get six values and they automatically increment properly so evenly so that we can get the value I hope that makes sense it's not super complicated ok the next one is similar to the ones and zeros that we had this one's going to just be a constant and allow us to type in whatever value we want which is really useful as well so say x equals and P dot full so instead of zeros and ones earning full same thing we're gonna give a shape so this gets many of two two and then the value that we type in here is going to be what's filled for the entire array say x equals MP doubtful shape to two with eights and then if I show you we get constants so eights in here like so okay the next one and this is gonna be for some of you guys that know a little bit about linear algebra if you don't don't worry about it because you might be kind of confused on what this is but I am just going to show you so this is gonna give us an identity matrix of shape whatever we give it so identity matrix have these same rows and columns so we just give it one argument which is gonna be rows or columns whatever so this case I'm gonna do five and if I print this so X you can see we get an identity matrix so with our leading ones in the corresponding columns and this can be useful for doing some calculations and you can give it obviously whatever parameter you want to give it right so five six two so on okay this next one and actually the last one I'm gonna show in this video I know this was kind of short is going to be creating a random array so I don't know why you would really want to do this but if for some reason you do you can type NP dot random dot random give it a shape let's just do it for five and if you print that you will get a bunch of random values that are of type float now I just want to test this out and see if it actually works with integers I haven't done this before so hopefully it doesn't crash I expected arguments so we can't act Oh d-type is an unexpected argument yeah so we can't actually end up giving it what do you call it integers like that I believe if we wanted to just convert these into integers we just multiply every element in the array by like 100 110 something like that to give us an integer anyways that's been it for this video if you guys enjoyed please make sure you leave a like and subscribe and stay tuned for the next videos where I'm going to go through stuff like math that we can do matrix multiplication we can do a bunch of cool stuff with num num pie this is just the beginning so make sure you watch the rest of them when they come out later [Music]

Original Description

In this video I explain how to implement arrays in python using the module numpy. This is a module you must download as it is not built into python. Numpy is extremely useful for using data structures and multi-dimensional lists. It has some built-in methods and properties that will save you a lot of time. In this specific tutorial I show different ways to create arrays. If you want to learn more about numpy and some more advanced examples stay tuned for the rest of the videos and subscribe! Text-Based Tutorial: https://techwithtim.net/tutorials/python-module-walk-throughs/numpy-module/array-creation/ Twitter: https://twitter.com/TechWithTimm Want To Support This Channel? Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud Please leave a LIKE and SUBSCRIBE for more content! Tags: - Tech - Tech With Tim - Programming - Coding - Pygame - Python Tutorials - Numpy - Numpy Arrays - Arrays in python - Numpy module - Nump tutorial - How to use an array in python - Array creation - Array creation in python
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video teaches how to create arrays in Python using the NumPy library, covering various methods and their applications. It is essential for beginners in Python programming and data science.

Key Takeaways
  1. Import the NumPy library
  2. Use np.zeros to create an array of zeros
  3. Use np.ones to create an array of ones
  4. Use np.arange to create an array with a specified range
  5. Use np.linspace to create an array with evenly spaced values
  6. Use np.full to create an array with a constant value
  7. Use np.identity to create an identity matrix
  8. Use np.random to create a random array
💡 The NumPy library provides various methods for creating arrays, each with its own specific use case and application.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →