#!/usr/bin/env python # coding: utf-8 # In[1]: import numpy as np # In[3]: A = np.array([[1, 2, 3], [2, 3, 4]]) u = np.array([10, 20, 30]) # In[4]: A + u # In[5]: A * u # In[6]: T = np.array([[[2, 3], [4, 1]], [[1, 10], [5, 9]]]) # In[7]: T.shape # In[8]: T + np.array([100, 200]) # In[15]: T.shape # In[11]: T + np.array([[100, 200]]) # In[13]: np.array([[100, 200]]).shape # In[12]: T + np.array([[100], [200]]) # In[14]: np.array([[100], [200]]).shape # In[16]: np.array([[1, 2, 3]]) + np.array([[10], [20]]) # In[17]: np.array([[1, 2, 3]]) * np.array([[10], [20]]) # In[19]: np.array([[10], [20]]) @ np.array([[1, 2, 3]]) # In[21]: np.array([1, 2, 3, 4, 5, 6]).reshape(2, 3) # In[22]: np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 3) # In[23]: np.array([1, 2, 3, 4, 5, 6]).reshape(3, -1) # In[24]: np.array([1, 2, 3, 4, 5, 6]).T # In[26]: np.array([1, 2, 3, 4, 5, 6]).reshape(1, -1).T # In[28]: np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1) # In[29]: np.array([1, 2, 3, 4, 5, 6]).reshape(2, 1, 3) # In[30]: x = np.array([1, 2, 3, 4, 5, 6]) # In[31]: x # In[33]: x[x % 2 == 0] = 0 # In[34]: x # In[43]: x = np.array([1, 2, 3, 4, 5, 6]) y = np.array([10, 20, 30, 40, 50, 60]) np.where(x % 2 == 0, 100, x) # In[45]: np.linalg.solve(np.array([[3, 2], [1, 4]]), np.array([10, 20])) # In[62]: np.random.multivariate_normal(np.array([0, 0]), np.array([[1, 0.7], [0.7, 1]]), size=5).T # In[80]: x, y = np.random.multivariate_normal(np.array([0, 0]), np.array([[1, 0.9], [0.9, 1]]), size=100).T # In[81]: import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') # In[82]: plt.plot(x, y, 'o') # In[83]: import pandas as pd # In[84]: s = pd.Series([10, 20, 30]) # In[85]: s # In[86]: s = pd.Series([10, 20, 30], index=['Alice', 'Bob', 'Claudia']) # In[87]: s # In[88]: s['Alice'] # In[89]: s[0] # In[90]: s[1:] # In[95]: u = pd.Series([30, 10, 40], index=['Angela', 'Alice', 'Claudia']) # In[96]: s # In[97]: u # In[98]: u + s # In[100]: nan = float("NaN") # In[102]: x = nan if x == nan: print("x is NaN") # In[103]: float("NaN") == float("NaN") # In[105]: x = nan if x != x and isinstance(x, float): print("x is NaN") # In[108]: class MyNaN(object): def __eq__(self, other): return False # In[109]: x = MyNaN() y = MyNaN() x == y # In[110]: None # In[111]: None == float("NaN") # In[112]: None == None # In[115]: pd.isna(u + s).sum() # In[116]: s # In[117]: s.mean() # In[118]: s.sum() # In[119]: s # In[120]: s['Alice'] # In[121]: s[0] # In[122]: numbers = pd.Series([10, 20, 30, 40], index=[3, 2, 0, 1]) # In[123]: numbers # In[125]: numbers[0] # In[126]: numbers.iloc[0] # In[128]: numbers.loc[0] # In[129]: s # In[130]: s = pd.Series([10, 20, 30, 40, 50], index=['Alice', 'Bob', 'Claudia', 'Daniel', 'Mark']) # In[131]: s # In[132]: s[1:3] # In[136]: s['Bob':'Daniel'] # In[135]: s[:2] # In[137]: s # In[138]: s['Anna':'Dmitry'] # In[143]: s = pd.Series([10, 20, 30, 40, 50], index=['Bob', 'Claudia', 'Daniel','Alice', 'Mark']) # In[144]: s # In[145]: s['Anna':'Dmitry'] # In[146]: s['Daniel':'Mark'] # In[147]: uuu = pd.Series([10, 20, 30, 40, 50], index=['Bob', 'Bob', 'Bob','Bob', 'Mark']) # In[149]: uuu['Bob'] # In[150]: uuu['Mark'] # In[151]: uuu['Bob':'Mark'] # In[153]: uuu + pd.Series([100, 200, 300, 400], index=['Bob', 'Bob', 'Mark', 'Mark']) # In[154]: s # In[155]: s # In[156]: s[s > 20] # In[157]: s > 20 # In[158]: b = pd.Series([0, 'hello']) # In[162]: b.replace({'hello': np.nan}) # In[165]: pd.Series([float("NaN"), 1]).replace({float("NaN"): 100}) # In[169]: pd.Series([float("NaN"), 1]).fillna(100) # In[170]: pd.Series([None, 1]).fillna(100) # In[172]: pd.Series([1, None, 2, 3, None, 4, None]).fillna(method='backfill') # In[173]: pd.Series([1, None, 2, 3, None, 4]).fillna(method='ffill') # In[178]: pd.Series([1, None, 2, 3, None, None, 4]).interpolate( method='quadratic') # In[ ]: