1/13
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
import numpy as np
np.__version__
checks the latest version of numpy
import numpy as np
my_list = [2, 3, 4, 6]
arr = np.array(my_list)
print(f'array: {arr}, array shape: {arr.shape}: array size {arr.size}')
Converts a list into an array by using np array on the list. Shape and size checks the number of rows, columns, and elements in the array
what does numpy’s shape function do?
checks the number of rows and columns in an array
what does numpy’s size function do?
counts the number of elements in an array
in numpy shape which values represents the rows and columns?
(rows, columns)
rev_arr = arr[::-1]
print(rev_arr)
using two empty:: selects all elements in the array while the -1 steps through all elements in reverse
what do the left and right sides of [:] when slicing an array?
the left side represents the starting point of an index and the right side represents the ending point of an index.
what does [::] when slicing an array?
two empty :: selects all elements in range
my_list_2 = list(range(8, 15, 2))
my_list_2 = my_list + my_list_2
arr_2 = np.array(my_list_2)
print(f'array: {arr_2} shape: {arr_2.shape}, size: {arr_2.size}')
adds two lists together with + and then converts the combined list into an array. the shape and size functions checks the number of rows, columns, and elements in the array
my_list = [2, 3, 4, 6]
my_list_2 = list(range(8, 15, 2))
for x in my_list_2:
my_list.append(x)
arr = np.array(my_list)
print(f'{arr} shape: {arr.shape} size: {arr.size}')
adds two lists together with a for loop and append and then converts it to an array. The for loop iterates over the second list where x represents each iteration. Append adds the elements of the second list. Shape and size count the number of rows, columns, and elements in the newly created array.
my_list = [2, 3, 4, 6]
my_list_2 = list(range(8, 15, 2))
arr = np.array([my_list, my_list_2])
print(f'{arr} shape: {arr.shape} size: {arr.size}')
adds two lists together and converts to an array by including both lists separated by commas inside the () of np.array. Shape and size count the rows, columns, and elements of the array
my_list = [2, 3, 4, 6]
my_list_2 = list(range(8, 15, 2))
my_list_3 = [16, 18, 20, 22.1]
my_list_3[3] = int(my_list_3[3])
my_list_4 = my_list + my_list_2 + my_list_3
arr_3 = np.array(my_list_4)
print(arr_3)
converts the third index in the third list from a float to an integer. The lists are then merged with + and converted into an array with np.array
my_list = [2, 3, 4, 6]
my_list_2 = list(range(8, 15, 2))
my_list_3 = [16, 18, 20, 22.1]
my_list_3[3] = int(my_list_3[3])
my_list.extend(my_list_2)
my_list.extend(my_list_3)
arr_3 = np.array(my_list)
print(arr_3)
converts the third index in the third list from a float to an integer. The first list is merged with other lists with the extend function and then converted to an array with np.array. The lists being merged with the first list are within the () of extend.
my_list = [2, 3, 4, 6]
my_list_2 = list(range(8, 15, 2))
my_list_3 = [16, 18, 20, 22.1]
arr = np.array([my_list, my_list_2, my_list_3])
print(arr.astype(int))
The lists are merged by including each list within the () of np.array. Astype is used in the print statement to convert any values that aren’t integers to integers.