Let’s check the dimensions of A using the shape method: A.shape Produces: (24,)Ĭoncatenate stuck our lists together one after another (like a train) and created a long vector of length 24. The dimensions of a matrix is really important, it governs whether two matrices can be added together, matrix multiplied, inverted, etc. Let’s try concatenate first and see what happens: A = np.concatenate() A Produces: array() We have several options for sticking them together. import random # Create 3 lists, each with 8 random numbers list_1 = list_2 = list_3 = ![]() For example, you might have just finished running a for loop where in each iteration of the loop, you append the calculated values that you want to analyze to one of several lists. While this might seem random, often in data science our data starts off in lists. Let’s say we have three lists of numbers that we would like to combine into a matrix. Let’s make an array so we can mess around with it. import numpy as np my_vec = np.array(my_list) my_vec + 1 Produces: array() my_vec / 2 Produces: array() NumPy Array Properties When you apply an arithmetic operation to a NumPy array, it is applied to every element of the array. Adding 1 to my_vec successfully increments every element of my_vec by 1. my_list + Produces: īy changing our list into a vector, we get the desired behavior. We could use a list comprehension to increment every value in the list by 1, but that’s overkill when we can just use NumPy. The code below appends a 1 to the end of my_list, not what we want. The + concatenates for lists instead of performing element addition like we hoped. For example, let’s try incrementing all elements of my_list by 1: my_list = my_list + 1 Produces: TypeError: can only concatenate list (not "int") to list Lists were not designed with those properties in mind. Why can’t I just use a list of numbers you might ask? Matrices have their own unique math properties. NumPy is Python’s goto library for working with vectors and matrices. ![]() Today, we will go over some NumPy array basics and tips to get you started on your data science journey on the right foot. So it’s best to get comfortable working with them. Moreover, some of Python’s popular data science libraries take NumPy arrays as inputs and spit them out as outputs. Even today, I prefer Pandas to NumPy because it looks nicer (when displayed), handles non-numeric data better, and is much more user friendly.īut Pandas is built on NumPy and opting to do your computations in NumPy can make your code run significantly faster. It’s also a bit awkward if you’re just starting out. NumPy is critical if you want to do data science. ![]() Learn How To Manipulate The Basic Building Blocks Of Data Science
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |