Numpy Basics
In this Numpy cheatsheet, we will go through basics of Numpy.
In [1]:
import pandas as pd
import numpy as np
Create Numpy Array
In [2]:
arr = np.array([10,11,12,14])
Find the length of numpy array.
In [3]:
len(arr)
Out[3]:
In [4]:
arr[1]
Out[4]:
Find type of numpy array.
In [5]:
type(arr[1])
Out[5]:
In [6]:
arr.dtype
Out[6]:
Create Numpy array with int32 type
In [7]:
arr = np.array([10,11,12],dtype=np.int32)
In [8]:
arr.dtype
Out[8]:
In [9]:
arr1 = np.random.rand(10000000)
arr2 = np.random.rand(10000000)
Multiply Two Numpy Arrays
In [10]:
%time arr1 * arr2
Out[10]:
In [11]:
arr = np.array([10, 21, 3])
list1 = arr.tolist()
print(f'List: {list1}')
In [12]:
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}')
In [13]:
l = [4,8,9]
arr = np.array(l)
print(arr)
Numpy Matrix
In [14]:
mat = np.array([[10,20,30],[1,2,3]])
In [15]:
mat
Out[15]:
Find shape of Numpy Matrix...
In [16]:
mat.shape
Out[16]:
Numpy matrix last row access...
In [17]:
mat[-1]
Out[17]:
Create numbers Using Numpy np.arange
In [18]:
nos = np.arange(6)
Reshape Numpy Matrix
In [19]:
nos.reshape(2,3)
Out[19]:
Transpose Numpy Matrix
In [20]:
nos = np.arange(6)
nos.transpose()
Out[20]:
In [21]:
nos.T
Out[21]:
Numpy Matrix Slicing
In [22]:
mat = np.array([[10,20,30],[1,2,3]])
In [23]:
mat
Out[23]:
Access first row and second column values...
In [24]:
mat[0,1]
Out[24]:
Access 2nd column values...
In [25]:
mat[:,1]
Out[25]:
Access all column values except values from first column...
In [26]:
mat[:,1:]
Out[26]:
Access values from column 2nd,3rd which are from row 2nd...
In [27]:
mat[1:,1:]
Out[27]:
Also we can use transpose method on the above sliced matrix...
In [28]:
mat[1:,1:].transpose()
Out[28]:
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