%%javascript
Jupyter.keyboard_manager.command_shortcuts.remove_shortcut('up');
Jupyter.keyboard_manager.command_shortcuts.remove_shortcut('down');
from IPython import display
Imports ..
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import axes3d
import pandas as pd
from rotate import rotanimate
import matplotlib.animation as animation
import subprocess
from IPython.display import Image
from matplotlib import cm
from IPython.display import Video
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import scale
plt.style.use('seaborn-whitegrid')
plt.rcParams["figure.figsize"] = [10,6]
#import numpy as np
#import matplotlib.pyplot as plt
#from mpl_toolkits.mplot3d import Axes3D
from sklearn import decomposition
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from itertools import chain
import math
import seaborn as sns; sns.set() # styling
from functools import reduce
import functools
import operator
from numpy.polynomial import polynomial as P
from scipy.interpolate import CubicSpline
from scipy.interpolate import interp1d
#### Defaults
sns.set_style("darkgrid", {"axes.facecolor": ".9"})
plt.rcParams.update({'font.size': 12})
import seaborn as sns; sns.set() # styling
Functions
def foldl(func, acc, xs):
return functools.reduce(func, xs, acc)
# tests
#print(foldl(operator.sub, 0, [1,2,3])) # -6
#print(foldl(operator.add, 'L', ['1','2','3'])) # 'L123'
def scanl_plus(data):
'''
returns list of successive reduced values from the list (see haskell foldl)
'''
return [0] + [sum(data[:(k+1)]) for (k,v) in enumerate(data)]
def make1D (data):
return np.array(list(map (lambda x : [x],data)))
def celsius_to_fahr(temp):
return 9/5 * temp + 32
def gen_answers_from_alphas(inputs, new_alphas):
return (np.matmul(inputs,new_alphas))
def polyfitx(x, y, degree):
results = {}
coeffs = np.polyfit(x, y, degree)
# Polynomial Coefficients
results['polynomial'] = coeffs.tolist()
# r-squared
p = np.poly1d(coeffs)
# fit values, and mean
yhat = p(x) # or [p(z) for z in x]
ybar = np.sum(y)/len(y) # or sum(y)/len(y)
ssreg = np.sum((yhat-ybar)**2) # or sum([ (yihat - ybar)**2 for yihat in yhat])
sstot = np.sum((y - ybar)**2) # or sum([ (yi - ybar)**2 for yi in y])
results['determination'] = ssreg / sstot
return results
def showECResults (title,ec_alphas, actual_alphas, principle_vals,ans, ans_scaler):
AxH2 = principle_vals.dot (ec_alphas)
new_nnAnsH2 = ans_scaler.inverse_transform (AxH2)
rH2 = np.corrcoef(ans,(new_nnAnsH2.reshape(-1,)))
rSq = (rH2[1,0])**2
print(rSq)
fig, ax = plt.subplots(1,2)
ax[0].plot(ans,new_nnAnsH,'o', color='green',marker=".", markersize=1);
ax[1].plot(ec_alphas, color='green',marker=".", markersize=10);
ax[1].plot(actual_alphas, ':o', color='orange',marker=".", markersize=12);
fig.suptitle(title)
plt.show()
def left_inverse (m ):
return (np.linalg.solve (m.T.dot(m), m.T))
# Two ways to compute this ..
# rsq = 1 - residual / sum((y - y.mean())**2)
#or
# rsq = 1 - residual / (n * y.var())
# https://stackoverflow.com/questions/3054191/converting-numpy-lstsq-residual-value-to-r2
def lstsq_rsq (output_from_lstsq, inputs, answers):
rsq_ = 1 - output_from_lstsq[1] / sum ((answers - answers.mean())**2)
return (rsq_[0])
def drawVector (origins,vectors):
vectors_np = np.array(vectors)
#origins = ([[0,0],[0,0]])
V = vectors_np
origins_t = zip(*origins)
fig, ax = plt.subplots()
fig.set_size_inches(8,8)
origins_l = np.array(list(map(lambda t: list(t),origins_t)))
q = ax.quiver(*origins_l, (list(V[:,0])), (list(V[:,1])), color=['r','b','g','r'], scale=1,units='xy')
ax.set_aspect('equal')
lim = 7
plt.xlim(-lim,lim)
plt.ylim(-lim,lim)
plt.title('Vector Tutorial',fontsize=10)
#plt.savefig('savedFig.png', bbox_inches='tight')
#print (type(q))
plt.show()
def splineCoeffsToAns (title,c,actual_sol, scaleF):
plen = 5
d = 0.3
xa = np.linspace ( 0, d,plen)
xb = np.linspace ( d,2*d,plen)
xc = np.linspace (2*d,3*d,plen)
xd = np.linspace (3*d,4*d,plen)
xe = np.linspace (4*d,5*d,plen)
xf = np.linspace (5*d,6*d,plen)
xg = np.linspace (6*d,7*d,plen)
xh = np.linspace (7*d,8*d,plen)
xi = np.linspace (8*d,9*d,plen)
#xj = np.linspace (9*d,2,100)
f1d = list(map (lambda xp : c[0][0] + c[0][1]*xp + c[0][2]*xp**2 + c[0][3]*xp**3,xa))
f2d = list(map (lambda xp : c[1][0] + c[1][1]*xp + c[1][2]*xp**2 + c[1][3]*xp**3,xb))
f3d = list(map (lambda xp : c[2][0] + c[2][1]*xp + c[2][2]*xp**2 + c[2][3]*xp**3,xc))
f4d = list(map (lambda xp : c[3][0] + c[3][1]*xp + c[3][2]*xp**2 + c[3][3]*xp**3,xd))
f5d = list(map (lambda xp : c[4][0] + c[4][1]*xp + c[4][2]*xp**2 + c[4][3]*xp**3,xe))
f6d = list(map (lambda xp : c[5][0] + c[5][1]*xp + c[5][2]*xp**2 + c[5][3]*xp**3,xf))
f7d = list(map (lambda xp : c[6][0] + c[6][1]*xp + c[6][2]*xp**2 + c[6][3]*xp**3,xg))
f8d = list(map (lambda xp : c[7][0] + c[7][1]*xp + c[7][2]*xp**2 + c[7][3]*xp**3,xh))
f9d = list(map (lambda xp : c[8][0] + c[8][1]*xp + c[8][2]*xp**2 + c[8][3]*xp**3,xi))
#f10d = list(map (lambda xp : c[9][0] + c[9][1]*xp + c[9][2]*xp**2 + c[9][3]*xp**3,xj))
ecAns = (f1d+f2d+f3d+f4d+f5d+f6d+f7d+f8d+f9d) #+f10)
print (f1d[0],f5d[0])
subsample_sol = np.array(actual_sol[::6])
fig9, ax9 = plt.subplots()
ax9.set_title ("EC vs Actual -> " + title)
ax9.plot(ecAns,':o', color = "orange",markersize = 4,label="EC Sol")
ax9.plot(subsample_sol*scaleF, ':o',markersize = 4, label = "Known Sol")
ax9.plot ([0,plen,2*plen,3*plen,4*plen,5*plen,6*plen,7*plen,8*plen],
[f1d[0],f2d[0],f3d[0],f4d[0],f5d[0],f6d[0],f7d[0],f8d[0],f9d[0]], 'o', color = "red", label = "EC Spline Pos")
ax9.legend()
#ax9.set_xlabel(r'$\Delta_i$', fontsize=15)
#ax9.set_ylabel(r'$\Delta_{i+1}$', fontsize=15)
#ax9.set_title('Volume and percent change')
ax9.set_xlabel( 'Pseudo Wavelength')
plt.savefig(title + ".png")
# Reduce Example
#reduce(lambda a,b: a+b, [1,2,3,4,5], 0)
def stretch(*lists):
length = max([len(l) for l in lists])
return [[l[i * len(l) // length] for i in range(length)]
for l in lists]
# Inputs -> 0 thru 340
# Red -> 341, Green -> 342, Blue -> 343
df=pd.read_csv('real_data01.txt', sep=' ',header=None, index_col=False)
real_data = df.values
real_inputs = real_data[:,0:341] # Do not include index 301 (from zero)
real_answers_red = real_data[:,341]
real_answers_green = real_data[:,342]
real_answers_blue = real_data[:,343]
print(real_answers_red[0])
print(real_answers_green[0])
print(real_answers_blue[0])
708.0209192547569 696.6185377684507 289.46545254227465
real_inputs[0][340]
62111.611798252336
# Red solution for contrived data
df=pd.read_csv('solution.txt', sep=' ',header=None, index_col=False)
contrived_red_solution_alphas = df.values
# Now dot the real inputs with the solution alphas
print(contrived_red_solution_alphas.shape)
print(real_inputs.shape)
# The shapes are funny so we've got a little work to do ..
new_inputs_trimmed = real_inputs[:,0:301] # hack later trim front and back ? Interpolate?
#print (new_inputs.shape)
(301, 1) (285, 341)
# Let's actually do the 'dotting'
possible_ans = np.matmul(new_inputs_trimmed,contrived_red_solution_alphas)
# And plot
plt.plot(possible_ans,real_answers_red, 'o')
[<matplotlib.lines.Line2D at 0x7fbb0031dd90>]
# 20 off the front and 20 off the back
new_inputs_centered = real_inputs[:,20:321] # hack later trim front and back ? Interpolate?
# Let's actually do the 'dotting'
possible_ans = np.matmul(new_inputs_centered,contrived_red_solution_alphas)
# And plot
plt.plot(possible_ans,real_answers_red, 'o')
[<matplotlib.lines.Line2D at 0x7fbb0025fe20>]