#!/usr/bin/env python # coding: utf-8 # # NumPy basics # In[1]: import numpy as np # Set seed for reproducibility np.random.seed(seed=123) # ## Scalars # In[2]: sk_id_curr = np.array(100001) print ("sk_id_curr: ", sk_id_curr) # Number of dimensions print ("sk_id_curr ndim: ", sk_id_curr.ndim) # Dimensions print ("sk_id_curr shape:", sk_id_curr.shape) # Size of elements print ("sk_id_curr size: ", sk_id_curr.size) # Data type print ("sk_id_curr dtype: ", sk_id_curr.dtype) # ## 1-D Array # In[3]: sk_id_curr = np.array([100001, 100005, 100013, 100028, 100038, 100042]) print ("sk_id_curr:", sk_id_curr) print ("sk_id_curr ndim: ", sk_id_curr.ndim) print ("sk_id_curr shape:", sk_id_curr.shape) print ("sk_id_curr size: ", sk_id_curr.size) print ("sk_id_curr dtype: ", sk_id_curr.dtype) # ## 2-D array (matrix) # In[4]: sk_id_curr = np.array([[100001, 100005], [100013, 100028], [100038, 100042]]) print ("sk_id_curr:", sk_id_curr) print ("sk_id_curr ndim: ", sk_id_curr.ndim) print ("sk_id_curr shape:", sk_id_curr.shape) print ("sk_id_curr size: ", sk_id_curr.size) print ("sk_id_curr dtype: ", sk_id_curr.dtype) # ## Functions # In[5]: print ("np.zeros((2,2)):", np.zeros((2,2))) print ("np.ones((2,2)):", np.ones((2,2))) print ("np.eye((2)):", np.eye((2))) print ("np.random.random((2,2)):", np.random.random((2,2))) # # Accessing # # ## Indexing # In[6]: sk_id_curr = np.array([100001, 100005, 100013, 100028, 100038, 100042]) print ("sk_id_curr[0]: ", sk_id_curr[0]) sk_id_curr[0] = 0 print ("sk_id_curr: ", sk_id_curr) # ## Slicing # In[7]: sk_id_curr = np.array([[100001, 100005, 111111], [100013, 100028, 222222], [100038, 100042, 333333]]) print (sk_id_curr) print ("sk_id_curr column 1: ", sk_id_curr[:, 1]) print ("sk_id_curr row 0: ", sk_id_curr[0, :]) print ("sk_id_curr rows 0,1,2 & cols 1,2:", sk_id_curr[:3, 1:3]) # ## Boolean array indexing # In[8]: sk_id_curr = np.array([[100001, 100005, 111111], [100013, 100028, 222222], [100038, 100042, 333333]]) print ("sk_id_curr:", sk_id_curr) print ("sk_id_curr > 111111:", sk_id_curr > 111111) print ("sk_id_curr[sk_id_curr > 111111]:", sk_id_curr[sk_id_curr > 111111]) # # Array math # # ## Basic math # In[9]: x = np.array([[1,2], [3,4]], dtype=np.float64) y = np.array([[1,2], [3,4]], dtype=np.float64) print ("x + y:", np.add(x, y)) # or x + y print ("x - y:", np.subtract(x, y)) # or x - y print ("x * y:", np.multiply(x, y)) # or x * y # ## Dot product # In[10]: a = np.array([[1,2,3], [4,5,6]], dtype=np.float64) # we can specify dtype b = np.array([[7,8], [9,10], [11, 12]], dtype=np.float64) print (a.dot(b)) # ## Sum across a dimension # In[11]: x = np.array([[1,2],[3,4]]) print (x) print ("sum all: ", np.sum(x)) # adds all elements print ("sum by col: ", np.sum(x, axis=0)) # add numbers in each column print ("sum by row: ", np.sum(x, axis=1)) # add numbers in each row # ## Transposing # In[12]: print ("x:", x) print ("x.T:", x.T) # ## Broadcasting # In[13]: x = np.array([[1,2], [3,4]]) y = np.array([5, 6]) z = x + y print ("z:", z) # ## Reshaping # In[14]: x = np.array([[1,2], [3,4], [5,6]]) print (x) print ("x.shape: ", x.shape) y = np.reshape(x, (2, 3)) print ("y.shape: ", y.shape) print ("y:", y)