cfs ureca
exercises for evaluation
#collapse_show
%lsmagic
#collapse
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
import datetime
from matplotlib import animation
import random
fig, ax = plt.subplots()
theta = np.linspace(0.5*np.pi, 2.5*np.pi, 100) # 0.5pi to make it start in the top centre of circle
r = np.sqrt(25.0) # radius of circle
x = r * np.cos(theta)
y = r * np.sin(theta)
circumference, = ax.plot(x, y, marker='o')
# to adjust BOTH the x and y axes such that the width between each divider is the same
ax.set_aspect(1)
plot_lim = r + 0.25
plt.xlim(-plot_lim, plot_lim)
plt.ylim(-plot_lim, plot_lim)
clock_timing_label_array = np.linspace(0.5*np.pi, 2.5*np.pi, 13)
clock_timing_label_x = (r - 0.2) * np.cos(clock_timing_label_array)
clock_timing_label_y = (r - 0.2) * np.sin(clock_timing_label_array)
time_coordinates = np.array((clock_timing_label_x, clock_timing_label_y)).T
ref_lines = []
for i, (x_pos, y_pos) in enumerate(time_coordinates):
if(i != 0):
plt.annotate("{}".format(i), (-x_pos, y_pos))
# Drawing reference lines to the clock for fun
sx = [0, -x_pos]
sy = [0, y_pos]
l, = ax.plot(sx, sy)
ref_lines.append(l)
clock_hand_x_array, clock_hand_y_array = [], []
clock_hand, = plt.plot([], [], 'r')
clock_hand_ticks = np.linspace(0.5*np.pi, 2.5*np.pi, 61)
clock_hand_ticks_x = (r - 0.2) * np.cos(clock_hand_ticks)
clock_hand_ticks_y = (r - 0.2) * np.sin(clock_hand_ticks)
seconds_coordinates = np.array((clock_hand_ticks_x, clock_hand_ticks_y)).T
def init():
return clock_hand,
def update(frame):
clock_hand_x_array, ydata = [], []
clock_hand_x_array = [0, -frame[0]]
clock_hand_y_array = [0, frame[1]]
clock_hand.set_data(clock_hand_x_array, clock_hand_y_array)
plt.title('Analog Clock {}'.format(datetime.datetime.now().strftime("%H:%M:%S")), fontsize=18)
return clock_hand,
ani = animation.FuncAnimation(fig,
update,
frames=seconds_coordinates,
init_func=init,
blit=True,
interval=1000
)
plt.grid(linestyle='--')
# This part of the code increase the size of the graph since default is too small
fig_size = plt.gcf().get_size_inches() #Get current size
sizefactor = 1.5 #Set a zoom factor
# Modify the current size by the factor
plt.gcf().set_size_inches(sizefactor * fig_size)
plt.title('Analog Clock 😃', fontsize=18)
def generate_color():
r = lambda: random.randint(0,255)
color = '#{:02x}{:02x}{:02x}'.format(r(), r(), r())
return color
def onclick(event):
circumference.set_color(generate_color())
def press(event):
if event.key=='y':
for l in ref_lines:
l.set_color(generate_color())
fig.canvas.mpl_connect('button_press_event', onclick)
fig.canvas.mpl_connect('key_release_event', press)
#mpl_connect connects event onclick with string 'button_press_event'
#plt.savefig("plot_circle_matplotlib_01.png", bbox_inches='tight')
plt.show()
import pandas as pd
df = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
cik = df[0][['CIK']]
cik.to_clipboard()
import graphviz
def gv(s): return graphviz.Source('digraph G{ rankdir="LR"' + s + '; }')
gv('diagrams[shape=box3d width=1 height=0.7] my -> diagrams -> are->very; diagrams->very->complex; my->complex')
import numpy as np
import matplotlib.pyplot as plt
num_pts=12 # number of points on the circle
ps = np.arange(num_pts)
# j = np.sqrt(-1)
pts = (np.exp(2j*np.pi/num_pts)**ps)
fig, ax = plt.subplots(1)
ax.plot(pts.real, pts.imag , 'o')
ax.set_aspect(1)
plt.show()
# %matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
import datetime
fig, ax = plt.subplots()
ax.set(xlim = (-1.5, 1.5), ylim = (-1.5, 1.5));
plt.axis('scaled')
ax.add_artist( plt.Circle((0,0), 1.1, fill=False) )
num_pts = 12 # number of points on the circle
pts = (np.exp(2j*np.pi/num_pts)** np.arange(num_pts) )
ax.plot(pts.real, pts.imag , 'o')
hand, = ax.plot([0, 0], [0, 1])
analogClock = ax.text(0, 0, '20', ha='center', va='center')
# plt.show()
# animation function. This is called sequentially
def animate(i):
analogClock.set_text( str(i) )
return (analogClock,)
anim = animation.FuncAnimation(fig, animate,
interval=1000, blit=True)
rc('animation', html='jshtml')
anim
import numpy as np
import pandas as pd
df1 = pd.read_stata('http://www.stata-press.com/data/r11/auto.dta')
df1[4:8] # df1.iloc[4:8,:]
#df1 = df1[df1.rep78.notna()]
df1.rep78 = df1.rep78.astype('Int8')
len(df1), df1.shape, df1.size #df1.count()
df1.groupby('rep78').size()
df = df1.groupby('rep78').agg({'mpg': ['size','mean', 'median'], 'price':'max', 'length':'min'})
df = df1.groupby(['rep78', 'foreign']).size().unstack()
#df.index = df.index.astype('int32')
df
'h' + str(3)
def my_aggfunc(arg): return(arg.name + str(len(arg)))
df = df1.pivot_table(index='rep78', columns='foreign', aggfunc=len, margins=True)['mpg'].fillna(0) #.astype('int32')
df
df1.groupby(['rep78', 'foreign']).agg({'mpg': 'mean', 'weight':'max'})
for i in [['x','y'], ['a','b']]:
vars()[i[0]] = i[1]
[x, a]
df = df1.pivot_table(index='rep78', columns='foreign', aggfunc=['mean'], values=['mpg'], margins=True).fillna(0)
df1a = df.T.reset_index(drop=True).T
df = df1.pivot_table(index='rep78', columns='foreign', aggfunc=['max'], values=['weight'], margins=True).fillna(0)
df1b = df.T.reset_index(drop=True).T
df1c = df1a.iloc[0:0]
df1c
for i in df1a.index:
df1c = df1c.append(df1a.loc[i])
df1c = df1c.append(df1b.loc[i])
df1c
l = df1.pivot_table(index='rep78', columns='foreign', aggfunc=['mean'], values=['mpg'], margins=True).fillna(0)
r = df1.pivot_table(index='rep78', columns='foreign', aggfunc=['max'], values=['weight'], margins=True).fillna(0)
d = l.join(r)
display(d.columns.levels)
d.columns = d.columns.droplevel(0)
d.stack(0)
# replace droplevel(0) with join(level 0 and 1)
# use ordered Categorical
df1.plot.scatter('weight', 'mpg')
import seaborn as sns
sns.lmplot(x="weight", y="mpg", data=df1, fit_reg=True)
# X = df1["weight"]
# y = df1["mpg"]
# X = sm.add_constant(X)
# model = sm.OLS(y, X).fit()
# predictions = model.predict(X)
# model.summary()
['inc80'[-2:],
(lambda x: [x[:-2], x[-2:]])('inc80')]
# df['variable'].apply(lambda x: x[-2:])
import re
# .match (first only) vs .findall (all)
[
(re.sub(r'(\D+)(\d+)', r'\2 \1', 'inc80')),
re.split(r'8', 'inc80'),
(re.search(r'(\D+)(\d+)', 'inc80').groups())
]
import pandas as pd
df2 = pd.read_stata('http://www.stata-press.com/data/r11/reshape1.dta')
df2 = df2.astype('int32')
df2
[i for i in list(df2.columns) if i[-2:-1]!='8' ]
a = df2.set_index(['id', 'sex'])
for i in a.T :
for j in a :
display([i, j])
# for loop
table = []
for i in range(df2.shape[0]):
temp = df2.iloc[i]
for j in range(3):
r = [temp[0],80+j,temp[1],temp[2+j],temp[5+j]]
table.append(r)
pd.DataFrame(table, columns = ["id","year","sex","inc","ue"])
df = df2.melt(['id', 'sex'])
df[['variable', 'year']] = df['variable'] .apply (lambda x: pd.Series([x[:-2], x[-2:]]))
#df[['variable', 'year']] = df['variable'].str.extract('(\D+)(\d+)'); display(df)
df.pivot_table(index=['id', 'year', 'sex'], columns='variable', values = 'value') .reset_index()
## df2 = pd.read_stata('http://www.stata-press.com/data/r11/reshape1.dta')
df = df2.melt(['id', 'sex'])
df['year'] = df['variable']
df['variable'] = df['variable'] .apply(lambda x: x[:-2])
df['year'] = df['year'] .apply(lambda x: x[-2:])
df.pivot_table(index=['id', 'year', 'sex'], columns='variable', values = 'value') .reset_index()
df = df2.melt(['id', 'sex'])
df[['variable', 'year']] = df['variable'] .apply (lambda x: pd.Series([x[:-2], x[-2:]]))
df.set_index(['id', 'year', 'sex', 'variable']) .unstack()
df = df2.set_index(['id', 'sex']).stack().reset_index()
df[['level_2', 'year']] = df['level_2'].str.extract('(\D+)(\d+)');
df = df.set_index(['id', 'year', 'sex', 'level_2']).unstack()
display(df)
[df.shape,
df.index.names,
df.columns.names,
df.columns.levels]
df3 = pd.read_csv('http://www.ntu.edu.sg/home/fscheong/public/citycolors.csv'); df3
a = df3.melt('city')
b = a.groupby('city') .agg({'value':'max'}) .reset_index()
c = a.merge(b, on='city')
c [c['value_x']==c['value_y']] [['city','variable']] .reset_index(drop=True) # .iloc[:,0:2]
cols = pd.MultiIndex.from_tuples([("a", "b"), ("a", "c")])
df = pd.DataFrame([[1,2], [3,4]], columns=cols)
df.columns.names = ['x', 'y']
display(df) #display(df['a'][['c']])
[df.shape,
list(df.index),
list(df.columns),
df.index.names,
df.columns.names,
df.columns.levels]
#df.columns = df.columns.droplevel()
df.columns = [col[1] for col in df.columns]
#df.columns = ['_'.join(col) for col in df.columns]
df