Skip to content

Instantly share code, notes, and snippets.

@delwar2016
Last active February 13, 2017 15:57
Show Gist options
  • Save delwar2016/dbb2b0ab04826732e583ba8268f062bc to your computer and use it in GitHub Desktop.
Save delwar2016/dbb2b0ab04826732e583ba8268f062bc to your computer and use it in GitHub Desktop.
Explain NumPy array object
import numpy as np
# Manual construction of arrays
# one dimension array
a = np.array([0,1,2,3])
print(a)
print ('dimension', a.ndim)
print('shape', a.shape)
print('length', len(a))
print('Datatype', a.dtype)
# Functions for creating arrays
# Evenly spaced
a = np.arange(10)
print('0,1,2,.....n-1', a)
b = np.arange(1, 9, 2)
print('start 1, end 9(exclusive), step 2', b)
# number of points
a = np.linspace(0, 1, 6)
print('start, end, num-points', a)
a = np.linspace(0, 1, 5, endpoint=True)
print('start, end, num-points, endpoint = false', a)
# Common arrays:
a = np.ones((3, 3)) # reminder: (3, 3) is a tuple
b = np.zeros((2, 2))
c = np.eye(3)
d = np.diag(np.array([1, 2, 3, 4]))
a = np.random.rand(4) # uniform in [0, 1]
b = np.random.randn(4) # Gaussian
np.random.seed(1234) # Setting the random seed
# Indexing and slicing
a = np.arange(10)
print('Reverse', a[::-1])
print('Slicing', a[2:9:3]) # [start:end:step] Arrays, like other Python sequences can also be sliced
Source: http://www.scipy-lectures.org/intro/numpy/array_object.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment