In [1]:
my_list = [1,2,3] # list
In [2]:
my_list
Out[2]:
In [3]:
import numpy as np
In [4]:
np.array(my_list)
Out[4]:
In [5]:
my_lol = [[1,2,3],[4,5,6],[7,8,9]] # list of lists
In [7]:
np.array(my_lol) # cast lol as 2d array. 2d == 2 levels of brackets
Out[7]:
In [10]:
np.arange(0,10,2) # (arange == 'array range')
Out[10]:
In [20]:
np.zeros(3) # 1d matrix of zeros
Out[20]:
In [21]:
np.zeros((5,5)) # 2d matrix of zeros, size defined by tuple
Out[21]:
In [15]:
np.ones((2,4))
Out[15]:
In [18]:
np.linspace(0,5,10) # 0-5 in ten equal steps. one-dimensional because only one bracket.
Out[18]:
In [25]:
np.eye(6) # identity matrix, square array with diagonal streak of ones; useful in linear algebra
Out[25]:
In [30]:
np.random.rand(8) # random, evenly distributed values between 0 and 1
Out[30]:
In [29]:
np.random.rand(5,5) # note that this does not take a tuple
Out[29]:
In [32]:
np.random.randn(5,5) # random, normally-distributed values, distribution centered on 0
Out[32]:
In [60]:
np.random.randint(100) # I think, in py2.7, low is 0 by default, high is input.
Out[60]:
In [45]:
np.random.randint(3,9,(3,3)) # random value between low (inclusive) and high (exclusive). eg: 3 <= value < 9
Out[45]:
In [61]:
arr = np.arange(25)
In [62]:
ranarr = np.random.randint(0,50,10)
In [74]:
arr = arr.reshape(5,5)
arr
Out[74]:
In [64]:
ranarr.max()
Out[64]:
In [67]:
ranarr.min()
Out[67]:
In [68]:
ranarr.argmax()
Out[68]:
In [70]:
ranarr.argmin()
Out[70]:
In [75]:
arr.shape
Out[75]:
In [72]:
ranarr.shape
Out[72]:
In [77]:
arr.dtype
Out[77]:
In [78]:
ranarr.dtype
Out[78]:
In [80]:
from numpy.random import randint
In [81]:
randint(2,13)
Out[81]:
In [ ]: