Numpy Cheat Sheet Python For Data Science: Inspecting Your Array Sorting Arrays
Numpy Cheat Sheet Python For Data Science: Inspecting Your Array Sorting Arrays
>>>
>>>
b.dtype.name #Name of data type
> Data Types >>> a[2] #Select the element at the 2nd index
1.5 2
2 3
3
6.0 4 5 6
>>> np.int64 #Signed 64-bit integer types
Slicing
Numpy
>>> np.float32 #Standard double-precision floating point
It provides a high-performance multidimensional array object, and tools for array([[1.5, 2., 3.]])
1.5 2 3
4 5 6
>>> g = a - b #Subtraction
Fancy Indexing
array([[-0.5, 0. , 0. ],
array([ 4. , 2. , 6. , 1.5])
>>> b[[1, 0, 1, 0]][:,[0,1,2,0]] #Select a subset of the matrix’s rows and columns
>>> b + a #Addition
array([[ 4. ,5. , 6. , 4. ],
array([[ 2.5, 4. , 6. ],
[ 1.5, 2. , 3. , 1.5],
[ 5. , 7. , 9. ]])
[ 4. , 5. , 6. , 4. ],
[ 1.5, 2. , 3. , 1.5]])
>>> a / b #Division
array([[ 0.66666667, 1. , 1. ],
>
array([[ 1.5, 4. , 9. ],
>>> a = np.array([1,2,3])
>>> np.sqrt(b) #Square root
>>> i.T #Permute array dimensions
>>> b = np.array([(1.5,2,3), (4,5,6)], dtype = float)
>>> np.sin(a) #Print sines of an array
>>> c = np.array([[(1.5,2,3), (4,5,6)],[(3,2,1), (4,5,6)]], dtype = float) >>> np.cos(b) #Element-wise cosine
Changing Array Shape
>>> np.log(a) #Element-wise natural logarithm
>>> b.ravel() #Flatten the array
[ 4. , 5. , 6. ]])
>>> np.save('my_array', a)
>>> b.cumsum(axis=1) #Cumulative sum of the elements
[ 3, 20]])
>>> np.savez('array.npz', a, b)
>>> a.mean() #Mean
>>> np.c_[a,d] #Create stacked column-wise arrays
>>> np.load('my_array.npy') >>> b.median() #Median
>>> np.loadtxt("myfile.txt")
[ 4. , 5. , 6. ]]]),
>>> h = a.view() #Create a view of the array with the same data