day 2 creating Numpy arrays

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/13

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

14 Terms

1
New cards
import numpy as np

np.__version__

checks the latest version of numpy

2
New cards
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

3
New cards

what does numpy’s shape function do?

checks the number of rows and columns in an array

4
New cards

what does numpy’s size function do?

counts the number of elements in an array

5
New cards

in numpy shape which values represents the rows and columns?

(rows, columns)

6
New cards
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

7
New cards

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.

8
New cards

what does [::] when slicing an array?

two empty :: selects all elements in range

9
New cards
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

10
New cards
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.

11
New cards
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

12
New cards
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

13
New cards
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.

14
New cards
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.