Skip to content

Instantly share code, notes, and snippets.

@leleofg
Last active February 1, 2018 01:12
Show Gist options
  • Select an option

  • Save leleofg/d16894e2ccf4235daf7fc4449cc3f7ae to your computer and use it in GitHub Desktop.

Select an option

Save leleofg/d16894e2ccf4235daf7fc4449cc3f7ae to your computer and use it in GitHub Desktop.
Numpy array
import numpy as np
mylist = [1, 2, 3, 4]
x = np.array(mylist)
print(x)
# array([1, 2, 3, 4])
y = np.array([1, 2, 3], [4, 6, 12])
print(y)
# array([[1, 2, 3],[4, 6, 12]])
y.shape
# (2, 3) = two lines and 3 columns
n = np.arange(0, 30, 2) # start in 0, go to 30, and step = 2
print(n)
# 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28
n = np.reshape(3, 5) # transform in matriz with 3 lines and 5 columns
o = np.linspace(0, 4, 9)
print(o)
#array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. ])
o.resize(3, 3) # matriz with 3 lines and 3 columns
np.ones(3,2) # matriz only numbers 1 with 3 lines and 2 columns
np.zeros(2,3) # matriz only numbers 0 with 2 lines and 3 columns
np.eye(3)
#array([[ 0., 1., 0.], [ 0., 0., 1.], [ 0., 0., 0.]])
np.diag(matriz) # return the main diagonal
np.array([1,2,3] * 3) # repeat my array 3 times and return only one array
np.repeat([1,2,3], 3) # same result of the last instruction
array = np.ones([2,3], int)
# array([[1, 1, 1],
# [1, 1, 1]])
np.vstack([array, 2*p])
# array([[1, 1, 1],
# [1, 1, 1],
# [2, 2, 2],
# [2, 2, 2]])
np.hstack([array, 2*p])
# array([[1, 1, 1, 2, 2, 2],
# [1, 1, 1, 2, 2, 2]]),
np.arange(13)**2
# array([0,1,4,9,16,25,36,49,64,81,100,121,144])
s[-4:]
# array([144,122,100,81])
s[-5::-2]
#array([64,36,16,4,0])
s[1:2]
#array([1,4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment