Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Numpy
import numpy as np
a = np.arange(6)
print(a)
#[Output]:
#[0 1 2 3 4 5]
## 1. Reshaping
a.reshape(2,3)
#[Output]:
#array([[0, 1, 2],
# [3, 4, 5]])
## 2. Flattening the array
a.ravel()
#[Output]:
#array([0, 1, 2, 3, 4, 5])
## 3.numpy.ndarray.flat
x = np.arange(1, 7).reshape(2, 3)
print(x)
#[Output]:
#array([[1, 2, 3],
# [4, 5, 6]])
print(x.flat[3])
#[Output]:
#4
print(x.T)
#[Output]:
#[[1 4]
# [2 5]
# [3 6]]
print(x.T.flat[3])
#[Output]:
#5
## 4.Flatten
a = np.array([[1,2], [3,4]])
a.flatten()
#[Output]:
#array([1, 2, 3, 4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.