Numpy Basics
In this Numpy cheatsheet, we will go through basics of Numpy.
import pandas as pd
import numpy as np
Create Numpy Array
arr = np.array([10,11,12,14])
Find the length of numpy array.
len(arr)
arr[1]
Find type of numpy array.
type(arr[1])
arr.dtype
Create Numpy array with int32 type
arr = np.array([10,11,12],dtype=np.int32)
arr.dtype
arr1 = np.random.rand(10000000)
arr2 = np.random.rand(10000000)
Multiply Two Numpy Arrays
%time arr1 * arr2
arr = np.array([10, 21, 3])
list1 = arr.tolist()
print(f'List: {list1}')
import numpy as np
# 2d array to list
arr = np.array([[11, 100, 7], [14, 6, 2]])
list1 = arr.tolist()
print(f'NumPy Array:\n{arr}')
print(f'List: {list1}')
l = [4,8,9]
arr = np.array(l)
print(arr)
Numpy Matrix
mat = np.array([[10,20,30],[1,2,3]])
mat
Find shape of Numpy Matrix...
mat.shape
Numpy matrix last row access...
mat[-1]
Create numbers Using Numpy np.arange
nos = np.arange(6)
Reshape Numpy Matrix
nos.reshape(2,3)
Transpose Numpy Matrix
nos = np.arange(6)
nos.transpose()
nos.T
Numpy Matrix Slicing
mat = np.array([[10,20,30],[1,2,3]])
mat
Access first row and second column values...
mat[0,1]
Access 2nd column values...
mat[:,1]
Access all column values except values from first column...
mat[:,1:]
Access values from column 2nd,3rd which are from row 2nd...
mat[1:,1:]
Also we can use transpose method on the above sliced matrix...
mat[1:,1:].transpose()
Related Notebooks
- How To Handle nan In Numpy
- Covid 19 Curve Fit Using Python Pandas And Numpy
- Polynomial Interpolation Using Python Pandas Numpy And Sklearn
- Convert Pandas DataFrame To Numpy Arrays
- Python Numpy Where
- Numpy Array vs Vector
- Understand Tensors With Numpy
- ERROR Could not find a version that satisfies the requirement numpy==1 22 3
- How To Solve Error Numpy Has No Attribute Float In Python