numPy Flashcards

1
Q

how to import numpy in python?

A

import numpy as np

we usually assign the package to a variable named np

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

how to create a numpy array (numpy.ndarray) with 3 elements, all zeros? (assign to a variable named “a”)

A

a = np.zeros(3)

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

how to create a numpy array (numpy.ndarray) with 6 elements, all ones? (assign to a variable named “a”)

A

a = np.ones(6)

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

how to create a 2 dimensional numpy array (numpy.ndarray) with shape 6, 4 with elements all zeros? (assign to a variable named “a”)

A

a = np.zeros((6,4))

the parameter is a tuple!

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

how to print the variable named “v” in Jupyter to see its content?

A

v

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

how to print the first element of one dimensional numpy array “a” in Jupyter to see its content?

A

a[0]

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

how to print the type of variable “v” in Jupyter?

A

type(v)

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

how to print the shape of a numpy array named “a” in Jupyter?

A

a.shape

this is the numpy.ndarray.shape function

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

how to change the shape of a numpy array named “a” in Jupyter to (4,5)?

A

a.shape = (4,5)

with a tuple

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

how to create a one dimensional numpy array, starting with 10, ending with 30, with equally distant elements, 12 of them altogether?
(assign to variable “a”)

A

a = np.linspace(10,30,12)

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

create a python list of 4,5,8 and immidiately convert to a numpy array and assign to a variable named “a”

A

a = np.array([4,5,8])

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

how to efficiently access the last element in a python list named l?

A

l[-1]

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

“a” array or list has 10 elements. Access the elements indexed 3,4,5

A

a[3:6]

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

create an array with 50 random floats, evenly distributed, from
0 to 10

A

a = np.random.rand(50)*10

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

create an array with 20 random integers from 0 to 10 (including 10 as a possible value)

A

a = np.random.randint(0,11,size=20)

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

access every second element of a list or array, starting from the first

17
Q

how to return an array of booleans, indicating if numpy array “a” values are greater than 5 or not at corresponding indexes?

18
Q

use numpy to return mean of array “a”

A

np.mean(a)

19
Q

use numpy to return standard deviation of array “a”

20
Q

use numpy to return sum of array “a”

21
Q

use numpy to return variance of array “a”

22
Q

use numpy to return minimum of array “a” and also the index of the first occurance of it

A
minOfA = np.min(a)
firstIndexOfMinOfA = np.argmin(a)
23
Q

use numpy to return an array which contains the elements of array “a” that are greater than 3

24
Q

change array “a” so that the values greater than 10 will be 5, the other will be 0

A

a = np.where(a > 10, 5, 0)

25
"a" and "b" are both numpy arrays with same length. Create a numpy array "c" which has also same length and the elements are the sums of corrwsponding elements of "a" and "b" arrays
c = a + b
26
add 5 to all elements of "a" numpy array
a = a + 5
27
dot product of two numpy arrays "a" and "b"
a @ b
28
sort array "a"
np.sort(a)
29
use numpy to create an array with 1000 elements that are standard normally distributed
a = np.random.normal(0,1,1000)
30
calculate histogram parameters of array "a"
np.histogram(a)
31
get the current working directory
import os | d = os.getcwd()
32
create an array "a" with a sequence evenly spaced, each value is followed with itself+0.2, starting with 3, ending with 6
a = np.arange(3,6,0.2)