NumPy Coding Application Questions Flashcards

(15 cards)

1
Q

Create a 1D NumPy array with numbers from 0 to 9 using np.arange().

A

import numpy as np
arr = np.arange(0, 10)
print(arr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a 2x3 array filled with zeros using np.zeros().

A

import numpy as np
arr = np.zeros((2, 3))
print(arr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Generate a 2x2 array with random integers between 1 and 10 using np.random.randint().

A

import numpy as np
arr = np.random.randint(1, 11, size=(2, 2))
print(arr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Given an array arr = np.array([10, 20, 30, 40]), print its shape.

A

import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr.shape)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Slice the array arr = np.array([1, 2, 3, 4, 5]) to get elements from index 1 to 3 (exclusive).

A

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[1:3])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Calculate the mean of the array arr = np.array([5, 10, 15, 20]).

A

import numpy as np
arr = np.array([5, 10, 15, 20])
print(np.mean(arr))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Find the maximum value in the array arr = np.array([3, 9, 2, 9, 5]).

A

import numpy as np
arr = np.array([3, 9, 2, 9, 5])
print(np.max(arr))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Conditionally select elements greater than 10 from arr = np.array([5, 15, 20, 8, 12]).

A

import numpy as np
arr = np.array([5, 15, 20, 8, 12])
print(arr[arr > 10])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Concatenate two arrays arr1 = np.array([1, 2]) and arr2 = np.array([3, 4]).

A

import numpy as np
arr1 = np.array([1, 2])
arr2 = np.array([3, 4])
concatenated = np.concatenate((arr1, arr2))
print(concatenated)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Reshape the array arr = np.array([1, 2, 3, 4, 5, 6]) into a 2x3 array.

A

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = arr.reshape(2, 3)
print(reshaped)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Sort the array arr = np.array([10, 5, 3, 8, 2]) in ascending order.

A

import numpy as np
arr = np.array([10, 5, 3, 8, 2])
sorted_arr = np.sort(arr)
print(sorted_arr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Create an array with 5 evenly spaced numbers from 0 to 1 using np.linspace().

A

import numpy as np
arr = np.linspace(0, 1, 5)
print(arr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Access the element at row 1, column 2 in the 2D array arr = np.array([[1, 2, 3], [4, 5, 6]]).

A

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr[1, 2])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Calculate the standard deviation of the array arr = np.array([1, 2, 3, 4, 5]).

A

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(np.std(arr))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Stack two arrays arr1 = np.array([1, 2, 3]) and arr2 = np.array([4, 5, 6]) vertically using np.vstack().

A

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
stacked = np.vstack((arr1, arr2))
print(stacked)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly