0% found this document useful (0 votes)
58 views

NumPy Cheat Sheet

Uploaded by

codingsteez4
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)
58 views

NumPy Cheat Sheet

Uploaded by

codingsteez4
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

create a NumPy array using numpy.

empty()
Inspecting Properties Output: Removing Elements from Numpy Array Flatten a Two Dimensional array a[start:stop:step] Vector Math corrcoef
NumPy Cheat Sheet print(np.empty([4, 3], dtype=int))

Size arr.size Original Array: [[ 1. 2. 3. 4.]


print("a[-2:7:1] = ",arr[-2:7:1])
print("a[1:] = ",arr[1:])
[ 5. 6. 7. 8.] One-Dimensional array One-Dimensional array Python3 Python3
Length len(arr) [ 9. 10. 11. 12.]] arr = np.array([.5, 1.5, 2.5, 3.5, 4.5, 10.1])
Initial Placeholders Array after appending: [ 1. 2. 3. 4. 5. 6. 7. 8. Python3 Python3 Indexing Numpy Array
create numpy 1d-array
Shape arr.shape
NumPy stands for Numerical Python. 9. 10. 11. 12. 7.] Python Program illustrating
Two dimensional numpy array
numpy.delete() create numpy 1d-array
One Dimensional Array Datatype arr.dtype print("Original arr:", arr) array1 = np.array([0, 1, 2])
It is one of the most important foundational numpy.delete() list_1 = [1, 2, 3, 4] Numpy array indexing is of two types: Integer print("Shape : ", arr.shape) array2 = np.array([3, 4, 5])
packages for numerical computing & data
arange() np.arange(1, 10) Changing Datatype of Array arr.astype('float64') N-Dimensional Array print("Original arr:", arr) list_2 = [5, 6, 7, 8]
indexing and Boolean indexing.
print("Shape : ", arr.shape) arr = np.array([list_1, list_2]) applying sqrt() method pearson product-moment correlation
linespace() np.linspace(1, 10, 3) arr.tolist()
analysis in Python. Most computational Converting Array to List Python3
Python3
print("Square-root:", np.sqrt(arr)) coefficients of the arrays
Adding the values at the end deletion from 1D array print(arr.flatten()) rslt = np.corrcoef(array1, array2)
packages providing scientific functionality use zeros() np.zeros(5, dtype=int)
object = 2
applying log() method
Integer Indexing print("Log Value: ", np.log(arr))
NumPy’s array objects as the lingua franca for ones() np.ones(5, dtype=int) Saving and Loading File of a numpy array a = np.delete(arr, object)
a = np.array([[1 ,2 ],[3 ,4 ],[5 ,6 ]])
print(rslt)
data exchange. arr = np.arange(1, 13).reshape(2, 6)
print("\ndeleteing the value at index {} from
Transpose applying absolute() method
random.rand() np.random.rand(5) array:\n {}".format(object,a)) print(a[[0 ,1 ,2 ],[0 ,0 ,1]]) print("Absolute Value:", np.absolute(arr))
Saving array on disk np.save("file", np.arange(5)) print("Original Array")
print("Shape : ", a.shape) Output:
random.randint() np.random.randint(5, size=10)
np.load("file.npy")
print(arr, "\n") Boolean Indexing applying sin() method
Types of Numpy Array Loading a file
create another array which is
a = np.array([10, 40, 80, 50, 100]) print("Sine values:", np.sin(arr))
np.loadtxt('file.txt')
Python3 print(a[a>50])
[[1. 1.]
Python3 Importing a Text File
applying ceil() method [1. 1.]]
1D array 2D array 3D array create a NumPy array using numpy.arange() Importing CSV File
np.genfromtxt('file.csv' to be appended column-wise Reshaping Array making a 3x3 array print("Ceil values:", np.ceil(arr))
delimiter=',') col = np.arange(5, 11).reshape(1, 6)
Axis 1 print(np.arange(1, 10)) gfg = np.array([[1, 2],
1 2 3 1.5 2 3 Write Text File
np.savetxt('file.txt',arr arr_col = np.append(arr, col, axis=0) applying floor() method
delimiter=' ') [4, 5],
4 5 6 create a NumPy array using numpy.linspace() print("Array after appending the values
column wise") Python3 [7, 8]]) Copying and Viewing Array print("Floor Values:", np.floor(arr))

print(np.linspace(1, 10, 3)) applying round_() method


Axis 1 Axis 0 Axis 2 Data Types print(arr_col, "\n") before transpose Coping to new memory space arr.copy() print ("Rounded values:", np.round_(arr))
Axis 0 creating a numpy array
create a NumPy array using numpy.zeros() array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9,
print(gfg, end ='\n\n')
print(np.zeros(5, dtype=int)) to be appended row wise Shallow Copy arr.view()
Signed 64-bit integer types np.int64 10, 11, 12, 13, 14, 15, 16]) after transpose
row = np.array([1, 2]).reshape(2, 1)
Standard double-precision arr_row = np.append(arr, row, axis=1) print(gfg.transpose(1, 0)) Statistic
Creating Arrays Commands ccreate a NumPy array using numpy.ones() floating point
np.float32 printing array
print(np.ones(5, dtype=int))
Complex numbers represented np.complex
print("Array after appending the values row wise")
print(arr_row)
print("Array: " + str(array)) NumPy Array Mathematics
by 128 floats
One Dimensional Array create a NumPy array using numpy.random.rand() Boolean type storing TRUE
reshaping numpy array Python3
np.bool
From Python List np.array([1, 2, 3, 4, 5])
print(np.random.rand(5)) & FALSE values Output: converting it to 2-D from 1-D array Combining and Splitting Commands Arithmetic Operations numpy.delete()
python object type np.object reshaped1 = array.reshape((4, array.size//4))
np.concatenate((arr1, arr2), print("Original arr:", arr)
From Python Tuple np.array((1, 2, 3, 4, 5)) create a NumPy array using numpy.random.randint() Original Array Combining Arrays
axis = 0)
printing reshaped array print("Shape : ", arr.shape)
print(np.random.randint(5, size=10)) Fixed-length string type np.string_
np.fromiter((a for a in print("First Reshaped Array:") Splitting array np.split(arr, 3, 1) Adds np.add(a, b)
fromiter() function range(8)), float) [[ 1 2 3 4 5 6] elements of 2 Array 1D array
Fixed-length unicode type np.unicode_ [ 7 8 9 10 11 12]] print(reshaped1)
Horizontal Split np.hsplit(arr, 3) Substracts np.subtract(a, b) arr = [20, 2, 7, 1, 34]
N-dimensional Numpy Arrays Array after appending the values column wise elements of 2 Array
Python3 [[ 1 2 3 4 5 6]
creating another reshaped array Vertical Split np.vsplit(a, 3) Multiply mean
np.zeros([4, 3], dtype = np.multiply(a, b)
create a NumPy array from a list
zeros()
np.int32) Sorting Array [ 7 8 9 10 11 12]
reshaped2 = np.reshape(array, (2, 8)) elements of 2 Array

Divide
print("mean of arr:", np.mean(arr))
np.ones([4, 3], dtype = [ 5 6 7 8 9 10]] np.divide(a, b)
li = [1, 2, 3, 4] ones()
np.int32)
printing reshaped array elements of 2 Array median
print(np.array(li))
full() np.full([2, 2], 67, dtype =
Sorting 1D Array arr.sort() Array
[[ 1
after appending the values row wise
2 3 4 5 6 1]
print("\nSecond Reshaped Array:") Indexing, Slicing and Subsetting Modulo
elements of 2 Array
np.mod(a, b) print("median of arr:", np.median(arr))
int) Sorting along the first axis of print(reshaped2)
create a NumPy array from a tuple the 2D array
np.sort(a, axis = 0) [ 7 8 9 10 11 12 2]] Remainder np.remainder(a,b)
sum
tup = (5, 6, 7, 8) eye() np.eye(4) elements of 2 Array print("Sum of arr(uint8):",
print(np.array(tup))
Subsetting Numpy Array Power
elements of 2 Array
np.power(a, b) np.sum(arr, dtype = np.uint8))

create a NumPy array using fromiter() Python3 NumPy Array Manipulation Inserting Elements into the Array Resizing an Array Exponant np.exp(b)
print("Sum of arr(float32):",
np.sum(arr, dtype = np.float32))
elements of 2 Array
iterable = (a for a in range(8)) create a NumPy array using numpy.zeros()
print(np.fromiter(iterable, float))
print(np.arange(1, 10))
Python3 min and max
Appending Elements to Array One-Dimensional array Numpy arrays can be resized using the resize() Index values can be negative.
print("maximum element:", np.max(arr))
Multi-Dimensional Array create a NumPy array using numpy.ones()
function. It returns nothing but changes the print(arr)
Comparison print("minimum element:", np.min(arr))
print(np.ones([4, 3], dtype = np.int32)) Python3 var
np.array([[1, 2, 3, 4],[5, 6,
One-Dimensional array original array. print("Elements are:", arr[np.array([1, 3, -3])])
Using Python Lists 7, 8], [9, 10, 11, 12]]) arr = np.asarray([1, 2, 3, 4]) print("var of arr:", np.var(arr))
create a NumPy array using numpy.full()
Python3 Python3 print("var of arr(float32):",
Using empty() np.empty([4, 3], dtype=int) print(np.full([2, 2], 67, dtype = int)) Python Program illustrating numpy.insert() Python3 np.var(arr, dtype = np.float32))
Adding the values at the end print("1D arr:", arr) Slicing Numpy Array an_array = np.array([[1, 2], [3, 4]]) standard deviation
Python3 create a NumPy array using numpy.eye() print("Shape:", arr.shape) Making a random array
of a numpy array arr = np.array([1, 2, 3, 4, 5, 6])
another_array = np.array([[1, 2], [3, 4]]) print("std of arr:", np.std(arr))
print(np.eye(4))
create a NumPy array from a list print("Original Array:", arr) Inserting value 9 at index 1 The ":" operator means all elements till the end. comparison = an_array == another_array print ("More precision with float32",
list_1 = [1, 2, 3, 4] a = np.insert(arr, 1, 9) Required values 12, existing values 6 equal_arrays = comparison.all() np.std(arr, dtype = np.float32))
list_2 = [5, 6, 7, 8] appending to the array print("\nArray after insertion:", a) arr.resize(3, 4)
Python3
list_3 = [9, 10, 11, 12] arr = np.append(arr, [7]) print("Shape:", a.shape) print(arr) print(equal_arrays)
print(np.array([list_1, list_2, list_3])) print("Array after appending:", arr) print(arr)

You might also like