Numpy Flashcards
Import numpy and assign to the alias np.
import numpy as np
Create a NumPy ndarray from the list [10, 20, 30]. Assign the result to the variable data_ndarray.
data_ndarray = np.array([10,20,30])
What is the difference in using Numpy vs a List of lists
Our computer would take eight processor cycles to process the eight rows of our data.
The NumPy library takes advantage of a processor feature called Single Instruction Multiple Data (SIMD) to process data faster. SIMD allows a processor to perform the same operation, on multiple data points, in a single processor cycle
Convert values from a list of lists to a numpy array
converted_taxi_list = []
for row in taxi_list:
converted_row = []
for item in row:
converted_row.append(float(item))
converted_taxi_list.append(converted_row)
Convert a variable called coverted_taxi_list to a numpy array
taxi = np.array(converted_taxi_list)
What is the difference between tuples and lists
Tuples are very similar to Python lists, but can’t be modified.
If data.shape returns the following tuple (2, 3) How do you interpret it?
The first number tells us that there are 2 rows in data_ndarray.
The second number tells us that there are 3 columns in data_ndarray.
How do you get the shape of a numpy array called taxi?
taxi_shape=(taxi.shape)
What is the sintax of any 2D array, for selecting data?
ndarray[row_index,column_index]
Slicing to select the items at index 1, 2, and 3
[1:4]
lect the item at row index 21 and column index 5. Assign it to row_21_column_5
row_21_column_5= taxi[21,5]
Select every column for the rows at indexes 391 to 500 inclusive. Assign them to rows_391_to_500.
rows_391_to_500=taxi[391:501]
Select the row at index 0. Assign it to row_0.
row_0=taxi[0]
Select every row for the columns at indexes 1, 4, and 7. Assign them to columns_1_4_7.
columns = [1,4,7]
Select the columns at indexes 5 to 8 inclusive for the row at index 99. Assign them to row_99_columns_5_to_8.
row_99_columns_5_to_8= taxi[99,5:9]
Select the rows at indexes 100 to 200 inclusive for the column at index 14. Assign them to rows_100_to_200_column_14.
rows_100_to_200_column_14= taxi[100:201,14]
Use vector addition to add fare_amount and fees_amount. Assign the result to fare_and_fees.
fare_amount = taxi[:,9] fees_amount = taxi[:,10]
fare_and_fees=fare_amount+fees_amount
Select all of the rows from colums 0 and 1
col1 = my_numbers[:,0] col2 = my_numbers[:,1]
Use vector division to divide trip_distance_miles by trip_length_hours. Assign the result to trip_mph.
trip_distance_miles = taxi[:,7] trip_length_seconds = taxi[:,8]
trip_length_hours = trip_length_seconds / 3600 # 3600 seconds is one hour trip_mph=trip_distance_miles/trip_length_hours
calculate the maximum value of trip_mph. Assign the result to mph_max.
mph_max=trip_mph.max()
calculate the average value of trip_mph. Assign the result to mph_mean.
mph_mean=trip_mph.mean()
Calculate the min value of trip_mph
mph_min = trip_mph.min()
Calculate the median average value of trip_mph
np.median(trip_mph)