Skip to content

Instantly share code, notes, and snippets.

@Robofied
Created February 15, 2019 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robofied/efc9758f7b64b79b6585a92896dd8a7e to your computer and use it in GitHub Desktop.
Save Robofied/efc9758f7b64b79b6585a92896dd8a7e to your computer and use it in GitHub Desktop.
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