numpy basic 1 Flashcards
data type, array creation func
int8, uint8
i1, u1
int16, uint16,
i2, u2
int32, uint32
i4 , u4
int64, uint64
i8, u8
float16
f2
float32
f4 or f
float64
f8 or d
float128
f16 or g
complex64
c8 c16
complex128
c32
bool
?
object
0
string
S
unicode
U
array
Convert input data (list, tuple, array, or other sequence type) to an ndarray either by inferring a dtype
or explicitly specifying a dtype; copies the input data by default
In [19]: data1 = [6, 7.5, 8, 0, 1]
In [20]: arr1 = np.array(data1)
In [21]: arr1
Out[21]: array([ 6. , 7.5, 8. , 0. , 1. ])
asarray
Convert input to ndarray, but do not copy if the input is already an ndarray
no shit seen look into the future
arange Like the built-in range but returns an ndarray instead of a list
In [32]: np.arange(15) Out[32]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
arange Like the built-in range but returns an ndarray instead of a list
In [32]: np.arange(15)
Out[32]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
ones, zeros, empty ,full
quite the same hope u didn’t forget
ones_like, zeros_like, empty_like , full like
makes a arr full of value like the arr given as arg
np.astype
In [44]: numeric_strings = np.array([‘1.25’, ‘-9.6’, ‘42’], dtype=np.string_)
In [45]: numeric_strings.astype(float)
Out[45]: array([ 1.25, -9.6 , 42. ])
arr 1d index and slicing prblm
arr = np.arange(10)
arr[5:8] = ?
Out[63]: array([5, 6, 7])
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [73]: arr2d[2]
= ?
Out[73]: array([7, 8, 9])
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [74]: arr2d[0][2] and arr2d[0, 2]
output?
3
arr = [1,2,3,4]
what are the index of each value in arr
1=0
2=1
3=2
4=3
.copy()
to copy an arr
if arr_1 = [1,2,3]
arr_2 =[ [5,6,7], [8,9,0] ]
how to transform the first value of arr_2 to arr_1
expected value = [[1,2,3], [8,9,0]]
arr_2[0] = arr_1
arr_2
how to have dot product of arr1 and arr2
np.dot(arr1, arr2)
how to create transpose of an arr
arr.T Simple transposing with .T is a special case of swapping axes. ndarray has the method swapaxes, which takes a pair of axis numbers and switches the indicated axes to rear‐ range the data: In [135]: arr Out[135]: array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7]], [[ 8, 9, 10, 11], [12, 13, 14, 15]]]) In [136]: arr.swapaxes(1, 2) Out[136]: array([[[ 0, 4], [ 1, 5], [ 2, 6], [ 3, 7]], [[ 8, 12], [ 9, 13], [10, 14], [11, 15]]])