0% found this document useful (0 votes)
70 views1 page

Numpy Cheat Sheet Python For Data Science: Inspecting Your Array Sorting Arrays

The NumPy library is a core library for scientific computing in Python. It provides high-performance multidimensional array objects and tools for working with arrays. NumPy allows operations on entire arrays rather than individual elements. Common operations include arithmetic operations, indexing/slicing, data types, shape manipulation, and linear algebra.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views1 page

Numpy Cheat Sheet Python For Data Science: Inspecting Your Array Sorting Arrays

The NumPy library is a core library for scientific computing in Python. It provides high-performance multidimensional array objects and tools for working with arrays. NumPy allows operations on entire arrays rather than individual elements. Common operations include arithmetic operations, indexing/slicing, data types, shape manipulation, and linear algebra.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

> Inspecting Your Array > Sorting Arrays

Python For Data Science

>>> a.shape #Array dimensions


>>> a.sort() #Sort an array

>>> len(a) #Length of array


>>> c.sort(axis=0) #Sort the elements of an array's axis
>>> b.ndim #Number of array dimensions

e.size #Number of array elements

NumPy Cheat Sheet


>>>
>>> b.dtype #Data type of array elements

>>>
>>>
b.dtype.name #Name of data type

b.astype(int) #Convert an array to a different type > Subsetting, Slicing, Indexing


Learn NumPy online at www.DataCamp.com Subsetting

> Data Types >>> a[2] #Select the element at the 2nd index

>>> b[1,2] #Select the element at row 1 column 2 (equivalent to b[1][2])

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

>>> np.complex #Complex numbers represented by 128 floats


>>> a[0:2] #Select items at index 0 and 1
1 2 3
>>> Numpy
np.bool #Boolean type storing TRUE and FALSE values
array([1, 2])

>>> np.object #Python object type


>>> b[0:2,1] #Select items at rows 0 and 1 in column 1
1.5 2 3
The NumPy library is the core library for scientific computing in Python.
>>> np.string_ #Fixed-length string type
array([ 2., 5.])
4 5 6
>>> np.unicode_ #Fixed-length unicode type >>> b[:1] #Select all items at row 0 (equivalent to b[0:1, :])

It provides a high-performance multidimensional array object, and tools for array([[1.5, 2., 3.]])

1.5 2 3

working with these arrays >>> c[1,...] #Same as [1,:,:]

4 5 6

> Array Mathematics


array([[[ 3., 2., 1.],

Use the following import convention: [ 4., 5., 6.]]])

>>> a[ : :-1] #Reversed array a array([3, 2, 1])


>>> import numpy as np
Boolean Indexing
Arithmetic Operations >>> a[a<2] #Select elements from a less than 2
1 2 3
NumPy Arrays array([1])

>>> g = a - b #Subtraction
Fancy Indexing
array([[-0.5, 0. , 0. ],

>>> b[[1, 0, 1, 0],[0, 1, 2, 0]] #Select elements (1,0),(0,1),(1,2) and (0,0)

[-3. , -3. , -3. ]])

array([ 4. , 2. , 6. , 1.5])

>>> np.subtract(a,b) #Subtraction

>>> 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. ],

>>> np.add(b,a) Addition

[ 1.5, 2. , 3. , 1.5]])
>>> a / b #Division

array([[ 0.66666667, 1. , 1. ],

[ 0.25 , 0.4 , 0.5 ]])

>>> np.divide(a,b) #Division

> Array Manipulation


Creating Arrays
>>> a * b #Multiplication

>
array([[ 1.5, 4. , 9. ],

[ 4. , 10. , 18. ]])

>>> np.multiply(a,b) #Multiplication


Transposing Array
>>> np.exp(b) #Exponentiation
>>> i = np.transpose(b) #Permute array dimensions

>>> 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

>>> e.dot(f) #Dot product


>>> g.reshape(3,-2) #Reshape, but don’t change data
Initial Placeholders array([[ 7., 7.],

[ 7., 7.]]) Adding/Removing Elements


>>> h.resize((2,6)) #Return a new array with shape (2,6)

>>> np.zeros((3,4)) #Create an array of zeros


>>> np.append(h,g) #Append items to an array

>>> np.ones((2,3,4),dtype=np.int16) #Create an array of ones


Comparison >>> np.insert(a, 1, 5) #Insert items in an array

>>> d = np.arange(10,25,5) #Create an array of evenly spaced values (step value)


>>> np.delete(a,[1]) #Delete items from an array
>>> np.linspace(0,2,9) #Create an array of evenly spaced values (number of samples)

>>> e = np.full((2,2),7) #Create a constant array


>>> a == b #Element-wise comparison
Combining Arrays
>>> f = np.eye(2) #Create a 2X2 identity matrix
array([[False, True, True],
>>> np.concatenate((a,d),axis=0) #Concatenate arrays

>>> np.random.random((2,2)) #Create an array with random values


[False, False, False]], dtype=bool)

array([ 1, 2, 3, 10, 15, 20])

>>> np.empty((3,2)) #Create an empty array >>> a < 2 #Element-wise comparison


>>> np.vstack((a,b)) #Stack arrays vertically (row-wise)

array([True, False, False], dtype=bool)


array([[ 1. , 2. , 3. ],

>>> np.array_equal(a, b) #Array-wise comparison [ 1.5, 2. , 3. ],

[ 4. , 5. , 6. ]])

> I/O Aggregate Functions


>>> np.r_[e,f] #Stack arrays vertically (row-wise)

>>> np.hstack((e,f)) #Stack arrays horizontally (column-wise)

array([[ 7., 7., 1., 0.],

[ 7., 7., 0., 1.]])

Saving & Loading On Disk >>> a.sum() #Array-wise sum


>>> np.column_stack((a,d)) #Create stacked column-wise arrays

>>> a.min() #Array-wise minimum value


array([[ 1, 10],

>>> b.max(axis=0) #Maximum value of an array row


[ 2, 15],

>>> 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

>>> a.corrcoef() #Correlation coefficient


Splitting Arrays
>>> np.std(b) #Standard deviation >>> np.hsplit(a,3) #Split the array horizontally at the 3rd index

Saving & Loading Text Files [array([1]),array([2]),array([3])]

>>> np.vsplit(c,2) #Split the array vertically at the 2nd index

>>> np.loadtxt("myfile.txt")

>>> np.genfromtxt("my_file.csv", delimiter=',')

>>> np.savetxt("myarray.txt", a, delimiter=" ")


> Copying Arrays [array([[[ 1.5, 2. , 1. ],

[ 4. , 5. , 6. ]]]),

array([[[ 3., 2., 3.],

[ 4., 5., 6.]]])]

>>> h = a.view() #Create a view of the array with the same data

>>> np.copy(a) #Create a copy of the array

>>> h = a.copy() #Create a deep copy of the array


> Asking For Help
Learn Data Skills Online at www.DataCamp.com
>>> np.info(np.ndarray.dtype)

You might also like