import numpy as np
lst = [1, 2, 3, 4]
lst
[1, 2, 3, 4]
lst = range(100)
range(1, 10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
lst[1][2]
6
lst[1][1:]
[5, 6]
lst[2][1:]
[8, 9]
np.array(lst)
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr = np.array(lst)
arr
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr[1:, :2]
array([[4, 5], [7, 8]])
arr[0]
array([1, 2, 3])
arr[:, 0]
array([1, 4, 7])
lst = [1, 4, 7]
def sum_list(lst):
sum = 0
for el in lst:
sum += el
return sum
sum_list(lst)
12
big_lst = range(100000)
%timeit sum_list(lst)
1000000 loops, best of 3: 506 ns per loop
big_lst = range(100000)
%timeit sum_list(big_lst)
100 loops, best of 3: 7.01 ms per loop
arr = np.array(big_lst)
%timeit sum_list(arr)
10 loops, best of 3: 70.7 ms per loop
%timeit arr.sum()
10000 loops, best of 3: 77.9 us per loop
np.sum(arr)
4999950000
np.sum(big_lst)
4999950000
np.mean(big_lst)
49999.5
np.std(big_lst)
28867.513458037916
np.std?
np.std(big_lst, ddof=1)
28867.657796687741
range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(9)
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
np.reshape?
np.reshape(np.arange(9), (3, 3))
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
np.zeros(10)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.zeros((3, 2))
array([[ 0., 0.], [ 0., 0.], [ 0., 0.]])
np.eye(10)
array([[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])
np.diag(np.eye(10))
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
np.diag(np.diag(np.eye(10)))
array([[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])
np.diag([1, 6, 4])
array([[1, 0, 0], [0, 6, 0], [0, 0, 4]])
np.random.randn(4, 3)
array([[ 0.45727794, 0.95308707, -0.67349952], [ 0.42982752, 1.84326079, 0.88624181], [ 0.09784898, 0.65451898, 1.1747724 ], [ 0.72163378, -0.02091747, -1.01356388]])
randn(4, 3)
array([[-0.2567363 , -0.01732275, 1.41093738], [ 1.07042878, -0.21508292, -0.54726137], [ 0.12873854, -0.79321983, 0.15202132], [ 0.19978042, -1.87479486, -0.75646302]])
mat = np.reshape(np.arange(9), (3, 3))
mat
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
np.sum(mat)
36
np.sum?
np.sum(mat, axis=0)
array([ 9, 12, 15])
mat
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
np.sum(mat, axis=1)
array([ 3, 12, 21])
mat
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
mat + 5
array([[ 5, 6, 7], [ 8, 9, 10], [11, 12, 13]])
mat * 10
array([[ 0, 10, 20], [30, 40, 50], [60, 70, 80]])
mat2 = mat**3
mat2
array([[ 0, 1, 8], [ 27, 64, 125], [216, 343, 512]])
mat + mat2
array([[ 0, 2, 10], [ 30, 68, 130], [222, 350, 520]])
mat
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
mat[1:, 1:] + mat2
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /Users/ely/Downloads/classes/pydata_workshop_20121216/<ipython-input-69-6b2e1601ecfe> in <module>() ----> 1 mat[1:, 1:] + mat2 ValueError: operands could not be broadcast together with shapes (2,2) (3,3)
mat[:, 0]
array([0, 3, 6])
mat2
array([[ 0, 1, 8], [ 27, 64, 125], [216, 343, 512]])
mat[:, 0] + mat2
array([[ 0, 4, 14], [ 27, 67, 131], [216, 346, 518]])
mat2.T
array([[ 0, 27, 216], [ 1, 64, 343], [ 8, 125, 512]])
mat[:, 0].T
array([0, 3, 6])
mat2.shape
(3, 3)
mat3 = np.array([range(3), range(3)])
mat3
array([[0, 1, 2], [0, 1, 2]])
mat3.shape
(2, 3)
lst
[1, 4, 7]
lst == 4
False
arr = np.array(lst)
arr
array([1, 4, 7])
arr == 4
array([False, True, False], dtype=bool)
arr[1]
4
arr[:2]
array([1, 4])
arr[arr == 4]
array([4])
arr == 4
array([False, True, False], dtype=bool)
arr >= 4
array([False, True, True], dtype=bool)
arr[arr >= 4]
array([4, 7])
#1: create 4x4 identity matrix, and set the 2nd row to be all 10s
#2: create 6x3 random matrix, and compute the column-wise sample standard deviation
ex_one = np.eye(4)
ex_one
array([[ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])
ex_one[1, :].shape
(4,)
ex_one[1, :] = 10 * np.ones(ex_one[1, :].shape)
ex_one
array([[ 1., 0., 0., 0.], [ 10., 10., 10., 10.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])
ex_one[1, :] = 0
ex_one[1, :] = 10
ex_one
array([[ 1., 0., 0., 0.], [ 10., 10., 10., 10.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])
m2 = np.random.randn(6, 3)
m2
array([[-0.99288657, 2.29884659, 1.23340896], [ 1.20568734, 0.58893783, -1.19805267], [ 0.01238457, -0.89575218, -0.32703349], [-1.33648817, -1.41533797, -0.78696767], [-0.311132 , -1.35272218, 0.29392369], [-1.02919624, -0.01262656, 0.11126516]])
np.std(m2, axis=0, ddof=1)
array([ 0.93596566, 1.42572445, 0.86093872])
def func(mat):
return np.std(mat, axis=0, ddof=1)
mat = np.reshape(np.arange(9), (3, 3))
mat
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
means = np.mean(mat, axis=0)
means
array([ 3., 4., 5.])
dev = mat - means
se = dev**2
np.sum(se, axis=0) / (3 - 1)
array([ 9., 9., 9.])
np.insert?