markdown
stringlengths
0
37k
code
stringlengths
1
33.3k
path
stringlengths
8
215
repo_name
stringlengths
6
77
license
stringclasses
15 values
$$f:M_{mxn} \to M_{mxn}$$ $$ \forall \quad i, j: \quad i < m, j < n \qquad a_{ij} = \left{ \begin{array}{ll} \text{True} & \quad se \quad a_{ij} > 30 \ \text{False} & \quad \text{c.c} \end{array} \right. $$
print(A > 30)
disciplines/SME0819 - Matrices for Applied Statistics/0x00_Fundamentals/Matrices - Fundamentals.ipynb
jhonatancasale/graduation-pool
apache-2.0
Usando um vetor de Bools como Indexador
print(A[A > 30])
disciplines/SME0819 - Matrices for Applied Statistics/0x00_Fundamentals/Matrices - Fundamentals.ipynb
jhonatancasale/graduation-pool
apache-2.0
$$A_{mxn} * B_{mxn} \mapsto C_{mxn}$$ $$c_{ij} = a_{ij} * b_{ij}$$ $$\forall \quad i, j: \quad i < m, j < n$$
print("{} * {} -> {}".format(A, B, A * B))
disciplines/SME0819 - Matrices for Applied Statistics/0x00_Fundamentals/Matrices - Fundamentals.ipynb
jhonatancasale/graduation-pool
apache-2.0
print("{}.{} -> {}".format(A, B, A.dot(B))) print("{}.{} -> {}".format(A, B, np.dot(A, B))) print(np.ones(10) * 12) M = np.linspace(-1, 1, 16).reshape(4, 4) print(M) print("sum(A) -> {}".format(M.sum())) print("max(A) -> {} | min(A) -> {}" .format(M.max(), M.min())) N = np.arange(16).reshape(4, 4) print(N) print(N.sum(axis=0)) # sum by column print(N.sum(axis=1)) #sum by row print(N.min(axis=1)) print(N.cumsum(axis=0)) print(N) for column in range(N.shape[1]): print(N[:,column]) print(N.T) print(N) print(N.transpose()) print(N) I = np.eye(2) print(I) I2 = I * 2 I2_inv = np.linalg.inv(I2) print(I2_inv) print(np.dot(I2, I2_inv)) dir(np.linalg) print(np.trace(I2)) Prod = np.dot(I2, I2) print(Prod) print(np.linalg.eig(Prod))
disciplines/SME0819 - Matrices for Applied Statistics/0x00_Fundamentals/Matrices - Fundamentals.ipynb
jhonatancasale/graduation-pool
apache-2.0
$$Ax = y$$
A = np.linspace(1, 4, 4).reshape(2, 2) print(A) y = np.array([5., 7.]) x = np.linalg.solve(A, y) print(x) print(np.dot(A, x.T)) x = np.arange(0, 10, 2) y = np.arange(5) print(np.vstack([x, y])) print(np.hstack([x, y])) print(np.hsplit(x, [2])) print(np.hsplit(x, [2, 4])) print(np.vsplit(np.eye(3), range(1, 3)))
disciplines/SME0819 - Matrices for Applied Statistics/0x00_Fundamentals/Matrices - Fundamentals.ipynb
jhonatancasale/graduation-pool
apache-2.0
Now for the heavy lifting: We first have to come up with the weights, - calculate the month lengths for each monthly data record - calculate weights using groupby('time.season') Finally, we just need to multiply our weights by the Dataset and sum allong the time dimension.
# Make a DataArray with the number of days in each month, size = len(time) month_length = xray.DataArray(get_dpm(ds.time.to_index(), calendar='noleap'), coords=[ds.time], name='month_length') # Calculate the weights by grouping by 'time.season'. # Conversion to float type ('astype(float)') only necessary for Python 2.x weights = month_length.groupby('time.season') / month_length.astype(float).groupby('time.season').sum() # Test that the sum of the weights for each season is 1.0 np.testing.assert_allclose(weights.groupby('time.season').sum().values, np.ones(4)) # Calculate the weighted average ds_weighted = (ds * weights).groupby('time.season').sum(dim='time') print(ds_weighted) # only used for comparisons ds_unweighted = ds.groupby('time.season').mean('time') ds_diff = ds_weighted - ds_unweighted # Quick plot to show the results is_null = np.isnan(ds_unweighted['Tair'][0].values) fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(14,12)) for i, season in enumerate(('DJF', 'MAM', 'JJA', 'SON')): plt.sca(axes[i, 0]) plt.pcolormesh(np.ma.masked_where(is_null, ds_weighted['Tair'].sel(season=season).values), vmin=-30, vmax=30, cmap='Spectral_r') plt.colorbar(extend='both') plt.sca(axes[i, 1]) plt.pcolormesh(np.ma.masked_where(is_null, ds_unweighted['Tair'].sel(season=season).values), vmin=-30, vmax=30, cmap='Spectral_r') plt.colorbar(extend='both') plt.sca(axes[i, 2]) plt.pcolormesh(np.ma.masked_where(is_null, ds_diff['Tair'].sel(season=season).values), vmin=-0.1, vmax=.1, cmap='RdBu_r') plt.colorbar(extend='both') for j in range(3): axes[i, j].axes.get_xaxis().set_ticklabels([]) axes[i, j].axes.get_yaxis().set_ticklabels([]) axes[i, j].axes.axis('tight') axes[i, 0].set_ylabel(season) axes[0, 0].set_title('Weighted by DPM') axes[0, 1].set_title('Equal Weighting') axes[0, 2].set_title('Difference') plt.tight_layout() fig.suptitle('Seasonal Surface Air Temperature', fontsize=16, y=1.02) # Wrap it into a simple function def season_mean(ds, calendar='standard'): # Make a DataArray of season/year groups year_season = xray.DataArray(ds.time.to_index().to_period(freq='Q-NOV').to_timestamp(how='E'), coords=[ds.time], name='year_season') # Make a DataArray with the number of days in each month, size = len(time) month_length = xray.DataArray(get_dpm(ds.time.to_index(), calendar=calendar), coords=[ds.time], name='month_length') # Calculate the weights by grouping by 'time.season' weights = month_length.groupby('time.season') / month_length.groupby('time.season').sum() # Test that the sum of the weights for each season is 1.0 np.testing.assert_allclose(weights.groupby('time.season').sum().values, np.ones(4)) # Calculate the weighted average return (ds * weights).groupby('time.season').sum(dim='time')
examples/xray_seasonal_means.ipynb
NicWayand/xray
apache-2.0
https://cschoel.github.io/nolds/nolds.html#detrended-fluctuation-analysis
# -*- coding: UTF-8 -*- from __future__ import division import numpy as np import pandas as pd import sys import math from sklearn.preprocessing import LabelEncoder, OneHotEncoder import re import os import csv from helpers.outliers import MyOutliers from skroutz_mobile import SkroutzMobile from sklearn.ensemble import IsolationForest import matplotlib.pyplot as plt import seaborn as sns from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.tree import DecisionTreeClassifier, export_graphviz from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score, confusion_matrix, r2_score from skroutz_mobile import SkroutzMobile from sklearn.model_selection import StratifiedShuffleSplit from helpers.my_train_test_split import MySplitTrainTest from sklearn.preprocessing import StandardScaler from preprocess_price_history import PreprocessPriceHistory from price_history import PriceHistory from dfa import dfa import scipy.signal as ss import nolds %matplotlib inline random_state = np.random.RandomState(seed=16011984) csv_in = "../price_history_02_with_seq_start.csv" orig_df = pd.read_csv(csv_in, index_col=0, encoding='utf-8', quoting=csv.QUOTE_ALL) orig_df.shape df = orig_df.drop(labels=PriceHistory.SPECIAL_COLS, axis=1) df.shape CSV_FILEPATH = "../price_history_02_with_seq_start.csv" #xx = df.iloc[0, ] ph = PriceHistory(CSV_FILEPATH) tt = ph.extractSequenceByLocation(iloc=0) tt.shape tt[-1] alpha = nolds.dfa(tt) alpha seqs = [ph.extractSequenceByLocation(iloc=ii) for ii in xrange(len(ph.df))] len(seqs) len(seqs[0]) alphas = [] for seq in seqs: try: alpha = nolds.dfa(seq.values) if not np.isnan(alpha): alphas.append(alpha) except AssertionError, ee: pass #alphas = [seq for seq in seqs if len(seq) > 1 and not np.all(seq[0] == seq)] len(alphas) plt.figure(figsize=(17,8)) sns.distplot(alphas, rug=True, axlabel='Alpha of Detrended Flunctuation Analysis') plt.show()
02_preprocessing/exploration04-price_history_dfa.ipynb
pligor/predicting-future-product-prices
agpl-3.0
Conclusion the estimate alpha for the Hurst parameter (alpha < 1: stationary process similar to fractional Gaussian noise with H = alpha, alpha > 1: non-stationary process similar to fractional Brownian motion with H = alpha - 1) So most price histories are identified as we would expect, as non-stationary processes
# References
02_preprocessing/exploration04-price_history_dfa.ipynb
pligor/predicting-future-product-prices
agpl-3.0
https://cschoel.github.io/nolds/nolds.html#detrended-fluctuation-analysis https://scholar.google.co.uk/scholar?q=Detrended+fluctuation+analysis%3A+A+scale-free+view+on+neuronal+oscillations&btnG=&hl=en&as_sdt=0%2C5 MLA format: Hardstone, Richard, et al. "Detrended fluctuation analysis: a scale-free view on neuronal oscillations." Frontiers in physiology 3 (2012). Price histories Detrended
seq = seqs[0].values plt.plot(seq) detrendeds = [ss.detrend(seq) for seq in seqs] len(detrendeds) plt.plot(detrendeds[0]) detrendeds[0] alldetr = [] for detrended in detrendeds: alldetr += list(detrended) len(alldetr) fig = plt.figure( figsize=(14, 6) ) sns.distplot(alldetr, axlabel="Price Deviation from zero after detrend") plt.show() stdsca = StandardScaler(with_std=False) seqs_zero_mean = [stdsca.fit_transform(seq.values.reshape(1, -1).T) for seq in seqs] len(seqs_zero_mean), seqs_zero_mean[0].shape, seqs_zero_mean[3].shape allzeromean = np.empty(shape=(0, 1)) for seq in seqs_zero_mean: allzeromean = np.vstack( (allzeromean, seq) ) allzeromean.shape fig = plt.figure( figsize=(14, 6) ) sns.distplot(allzeromean.flatten(), axlabel="Price Deviation from zero before detrend") plt.show()
02_preprocessing/exploration04-price_history_dfa.ipynb
pligor/predicting-future-product-prices
agpl-3.0
Performing basic math functions
x = 2 print('2*x =',2*x) #multiplcation print('x^3 =',x**3) #exponents print('e^x =',np.exp(x)) #e^x print('e^x =',np.e**x) #e^x alternate form print('Pi =',np.pi) #Pi
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
Integration This will integrate a function that you provide. There a number of other methods for numerical integration that can be found online. For our examples we will use: $I = \int_{0}^{1} ax^2 + b dx$
from scipy.integrate import quad # First define a function that you want to integrate def integrand(x,a,b): return a*x**2 + b # Set your constants a = 2 b = 1 I = quad(integrand, 0, 1, args=(a,b)) print(I) # I has two values, the first value is the estimation of the integration, the second value is the upper bound on the error. # Notice that the upper bound on the error is extremely small, this is a good estimation.
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
Arrays
y = np.array([1,2,3,4,5]) #create an array of values print('y =\t',y) #'\t' creates an indent to nicely align answers print('y[0] =\t',y[0]) #Python starts counting at 0 print('y[2] =\t',y[2]) #y[2] gives the third element in y (I don't agree with this syntax but what can ya do?) print('y*x =\t',y*x) x = np.array([6,7,8,9]) z = np.concatenate((y,x),axis=None) #concatenate two arrays print('[y,x] =\t',z) print('sum of y elements =\t',np.sum(y)) #sum the elements of an array print('sum of z elements =\t',np.sum(z))
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
for loops
#This first loop iterates over the elements in an array array = np.array([0,1,2,3,4]) print('Frist Loop') for x in array: print(x*2) #This second loop iterates for x in the range of [0,4], again we have to say '5' because of the way Python counts print('Second Loop') for x in range(5): print(x*2)
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
Summation with for loops $\sum_{n=1}^{4} 2^{-n}$
answer = 0 #Each iteration will be added to this, so we start it at zero storage = [] #This will be used to store values after each iteration for n in range(1,5): storage.append(2**(-n)) #The append command adds elements to an array answer+=2**(-n) #+= is the same as saying answer = answer + ... print('answer =\t',answer) print('stored values=\t',storage)
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
while loops
#This while loop accomplishes the same thing as the two for loops above x=0 while x<5: print(x*2) x+=1
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
if statements
#Order of your if statements matters. array = np.array([2,4,6,7,11]) for x in array: if x<5: print('Not a winner') elif x<10: print(2*x) else: break
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
Linear Algebra
#Create a matrix a = np.array([[1,2,3],[4,5,6],[7,8,9]]) print('a =\n',a) #get eigenvalues and eigvenvectors of a w,v = linalg.eig(a) print('eigenvalues =\t',w) print('eigenvectors =\n',v) #Matrix multiplication b = np.array([1,0,0]) print('a*b =\t',[email protected]) #'@' does matrix multiplication, '.T' transposes a matrix or vector
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
Creating a function
#'def' starts the function. Variables inside the parentheses are inputs to your function. #Return is what your function will output. #In this example I have created a function that provides the first input raised to the power of the second input. def x2y(x,y): return x**y x2y(4,2)
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
Symbolic Math
#This lets us create functions with variables #First define a variable x = sy.Symbol('x') #Next create a function function = x**4 der = function.diff(x,1) print('first derivative =\t',der) der2 = function.diff(x,2) print('second derivative =\t',der2) #You can substitute back in for symbols now print('1st derivative at x=2 =\t',der.subs(x,2)) print('2nd derivative at x=2 =\t',der2.subs(x,2)) function2 = ln(x) lnder = function2.diff(x,1) print('derivative of ln(x) =\t',lnder)
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
Plotting
#Standard plot x = np.linspace(0,10) y = np.sin(x) z = np.cos(x) plt.plot(x,y,x,z) plt.xlabel('Radians'); plt.ylabel('Value'); plt.title('Standard Plot') plt.legend(['Sin','Cos']) plt.show() #Scatter Plot x = np.linspace(0,10,11) y = np.sin(x) z = np.cos(x) plt.scatter(x,y) plt.scatter(x,z) plt.xlabel('Radians'); plt.ylabel('Value'); plt.title('Scatter Plot') plt.legend(['Sin','Cos']) plt.show()
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
Math package has useful tools as well
print('5! =\t',math.factorial(5)) print('|-3| =\t',math.fabs(-3))
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
Something to help with your homework Assume you have a chemical reaction defined by: A + 2B -> C For every mole of A consumed, 2 moles of B are consumed, and 1 mole of C is produced. If we have the following molar flow rates: FA0 = 1.5 moles/s = Initial flow of A FB0 = = 2.5 moles/s = Initial flow of B FA = Flow rate of A as a function of reaction advancement FB = Flow rate of B as a function of reaction advancement FC = Flow rate of C as a function of reaction advancement Plot the molar flow rate of each species as a function of reaction advancement.
# Set up a vector to store values of advancement adv = np.arange(0,20,.01) # Inital Flow Rates fa0 = 1.5 #moles/s fb0 = 2.5 fc0 = 0 # Calculate flow rate as a function of advancement fa = fa0-1*adv fb = fb0-2*adv fc = fc0+adv # Find the maximum value of advancement, value at which one of the reactants hits 0 moles/s aind = np.where(fa<0)[0][0] bind = np.where(fb<0)[0][0] max_adv = min([aind,bind]) # Cut all of the vectors to the maximum value of advancement adv=adv[:max_adv] fa=fa[:max_adv] fb=fb[:max_adv] fc=fc[:max_adv] # Plot everything real nice plt.plot(adv,fa,label='A') plt.plot(adv,fb,label='B') plt.plot(adv,fc,label='C') plt.grid() plt.xlabel('Reaction Advancement',weight='bold') plt.ylabel('Molar Flow Rate',weight='bold') plt.legend() plt.show()
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
Putting it to use: Homework For Fun! Use 'for' loops to create Taylor expansions of $ln(1+x)$ centered at 0, with an order of 1, 2, 3, and 4. Plot these Taylor expansions along with the original equation on one plot. Label your plots. As a reminder the formula for a Taylor expansion is: $f(a) + \sum_{n=1}^{\infty}\frac{f^n(a)}{n!}(x-a)^n$ Since our expansion is centered at zero, $a=0$.
#Insert code here.
Resources/Python_Tutorial.ipynb
wmfschneider/CHE30324
gpl-3.0
However, this may not always be the case; if for statistical reasons it is important to average the same number of epochs from different conditions, you can use :meth:~mne.Epochs.equalize_event_counts prior to averaging. Another approach to pooling across conditions is to create separate :class:~mne.Evoked objects for each condition, and combine them afterward. This can be accomplished by the function :func:mne.combine_evoked, which computes a weighted sum of the :class:~mne.Evoked objects given to it. The weights can be manually specified as a list or array of float values, or can be specified using the keyword 'equal' (weight each :class:~mne.Evoked object by $\frac{1}{N}$, where $N$ is the number of :class:~mne.Evoked objects given) or the keyword 'nave' (weight each :class:~mne.Evoked object by the number of epochs that were averaged together to create it):
left_right_aud = mne.combine_evoked([left_aud, right_aud], weights='nave') assert left_right_aud.nave == left_aud.nave + right_aud.nave
0.20/_downloads/5514ea6c90dde531f8026904a417527e/plot_10_evoked_overview.ipynb
mne-tools/mne-tools.github.io
bsd-3-clause
Keeping track of nave is important for inverse imaging, because it is used to scale the noise covariance estimate (which in turn affects the magnitude of estimated source activity). See minimum_norm_estimates for more information (especially the whitening_and_scaling section). For this reason, combining :class:~mne.Evoked objects with either weights='equal' or by providing custom numeric weights should usually not be done if you intend to perform inverse imaging on the resulting :class:~mne.Evoked object. Other uses of Evoked objects Although the most common use of :class:~mne.Evoked objects is to store averages of epoched data, there are a couple other uses worth noting here. First, the method :meth:epochs.standard_error() &lt;mne.Epochs.standard_error&gt; will create an :class:~mne.Evoked object (just like :meth:epochs.average() &lt;mne.Epochs.average&gt; does), but the data in the :class:~mne.Evoked object will be the standard error across epochs instead of the average. To indicate this difference, :class:~mne.Evoked objects have a :attr:~mne.Evoked.kind attribute that takes values 'average' or 'standard error' as appropriate. Another use of :class:~mne.Evoked objects is to represent a single trial or epoch of data, usually when looping through epochs. This can be easily accomplished with the :meth:epochs.iter_evoked() &lt;mne.Epochs.iter_evoked&gt; method, and can be useful for applications where you want to do something that is only possible for :class:~mne.Evoked objects. For example, here we use the :meth:~mne.Evoked.get_peak method (which isn't available for :class:~mne.Epochs objects) to get the peak response in each trial:
for ix, trial in enumerate(epochs[:3].iter_evoked()): channel, latency, value = trial.get_peak(ch_type='eeg', return_amplitude=True) latency = int(round(latency * 1e3)) # convert to milliseconds value = int(round(value * 1e6)) # convert to µV print('Trial {}: peak of {} µV at {} ms in channel {}' .format(ix, value, latency, channel))
0.20/_downloads/5514ea6c90dde531f8026904a417527e/plot_10_evoked_overview.ipynb
mne-tools/mne-tools.github.io
bsd-3-clause
Q. What will the maximum (last element) of wave be? How to check?
print(waves.max()) waves # Now, convert to frequency # (note conversion from mm to m): freqs = c / (waves / 1e3) freqs # Make a table & print (zip pairs up wave and freq # into a list of tuples): table = [[wave, freq] for wave, freq in zip(waves, freqs)] for row in table: print(row) print(np.array(table)) # Just for review: print(list(zip(waves, freqs))) table = np.array([waves, freqs]) table
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Q. How could we regroup elements to match the previous incarnation? (row major)
table.transpose() # let's just work with the transpose table = table.T
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Q. What should this yield?
table.shape
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Q. What should this be?
table[20][0] table[20,0]
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Not possible for lists! :
l = list(table) print(l[20][0]) l[20,0] table.shape for index1 in range(table.shape[0]): # Q. What is table.shape[0]? for index2 in range(table.shape[1]): print('table[{}, {}] = {:g}'.format(index1, index2, table[index1, index2])) # Q. What will this loop print?
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
When you just loop over the elements of an array, you get rows:
table.shape[0] for row in table: # don't be fooled, it's not my naming of the looper that does that! print(row) for idontknowwhat in table: print(idontknowwhat)
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
This could also be done with one loop using numpy's ndenumerate. ndenumerate will enumerate the rows and columns of the array:
for index_tuple, value in np.ndenumerate(table): print('index {} has value {:.2e}'.format(index_tuple, value))
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Q. Reminder: what is the shape of table?
print(table.shape) print(type(table.shape))
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Q. So what is table.shape[0]?
table.shape[0]
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Q. And table.shape[1]?
table.shape[1]
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Arrays can be sliced analogously to lists. But we already saw, there's more indexing posssibilities on top with numpy.
table[0]
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Q: How to get the first column instead?
table[:, 0] # Note that this is different. # Q. What is this? table[:][0] # This will print the second column: table[:, 1] # To get the first five rows of the table: print(table[:5, :]) print() # Same as: print(table[:5])
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Numpy also has a multi-dimensional lazy indexing trick under its sleeve:
ndarray = np.zeros(2,3,4) # will fail. Why? Hint: Look at error message ndarray = np.zeros((2,3,4)) ndarray = np.arange(2*3*4).reshape((2,3,4)) # will fail. Why? ndarray ndarray[:, :, 0] ndarray[..., 0]
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Array Computing For an array $A$ of any rank, $f(A)$ means applying the function $f$ to each element of $A$. Matrix Objects
xArray1 = np.array([1, 2, 3], float) xArray1 xArray1.T xMatrix = np.matrix(xArray1) print(type(xMatrix)) xMatrix xMatrix.shape xMatrix2 = xMatrix.transpose() xMatrix2 # Or xMatrix.T
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Q. What is the identity matrix?
iMatrix = np.eye(3) # or np.identity iMatrix # And iMatrix2 = np.mat(iMatrix) # 'mat' short for 'matrix' iMatrix2 # Array multiplication. # Reminder of xMatrix? xMatrix # Multiplication of any matrix by the identity matrix # yields that matrix: xMatrix * iMatrix # Reminder of xMatrix2: xMatrix2 xMatrix2 = iMatrix * xMatrix2 xMatrix2 xMatrix * xMatrix2 np.dot(xMatrix, xMatrix2) xMatrix xMatrix2 xArray = np.array(xMatrix) xArray2 = np.array(xMatrix2) xArray * xArray2 xMatrix.shape, xMatrix2.shape xArray.shape np.array(xMatrix) * np.array(xMatrix2).T
lecture_14_ndarraysI.ipynb
CUBoulder-ASTR2600/lectures
isc
Plotting with parameters Write a plot_sin1(a, b) function that plots $sin(ax+b)$ over the interval $[0,4\pi]$. Customize your visualization to make it effective and beautiful. Customize the box, grid, spines and ticks to match the requirements of this data. Use enough points along the x-axis to get a smooth plot. For the x-axis tick locations use integer multiples of $\pi$. For the x-axis tick labels use multiples of pi using LaTeX: $3\pi$.
# YOUR CODE HERE def plot_sine1(a, b): t = np.linspace(0,4*np.pi,400) plt.plot(t,np.sin(a*t + b)) plt.xlim(0,4*np.pi) plt.ylim(-1.0,1.0) plt.xticks([0,np.pi,2*np.pi,3*np.pi,4*np.pi], ['0','π','2π','3π','4π']) plot_sine1(5, 3.4)
assignments/assignment05/InteractEx02.ipynb
joshnsolomon/phys202-2015-work
mit
In matplotlib, the line style and color can be set with a third argument to plot. Examples of this argument: dashed red: r-- blue circles: bo dotted black: k. Write a plot_sine2(a, b, style) function that has a third style argument that allows you to set the line style of the plot. The style should default to a blue line.
def plot_sine2(a,b,style): t = np.linspace(0,4*np.pi,400) plt.plot(t,np.sin(a*t + b),style) plt.xlim(0,4*np.pi) plt.ylim(-1.0,1.0) plt.xticks([0,np.pi,2*np.pi,3*np.pi,4*np.pi], ['0','π','2π','3π','4π']) plot_sine2(4.0, -1.0, 'r--')
assignments/assignment05/InteractEx02.ipynb
joshnsolomon/phys202-2015-work
mit
Use interact to create a UI for plot_sine2. Use a slider for a and b as above. Use a drop down menu for selecting the line style between a dotted blue line line, black circles and red triangles.
interact(plot_sine2, a=(0.0,5.0,.1), b=(-5.0,5.0,.1), style={'Dotted Blue': 'b:', 'Black Circles': 'ko', 'Red Triangles':'r^'}); assert True # leave this for grading the plot_sine2 exercise
assignments/assignment05/InteractEx02.ipynb
joshnsolomon/phys202-2015-work
mit
Here we will look at a molecular dynamics simulation of the barstar. As we will analyse Protein Block sequences, we first need to assign these sequences for each frame of the trajectory.
# Assign PB sequences for all frames of a trajectory trajectory = os.path.join(pbx.DEMO_DATA_PATH, 'barstar_md_traj.xtc') topology = os.path.join(pbx.DEMO_DATA_PATH, 'barstar_md_traj.gro') sequences = [] for chain_name, chain in pbx.chains_from_trajectory(trajectory, topology): dihedrals = chain.get_phi_psi_angles() pb_seq = pbx.assign(dihedrals) sequences.append(pb_seq)
doc/source/notebooks/Deformability.ipynb
jbarnoud/PBxplore
mit
Block occurences per position The basic information we need to analyse protein deformability is the count of occurences of each PB for each position throughout the trajectory. This occurence matrix can be calculated with the :func:pbxplore.analysis.count_matrix function.
count_matrix = pbx.analysis.count_matrix(sequences)
doc/source/notebooks/Deformability.ipynb
jbarnoud/PBxplore
mit
PBxplore provides the :func:pbxplore.analysis.plot_map function to ease the visualization of the occurence matrix.
pbx.analysis.plot_map('map.png', count_matrix) !rm map.png
doc/source/notebooks/Deformability.ipynb
jbarnoud/PBxplore
mit
The :func:pbxplore.analysis.plot_map helper has a residue_min and a residue_max optional arguments to display only part of the matrix. These two arguments can be pass to all PBxplore functions that produce a figure.
pbx.analysis.plot_map('map.png', count_matrix, residue_min=60, residue_max=70) !rm map.png
doc/source/notebooks/Deformability.ipynb
jbarnoud/PBxplore
mit
Note that matrix in the the figure produced by :func:pbxplore.analysis.plot_map is normalized so as the sum of each column is 1. The matrix can be normalized with the :func:pbxplore.analysis.compute_freq_matrix.
freq_matrix = pbx.analysis.compute_freq_matrix(count_matrix) im = plt.imshow(freq_matrix, interpolation='none', aspect='auto') plt.colorbar(im) plt.xlabel('Position') plt.ylabel('Block')
doc/source/notebooks/Deformability.ipynb
jbarnoud/PBxplore
mit
Protein Block entropy The $N_{eq}$ is a measure of variability based on the count matrix calculated above. It can be computed with the :func:pbxplore.analysis.compute_neq function.
neq_by_position = pbx.analysis.compute_neq(count_matrix)
doc/source/notebooks/Deformability.ipynb
jbarnoud/PBxplore
mit
neq_by_position is a 1D numpy array with the $N_{eq}$ for each residue.
plt.plot(neq_by_position) plt.xlabel('Position') plt.ylabel('$N_{eq}$')
doc/source/notebooks/Deformability.ipynb
jbarnoud/PBxplore
mit
The :func:pbxplore.analysis.plot_neq helper ease the plotting of the $N_{eq}$.
pbx.analysis.plot_neq('neq.png', neq_by_position) !rm neq.png
doc/source/notebooks/Deformability.ipynb
jbarnoud/PBxplore
mit
The residue_min and residue_max arguments are available.
pbx.analysis.plot_neq('neq.png', neq_by_position, residue_min=60, residue_max=70) !rm neq.png
doc/source/notebooks/Deformability.ipynb
jbarnoud/PBxplore
mit
Display PB variability as a logo
pbx.analysis.generate_weblogo('logo.png', count_matrix) display(Image('logo.png')) !rm logo.png pbx.analysis.generate_weblogo('logo.png', count_matrix, residue_min=60, residue_max=70) display(Image('logo.png')) !rm logo.png
doc/source/notebooks/Deformability.ipynb
jbarnoud/PBxplore
mit
默认图现在有三个节点, 两个 constant() op, 和一个matmul() op. 为了真正进行矩阵相乘运算, 并得到矩阵乘法的 结果, 你必须在会话里启动这个图. 在一个会话中启动图 构造阶段完成后, 才能启动图. 启动图的第一步是创建一个 Session 对象, 如果无任何创建参数, 会话构造器将启动默认图. 欲了解完整的会话 API, 请阅读Session 类.
# 启动默认图 sess = tf.Session() # 调用 sess 的 'run()' 方法来执行矩阵乘法 op, 传入 'product' 作为该方法的参数. # 上面提到, 'product' 代表了矩阵乘法 op 的输出, 传入它是向方法表明, 我们希望取回 # 矩阵乘法 op 的输出. # # 整个执行过程是自动化的, 会话负责传递 op 所需的全部输入. op 通常是并发执行的. # # 函数调用 'run(product)' 触发了图中 # 三个 op (两个常量 op 和一个矩阵乘法 op) 的执行. # # 返回值 'result' 是一个 numpy `ndarray` 对象. result = sess.run(product) print(result) # ==> [[ 12.]] # 任务完成, 关闭会话. sess.close()
dev/openlibs/tensorflow/basic_usage.ipynb
karst87/ml
mit
Session 对象在使用完后需要关闭以释放资源. 除了显式调用 close 外, 也可以使用 "with" 代码块 来自动完成关闭动作.
with tf.Session() as sess: result = sess.run(product) print(result)
dev/openlibs/tensorflow/basic_usage.ipynb
karst87/ml
mit
在实现上, TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU). 一般你不需要显式指定使用 CPU 还是 GPU, TensorFlow 能自动检测. 如果检测到 GPU, TensorFlow 会尽可能地利用找到的第一个 GPU 来执行操作. 如果机器上有超过一个可用的 GPU, 除第一个外的其它 GPU 默认是不参与计算的. 为了让 TensorFlow 使用这些 GPU, 你必须将 op 明确指派给它们执行. with...Device 语句用来指派特定的 CPU 或 GPU 执行操作:
with tf.Session() as sess: # with tf.device('/gpu:0'): with tf.device('/cpu:0'): matrix1 = tf.constant([[3, 3]]) matrix2 = tf.constant([[2], [2]]) product = tf.matmul(matrix1, matrix2) reuslt = sess.run(product) print(result)
dev/openlibs/tensorflow/basic_usage.ipynb
karst87/ml
mit
设备用字符串进行标识. 目前支持的设备包括: "/cpu:0": 机器的 CPU. "/gpu:0": 机器的第一个 GPU, 如果有的话. "/gpu:1": 机器的第二个 GPU, 以此类推. 阅读使用GPU章节, 了解 TensorFlow GPU 使用的更多信息. 交互式使用 文档中的 Python 示例使用一个会话 Session 来 启动图, 并调用 Session.run() 方法执行操作. 为了便于使用诸如 IPython 之类的 Python 交互环境, 可以使用 InteractiveSession 代替 Session 类, 使用 Tensor.eval() 和 Operation.run() 方法代替 Session.run(). 这样可以避免使用一个变量来持有会话.
# 进入一个交互式TensorFlow会话 import tensorflow as tf sess = tf.InteractiveSession() x = tf.Variable([1, 2]) a = tf.constant([3, 3]) # 使用初始化器 initializer op 的 run() 方法初始化 'x' x.initializer.run() # 增加一个减法 sub op, 从 'x' 减去 'a'. 运行减法 op, 输出结果 sub = tf.sub(x, a) print(sub.eval()) # ==> [-2. -1.]
dev/openlibs/tensorflow/basic_usage.ipynb
karst87/ml
mit
Loading pickle files The convert_data.py converts the ubyte format input files into numpy arrays. These arrays are then saved out as pickle files to be quickly loaded later on. The shape of the numpy arrays for images and labels are: Images: (N, rows, cols) Labels: (N, 1)
# Set up the file directory and names DIR = '../input/' X_TRAIN = DIR + 'train-images-idx3-ubyte.pkl' Y_TRAIN = DIR + 'train-labels-idx1-ubyte.pkl' X_TEST = DIR + 't10k-images-idx3-ubyte.pkl' Y_TEST = DIR + 't10k-labels-idx1-ubyte.pkl' print('Loading pickle files') X_train = pickle.load( open( X_TRAIN, "rb" ) ) y_train = pickle.load( open( Y_TRAIN, "rb" ) ) X_test = pickle.load( open( X_TEST, "rb" ) ) y_test = pickle.load( open( Y_TEST, "rb" ) ) n_train = X_train.shape[0] n_test = X_test.shape[0] print('Train images shape {}, labels shape {}'.format(X_train.shape, y_train.shape)) print('Test images shape {}, labels shape {}'.format(X_test.shape, y_test.shape))
notebooks/data_exploration.ipynb
timgasser/keras-mnist
mit
Sample training images with labels Let's show a few of the training images with the corresponding labels, so we can sanity check that the labels match the numbers, and the images themselves look like actual digits.
# Check a few training values at random as a sanity check def show_label_images(X, y): '''Shows random images in a grid''' num = 9 images = np.random.randint(0, X.shape[0], num) print('Showing training image indexes {}'.format(images)) fig, axes = plt.subplots(3,3, figsize=(6,6)) for idx, val in enumerate(images): r, c = divmod(idx, 3) axes[r][c].imshow(X[images[idx]]) axes[r][c].annotate('Label: {}'.format(y[val]), xy=(1, 1)) axes[r][c].xaxis.set_visible(False) axes[r][c].yaxis.set_visible(False) show_label_images(X_train, y_train)
notebooks/data_exploration.ipynb
timgasser/keras-mnist
mit
Sample test images with labels Now we can check the test images and labels by picking a few random ones, and making sure the images look reasonable and they match their labels.
# Now do the same for the training dataset show_label_images(X_test, y_test) # # Training label distribution y_train_df = pd.DataFrame(y_train, columns=['class']) y_train_df.plot.hist(legend=False) hist_df = pd.DataFrame(y_train_df['class'].value_counts(normalize=True)) hist_df.index.name = 'class' hist_df.columns = ['train']
notebooks/data_exploration.ipynb
timgasser/keras-mnist
mit
The class distribution is pretty evenly split between the classes. 1 is the most popular class with 11.24% of instances, and at the other end 5 is the least frequent class, with 9.04% of instances
# Test label distribution y_test_df = pd.DataFrame(y_test, columns=['class']) y_test_df.plot.hist(legend=False, bins=10) test_counts = y_test_df['class'].value_counts(normalize=True) hist_df['test'] = test_counts
notebooks/data_exploration.ipynb
timgasser/keras-mnist
mit
The distribution looks very similar between training and test datasets.
hist_df['diff'] = np.abs(hist_df['train'] - hist_df['test']) hist_df.sort_values('diff', ascending=False)['diff'].plot.bar()
notebooks/data_exploration.ipynb
timgasser/keras-mnist
mit
The largest difference is 0.0040% in the number 2 class.
# Final quick check of datatypes assert X_train.dtype == np.uint8 assert y_train.dtype == np.uint8 assert X_test.dtype == np.uint8 assert y_test.dtype == np.uint8
notebooks/data_exploration.ipynb
timgasser/keras-mnist
mit
Split a dataset into a tranining and a test folder In the code blocks below we load a real and synthetic dataset to highlight the HRT at the bottom of the script. Option 1: South African Heart Dataset
link_data = "https://web.stanford.edu/~hastie/ElemStatLearn/datasets/SAheart.data" dat_sah = pd.read_csv(link_data) # Extract the binary response and then drop y_sah = dat_sah['chd'] dat_sah.drop(columns=['row.names','chd'],inplace=True) # one-hot encode famhist dat_sah['famhist'] = pd.get_dummies(dat_sah['famhist'])['Present'] # Convert the X matrix to a numpy array X_sah = np.array(dat_sah)
_rmd/extra_hrt/hrt_python_copy.ipynb
erikdrysdale/erikdrysdale.github.io
mit
Note that the column types of each data need to be defined in the cn_type variable.
cn_type_sah = np.where(dat_sah.columns=='famhist','binomial','gaussian') # Do a train/test split np.random.seed(1234) idx = np.arange(len(y_sah)) np.random.shuffle(idx) idx_test = np.where((idx % 5) == 0)[0] idx_train = np.where((idx % 5) != 0)[0] X_train_sah = X_sah[idx_train] X_test_sah = X_sah[idx_test] y_train_sah = y_sah[idx_train] y_test_sah = y_sah[idx_test]
_rmd/extra_hrt/hrt_python_copy.ipynb
erikdrysdale/erikdrysdale.github.io
mit
Option 2: Non-linear decision boundary dataset
# ---- Random circle data ---- # np.random.seed(1234) n_circ = 1000 X_circ = np.random.randn(n_circ,5) X_circ = X_circ + np.random.randn(n_circ,1) y_circ = np.where(np.apply_along_axis(arr=X_circ[:,0:2],axis=1,func1d= lambda x: np.sqrt(np.sum(x**2)) ) > 1.2,1,0) cn_type_circ = np.repeat('gaussian',X_circ.shape[1]) idx = np.arange(n_circ) np.random.shuffle(idx) idx_test = np.where((idx % 5) == 0)[0] idx_train = np.where((idx % 5) != 0)[0] X_train_circ = X_circ[idx_train] X_test_circ = X_circ[idx_test] y_train_circ = y_circ[idx_train] y_test_circ = y_circ[idx_test] sns.scatterplot(x='var1',y='var2',hue='y', data=pd.DataFrame({'y':y_circ,'var1':X_circ[:,0],'var2':X_circ[:,1]}))
_rmd/extra_hrt/hrt_python_copy.ipynb
erikdrysdale/erikdrysdale.github.io
mit
Function support The code block below provides a wrapper to implement the HRT algorithm for a binary outcome using a single training and test split. See my previous post for generalizations of this method for cross-validation. The function also requires a cn_type argument to specify whether the column is continuous or Bernoulli. The glm_l2 function implements an L2-regularized generalized regression model for Gaussian and Binomial data using an iteratively re-weighted least squares method. This can generalized for elastic-net regularization as well as different generalized linear model classes. The dgp_fun function takes a model with with glm_l2 and will generate a new vector of the data conditional on the rest of the design matrix.
# ---- FUNCTION SUPPORT FOR SCRIPT ---- # def hrt_bin_fun(X_train,y_train,X_test,y_test,cn_type): # ---- INTERNAL FUNCTION SUPPORT ---- # # Sigmoid function def sigmoid(x): return( 1/(1+np.exp(-x)) ) # Sigmoid weightin def sigmoid_w(x): return( sigmoid(x)*(1-sigmoid(x)) ) def glm_l2(resp,x,standardize,family='binomial',lam=0,add_int=True,tol=1e-4,max_iter=100): y = np.array(resp.copy()) X = x.copy() n = X.shape[0] # Make sure all the response values are zeros or ones check1 = (~np.all(np.isin(np.array(resp),[0,1]))) & (family=='binomial') if check1: print('Error! Response variable is not all binary'); #return() # Make sure the family type is correct check2 = ~pd.Series(family).isin(['gaussian','binomial'])[0] if check2: print('Error! Family must be either gaussian or binoimal') # Normalize if requested if standardize: mu_X = X.mean(axis=0).reshape(1,X.shape[1]) std_X = X.std(axis=0).reshape(1,X.shape[1]) else: mu_X = np.repeat(0,p).reshape(1,X.shape[1]) std_X = np.repeat(1,p).reshape(1,X.shape[1]) X = (X - mu_X)/std_X # Add intercept if add_int: X = np.append(X,np.repeat(1,n).reshape(n,1),axis=1) # Calculate dimensions y = y.reshape(n,1) p = X.shape[1] # l2-regularization Lambda = n * np.diag(np.repeat(lam,p)) bhat = np.repeat(0,X.shape[1]) if family=='binomial': bb = np.log(np.mean(y)/(1-np.mean(y))) else: bb = np.mean(y) if add_int: bhat = np.append(bhat[1:p],bb).reshape(p,1) if family=='binomial': ii = 0 diff = 1 while( (ii < max_iter) & (diff > tol) ): ii += 1 # Predicted probabilities eta = X.dot(bhat) phat = sigmoid(eta) res = y - phat what = phat*(1-phat) # Adjusted response z = eta + res/what # Weighted-least squares bhat_new = np.dot( np.linalg.inv( np.dot((X * what).T,X) + Lambda), np.dot((X * what).T, z) ) diff = np.mean((bhat_new - bhat)**2) bhat = bhat_new.copy() sig2 = 0 else: bhat = np.dot( np.linalg.inv( np.dot(X.T,X) + Lambda ), np.dot(X.T, y) ) # Calculate the standard error of the residuals res = y - np.dot(X,bhat) sig2 = np.sum(res**2) / (n - (p - add_int)) # Separate the intercept if add_int: b0 = bhat[p-1][0] bhat2 = bhat[0:(p-1)].copy() / std_X.T # Extract intercept b0 = b0 - np.sum(bhat2 * mu_X.T) else: bhat2 = bhat.copy() / std_X.T b0 = 0 # Create a dictionary to store the results ret_dict = {'b0':b0, 'bvec':bhat2, 'family':family, 'sig2':sig2, 'n':n} return ret_dict # mdl=mdl_lst[4].copy(); x = tmp_X.copy() # Function to generate data from a fitted model def dgp_fun(mdl,x): tmp_n = mdl['n'] tmp_family = mdl['family'] tmp_sig2 = mdl['sig2'] tmp_b0 = mdl['b0'] tmp_bvec = mdl['bvec'] # Fitted value fitted = np.squeeze(np.dot(x, tmp_bvec) + tmp_b0) if tmp_family=='gaussian': # Generate some noise noise = np.random.randn(tmp_n)*np.sqrt(tmp_sig2) + tmp_b0 y_ret = fitted + noise else: y_ret = np.random.binomial(n=1,p=sigmoid(fitted),size=tmp_n) # Return return(y_ret) # Logistic loss function def loss_binomial(y,yhat): ll = -1*np.mean(y*np.log(yhat) + (1-y)*np.log(1-yhat)) return(ll) # Loop through and fit a model to each column mdl_lst = [] for cc in np.arange(len(cn_type)): tmp_y = X_test[:,cc] tmp_X = np.delete(X_test, cc, 1) tmp_family = cn_type[cc] mdl_lst.append(glm_l2(resp=tmp_y,x=tmp_X,family=tmp_family,lam=0,standardize=True)) # ---- FIT SOME MACHINE LEARNING MODEL HERE ---- # # Fit random forest clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0) clf.fit(X_train, y_train) # Baseline predicted probabilities and logistic loss phat_baseline = clf.predict_proba(X_test)[:,1] loss_baseline = loss_binomial(y=y_test,yhat=phat_baseline) # ---- CALCULATE P-VALUES FOR EACH MODEL ---- # pval_lst = [] nsim = 250 for cc in np.arange(len(cn_type)): print('Variable %i of %i' % (cc+1, len(cn_type))) mdl_cc = mdl_lst[cc] X_test_not_cc = np.delete(X_test, cc, 1) X_test_cc = X_test.copy() loss_lst = [] for ii in range(nsim): np.random.seed(ii) xx_draw_test = dgp_fun(mdl=mdl_cc,x=X_test_not_cc) X_test_cc[:,cc] = xx_draw_test phat_ii = clf.predict_proba(X_test_cc)[:,1] loss_ii = loss_binomial(y=y_test,yhat=phat_ii) loss_lst.append(loss_ii) pval_cc = np.mean(np.array(loss_lst) <= loss_baseline) pval_lst.append(pval_cc) # Return p-values return(pval_lst)
_rmd/extra_hrt/hrt_python_copy.ipynb
erikdrysdale/erikdrysdale.github.io
mit
Get the p-values for the different datasets Now that the hrt_bin_fun has been defined, we can perform inference on the columns of the two datasets created above.
pval_circ = hrt_bin_fun(X_train=X_train_circ,y_train=y_train_circ,X_test=X_test_circ,y_test=y_test_circ,cn_type=cn_type_circ) pval_sah = hrt_bin_fun(X_train=X_train_sah,y_train=y_train_sah,X_test=X_test_sah,y_test=y_test_sah,cn_type=cn_type_sah)
_rmd/extra_hrt/hrt_python_copy.ipynb
erikdrysdale/erikdrysdale.github.io
mit
The results below show that the sbp, tobacco, ldl, adiposity, and age are statistically significant features for the South African Heart Dataset. As expected, the first two variables, var1, and var2 from the non-linear decision boundary dataset are important as these are the two variables which define the decision boundary with the rest of the variables being noise variables.
pd.concat([pd.DataFrame({'vars':dat_sah.columns, 'pval':pval_sah, 'dataset':'SAH'}), pd.DataFrame({'vars':['var'+str(x) for x in np.arange(5)+1],'pval':pval_circ,'dataset':'NLP'})])
_rmd/extra_hrt/hrt_python_copy.ipynb
erikdrysdale/erikdrysdale.github.io
mit
Make sure that your environment path is set to match the correct version of pandeia
print(os.environ['pandeia_refdata'] ) import pandeia.engine print(pandeia.engine.__version__)
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
Load blank exo dictionary To start, load in a blank exoplanet dictionary with empty keys. You will fill these out for yourself in the next step.
exo_dict = jdi.load_exo_dict() print(exo_dict.keys()) #print(exo_dict['star']['w_unit'])
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
Edit exoplanet observation inputs Editting each keys are annoying. But, do this carefully or it could result in nonsense runs
exo_dict['observation']['sat_level'] = 80 #saturation level in percent of full well exo_dict['observation']['sat_unit'] = '%' exo_dict['observation']['noccultations'] = 1 #number of transits exo_dict['observation']['R'] = None #fixed binning. I usually suggest ZERO binning.. you can always bin later #without having to redo the calcualtion exo_dict['observation']['baseline_unit'] = 'total' #Defines how you specify out of transit observing time #'frac' : fraction of time in transit versus out = in/out #'total' : total observing time (seconds) exo_dict['observation']['baseline'] = 4.0*60.0*60.0 #in accordance with what was specified above (total observing time) exo_dict['observation']['noise_floor'] = 0 #this can be a fixed level or it can be a filepath #to a wavelength dependent noise floor solution (units are ppm)
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
Edit exoplanet host star inputs Note... If you select phoenix you do not have to provide a starpath, w_unit or f_unit, but you do have to provide a temp, metal and logg. If you select user you do not need to provide a temp, metal and logg, but you do need to provide units and starpath. Option 1) Grab stellar model from database
#OPTION 1 get start from database exo_dict['star']['type'] = 'phoenix' #phoenix or user (if you have your own) exo_dict['star']['mag'] = 8.0 #magnitude of the system exo_dict['star']['ref_wave'] = 1.25 #For J mag = 1.25, H = 1.6, K =2.22.. etc (all in micron) exo_dict['star']['temp'] = 5500 #in K exo_dict['star']['metal'] = 0.0 # as log Fe/H exo_dict['star']['logg'] = 4.0 #log surface gravity cgs
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
Option 1) Input as dictionary or filename
#Let's create a little fake stellar input import scipy.constants as sc wl = np.linspace(0.8, 5, 3000) nu = sc.c/(wl*1e-6) # frequency in sec^-1 teff = 5500.0 planck_5500K = nu**3 / (np.exp(sc.h*nu/sc.k/teff) - 1) #can either be dictionary input starflux = {'f':planck_5500K, 'w':wl} #or can be as a stellar file #starflux = 'planck_5500K.dat' #with open(starflux, 'w') as sf: # for w,f in zip(wl, planck_5500K): # sf.write(f'{w:.15f} {f:.15e}\n') exo_dict['star']['type'] = 'user' exo_dict['star']['mag'] = 8.0 #magnitude of the system exo_dict['star']['ref_wave'] = 1.25 exo_dict['star']['starpath'] = starflux exo_dict['star']['w_unit'] = 'um' exo_dict['star']['f_unit'] = 'erg/cm2/s/Hz'
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
Edit exoplanet inputs using one of three options 1) user specified 2) constant value 3) select from grid 1) Edit exoplanet planet inputs if using your own model
exo_dict['planet']['type'] ='user' #tells pandexo you are uploading your own spectrum exo_dict['planet']['exopath'] = 'wasp12b.txt' #or as a dictionary #exo_dict['planet']['exopath'] = {'f':spectrum, 'w':wavelength} exo_dict['planet']['w_unit'] = 'cm' #other options include "um","nm" ,"Angs", "sec" (for phase curves) exo_dict['planet']['f_unit'] = 'rp^2/r*^2' #other options are 'fp/f*' exo_dict['planet']['transit_duration'] = 2.0*60.0*60.0 #transit duration exo_dict['planet']['td_unit'] = 's' #Any unit of time in accordance with astropy.units can be added
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
2) Users can also add in a constant temperature or a constant transit depth
exo_dict['planet']['type'] = 'constant' #tells pandexo you want a fixed transit depth exo_dict['planet']['transit_duration'] = 2.0*60.0*60.0 #transit duration exo_dict['planet']['td_unit'] = 's' exo_dict['planet']['radius'] = 1 exo_dict['planet']['r_unit'] = 'R_jup' #Any unit of distance in accordance with astropy.units can be added here exo_dict['star']['radius'] = 1 exo_dict['star']['r_unit'] = 'R_sun' #Same deal with astropy.units here exo_dict['planet']['f_unit'] = 'rp^2/r*^2' #this is what you would do for primary transit #ORRRRR.... #if you wanted to instead to secondary transit at constant temperature #exo_dict['planet']['f_unit'] = 'fp/f*' #exo_dict['planet']['temp'] = 1000
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
3) Select from grid NOTE: Currently only the fortney grid for hot Jupiters from Fortney+2010 is supported. Holler though, if you want another grid supported
exo_dict['planet']['type'] = 'grid' #tells pandexo you want to pull from the grid exo_dict['planet']['temp'] = 1000 #grid: 500, 750, 1000, 1250, 1500, 1750, 2000, 2250, 2500 exo_dict['planet']['chem'] = 'noTiO' #options: 'noTiO' and 'eqchem', noTiO is chemical eq. without TiO exo_dict['planet']['cloud'] = 'ray10' #options: nothing: '0', # Weak, medium, strong scattering: ray10,ray100, ray1000 # Weak, medium, strong cloud: flat1,flat10, flat100 exo_dict['planet']['mass'] = 1 exo_dict['planet']['m_unit'] = 'M_jup' #Any unit of mass in accordance with astropy.units can be added here exo_dict['planet']['radius'] = 1 exo_dict['planet']['r_unit'] = 'R_jup' #Any unit of distance in accordance with astropy.units can be added here exo_dict['star']['radius'] = 1 exo_dict['star']['r_unit'] = 'R_sun' #Same deal with astropy.units here
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
Load in instrument dictionary (OPTIONAL) Step 2 is optional because PandExo has the functionality to automatically load in instrument dictionaries. Skip this if you plan on observing with one of the following and want to use the subarray with the smallest frame time and the readout mode with 1 frame/1 group (standard): - NIRCam F444W - NIRSpec Prism - NIRSpec G395M - NIRSpec G395H - NIRSpec G235H - NIRSpec G235M - NIRCam F322W - NIRSpec G140M - NIRSpec G140H - MIRI LRS - NIRISS SOSS
jdi.print_instruments() inst_dict = jdi.load_mode_dict('NIRSpec G140H') #loading in instrument dictionaries allow you to personalize some of #the fields that are predefined in the templates. The templates have #the subbarays with the lowest frame times and the readmodes with 1 frame per group. #if that is not what you want. change these fields #Try printing this out to get a feel for how it is structured: print(inst_dict['configuration']) #Another way to display this is to print out the keys inst_dict.keys()
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
Running PandExo You have four options for running PandExo. All of them are accessed through attribute jdi.run_pandexo. See examples below. jdi.run_pandexo(exo, inst, param_space = 0, param_range = 0,save_file = True, output_path=os.getcwd(), output_file = '', verbose=True) Option 1- Run single instrument mode, single planet If you forget which instruments are available run jdi.print_isntruments() and pick one
jdi.print_instruments() result = jdi.run_pandexo(exo_dict,['NIRCam F322W2'], verbose=True)
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
Note, you can turn off print statements with verbose=False Option 2- Run single instrument mode (with user dict), single planet This is the same thing as option 1 but instead of feeding it a list of keys, you can feed it a instrument dictionary (this is for users who wanted to simulate something NOT pre defined within pandexo)
inst_dict = jdi.load_mode_dict('NIRSpec G140H') #personalize subarray inst_dict["configuration"]["detector"]["subarray"] = 'sub2048' result = jdi.run_pandexo(exo_dict, inst_dict)
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
Option 3- Run several modes, single planet Use several modes from print_isntruments() options.
#choose select result = jdi.run_pandexo(exo_dict,['NIRSpec G140M','NIRSpec G235M','NIRSpec G395M'], output_file='three_nirspec_modes.p',verbose=True) #run all #result = jdi.run_pandexo(exo_dict, ['RUN ALL'], save_file = False)
notebooks/JWST_Running_Pandexo.ipynb
natashabatalha/PandExo
gpl-3.0
Autoencoders An autoencoder is a type of neural network used to learn an efficient representation, or encoding, for a set of data. The advantages of using these learned encodings are similar to those of word embeddings; they reduce the dimension of the feature space and can capture similarities between different inputs. Autoencoders are a useful unsupervised learning method, as they do not require any ground truth labels to train. This notebook is based on this tutorial and this keras example. Data We will use the MNIST dataset, which contains images of handwritten digits (0, 1, 2, etc.). This dataset has 60,000 training examples and 10,000 testing examples.
# Set random seeds for reproducible results. import numpy as np import tensorflow as tf np.random.seed(42) tf.random.set_seed(42) # Load dataset using keras data loader. (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Each image in the dataset is 28 x 28 pixels. Let's flatten each to a 1-dimensional vector of length 784.
image_size = x_train.shape[1] original_dim = image_size * image_size # Flatten each image into a 1-d vector. x_train = np.reshape(x_train, [-1, original_dim]) x_test = np.reshape(x_test, [-1, original_dim]) # Rescale pixel values to a 0-1 range. x_train = x_train.astype('float32') / 255 x_test = x_test.astype('float32') / 255 print('x_train:', x_train.shape) print('x_test:', x_test.shape)
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Autoencoder Structure <a title="Chervinskii [CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0)], via Wikimedia Commons" href="https://commons.wikimedia.org/wiki/File:Autoencoder_structure.png"><img width="512" alt="Autoencoder structure" src="https://upload.wikimedia.org/wikipedia/commons/2/28/Autoencoder_structure.png"></a> Source: Wikipedia An autoencoder works by learning to output a copy of its input, after passing the input through one or more smaller hidden layer(s). This hidden layer describes an encoding or "code" used to represent the input (x in the above graph). An autoencoder has two main parts: an encoder that maps the input into the code, and a decoder that maps the code back to a reconstruction of the original input (x' in the above graph). This structure forces the hidden layer to learn a more efficient, useful representation of the input data (z in the above graph, also called a "latent representation"). Basic Model Below is an example of a simple autoencoder that maps the 784-dimensional input image to a 36-dimensional latent representation, then attempts to reconstruct the 784-dimensional input image from that encoded representation. Instead of keras.models.Sequential, we'll use keras.models.Model to more clearly show the encoder and decoder parts of the autoencoder as individual models. This will also make it easier to extract the latent representations from the encoder. The Sequential API is usually easier to use while the Model API is more flexible. You can read more about their differences here.
from tensorflow.keras import Input from tensorflow.keras.layers import Dense from tensorflow.keras.models import Model
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Encoder
latent_dim = 36 # input layer (needed for the Model API). input_layer = Input(shape=(original_dim,), name='encoder_input') # Notice that with all layers except for the first, # we need to specify which layer is used as input. latent_layer = Dense(latent_dim, activation='relu', name='latent_layer')(input_layer) encoder = Model(input_layer, latent_layer, name='encoder') encoder.summary()
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Decoder
latent_inputs = Input(shape=(latent_dim,), name='decoder_input') output_layer = Dense(original_dim, name='decoder_output')(latent_inputs) decoder = Model(latent_inputs, output_layer, name='decoder') decoder.summary()
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Training The full autoencoder passes the inputs to the encoder, then the latent representations from the encoder to the decoder. We'll use the Adam optimizer and Mean Squared Error loss.
autoencoder = Model( input_layer, decoder(encoder(input_layer)), name="autoencoder" ) autoencoder.compile(optimizer='adam', loss='mse') autoencoder.summary()
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
We will train for 50 epochs, using EarlyStopping to stop training early if validation loss improves by less than 0.0001 for 10 consecutive epochs. Using a batch size of 2048, this should take 1-2 minutes to train.
early_stopping = tf.keras.callbacks.EarlyStopping( monitor='val_loss', # minimum change in loss that qualifies as "improvement" # higher values of min_delta lead to earlier stopping min_delta=0.0001, # threshold for number of epochs with no improvement patience=10, verbose=1 ) autoencoder.fit( # input x_train, # output x_train, epochs=50, batch_size=2048, validation_data=(x_test, x_test), callbacks=[early_stopping] )
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Visualize Predictions
decoded_imgs = autoencoder.predict(x_test) import matplotlib.pyplot as plt def visualize_imgs(nrows, axis_names, images, sizes, n=10): ''' Plots images in a grid layout. nrows: number of rows of images to display axis_names: list of names for each row images: list of arrays of images sizes: list of image size to display for each row n: number of images to display per row (default 10) nrows = len(axis_names) = len(images) ''' fig, axes = plt.subplots(figsize=(20,4), nrows=nrows, ncols=1, sharey=False) for i in range(nrows): axes[i].set_title(axis_names[i], fontsize=16) axes[i].axis('off') for col in range(n): for i in range(nrows): ax = fig.add_subplot(nrows, n, col + 1 + i * n) plt.imshow(images[i][col].reshape(sizes[i], sizes[i])) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) fig.tight_layout() plt.show() visualize_imgs( 2, ['Original Images', 'Reconstructions'], [x_test, decoded_imgs], [image_size, image_size] )
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
This shows 10 original images with their corresponding reconstructed images directly below. Clearly, our autoencoder captured the basic digit structure of each image, though the reconstructed images are less sharp. Application: Image Compression Autoencoders have been used extensively in image compression and processing. An autoencoder can create higher resolution images from low-resolution images, and even colorize black and white images. To see how autoencoders can be used to compress images, we can use our already trained encoder as an image compressor. You can think of the decoder as a decompressor, reconstructing the original image from the compressed one.
# Compress original images. encoded_imgs = encoder.predict(x_test) # Reconstruct original images. decoded_imgs = decoder.predict(encoded_imgs) visualize_imgs( 3, ['Original Images', '36-dimensional Latent Representation', 'Reconstructions'], [x_test, encoded_imgs, decoded_imgs], [image_size, 6, image_size] )
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Now we can visualize the latent representation of each image that the autoencoder learned. Since this reduces the 784-dimensional original image to a 36-dimensional image, it essentially performs an image compression. Application: Image Denoising Autoencoders can also "denoise" images, such as poorly scanned pictures, and even partially damaged and destroyed paper documents (Kaggle dataset). To train a denoising autoencoder, we must first add noise to the images. Note: "Noise" refers to something that interferes with the quality of original input, such as static in an image or a partially jumbled message. Add Noise imgaug is a useful package to perform various image augmentations. Many of the arithmetic functions in the package simulate adding noise to an image. We'll use the SaltAndPepper technique. Note: This will take slightly under a minute to run on the full training and testing sets.
from imgaug import augmenters # Reshape images to 3-dimensional for augmenter. Since the images were # originally 2-dimensional, the third dimension is just 1. x_train = x_train.reshape(-1, image_size, image_size, 1) x_test = x_test.reshape(-1, image_size, image_size, 1) # p is the probability of changing a pixel to noise. # higher values of p mean noisier images. noise = augmenters.SaltAndPepper(p=0.6) # We could chain multiple augmenters using Sequential. seq = augmenters.Sequential([noise]) # Rescale pixel values to 0-255 (instead of 0-1) for augmenter, # add noise to images, then rescale pixel values back to 0-1. x_train_noise = seq.augment_images(x_train * 255) / 255 x_test_noise = seq.augment_images(x_test * 255) / 255
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
For comparison, here are what 5 images look like before we add noise:
f, ax = plt.subplots(figsize=(20,2), nrows=1, ncols=5) for i in range(5, 10): ax[i-5].imshow(x_train[i].reshape(image_size, image_size)) plt.show()
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
After we add noise, the images look like this:
f, ax = plt.subplots(figsize=(20,2), nrows=1, ncols=5) for i in range(5, 10): ax[i-5].imshow(x_train_noise[i].reshape(image_size, image_size)) plt.show()
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
As you can see, the images are quite noisy and difficult to denoise even with the human eye. Luckily, autoencoders are much better at this task. We'll follow a similar architecture as before, but this time we'll train the model using the noisy images as input and the original, un-noisy images as output. Encoder We will need a more sophisticated encoder / decoder architecture to handle the more complex problem. The encoder will use 3 Conv2D layers, with decreasing output filter sizes and a MaxPool layer after each. This will perform the desired effect of compressing, or "downsampling", the image. Since we are using convolutional layers, we can work directly with the 3-dimensional images.
from tensorflow.keras.layers import Conv2D, MaxPool2D, UpSampling2D filter_1 = 64 filter_2 = 32 filter_3 = 16 kernel_size = (3, 3) pool_size = (2, 2) latent_dim = 4 input_layer = Input(shape=(image_size, image_size, 1)) # First convolutional layer encoder_conv1 = Conv2D(filter_1, kernel_size, activation='relu', padding='same')(input_layer) encoder_pool1 = MaxPool2D(pool_size, padding='same')(encoder_conv1) # Second convolutional layer encoder_conv2 = Conv2D(filter_2, kernel_size, activation='relu', padding='same')(encoder_pool1) encoder_pool2 = MaxPool2D(pool_size, padding='same')(encoder_conv2) # Third convolutional layer encoder_conv3 = Conv2D(filter_3, kernel_size, activation='relu', padding='same')(encoder_pool2) latent_layer = MaxPool2D(pool_size, padding='same')(encoder_conv3) encoder_denoise = Model(input_layer, latent_layer, name='encoder') encoder_denoise.summary()
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Decoder The decoder will work in reverse, using 3 Conv2D layers, with increasing output filter sizes and an UpSampling2D layer after each. This will perform the desired effect of reconstructing or denoising the image.
latent_inputs = Input(shape=(latent_dim, latent_dim, filter_3)) # First convolutional layer decoder_conv1 = Conv2D(filter_3, kernel_size, activation='relu', padding='same')(latent_inputs) decoder_up1 = UpSampling2D(pool_size)(decoder_conv1) # Second convolutional layer decoder_conv2 = Conv2D(filter_2, kernel_size, activation='relu', padding='same')(decoder_up1) decoder_up2 = UpSampling2D(pool_size)(decoder_conv2) # Third convolutional layer decoder_conv3 = Conv2D(filter_1, kernel_size, activation='relu')(decoder_up2) decoder_up3 = UpSampling2D(pool_size)(decoder_conv3) # Output layer, which outputs images of size (28 x 28 x 1) output_layer = Conv2D(1, kernel_size, padding='same')(decoder_up3) decoder_denoise = Model(latent_inputs, output_layer, name='decoder') decoder_denoise.summary()
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Training We will again use early stopping and the same model parameters.
denoise_autoencoder = Model( input_layer, decoder_denoise(encoder_denoise(input_layer)) ) denoise_autoencoder.compile(optimizer='adam', loss='mse') denoise_autoencoder.summary()
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
We will only train for 10 epochs this time since the model is more complex and takes longer to train. This should take around a minute.
denoise_autoencoder.fit( # Input x_train_noise, # Output x_train, epochs=10, batch_size=2048, validation_data=(x_test_noise, x_test), callbacks=[early_stopping] )
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Visualize Denoised Images Let's visualize the first 10 denoised images.
denoised_imgs = denoise_autoencoder.predict(x_test_noise[:10]) visualize_imgs( 3, ['Noisy Images', 'Denoised Images', 'Original Images'], [x_test_noise, denoised_imgs, x_test], [image_size, image_size, image_size] )
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
As we can see, the autoencoder is mostly successful in recovering the original image, though a few denoised images are still blurry or unclear. More training or a different model architecture may help. Resources Introduction to Autoencoders Building Autoencoders in Keras PCA vs. Autoencoders Variational Autoencoders Auto-Encoding Variational Bayes paper Generating Images with VAEs Credit Card Fraud Detection using Autoencoders Autoencoder Explained Video Exercises Exercise 1: Watermarks In this exercise we'll perform a task similar to the denoising in the example above. The Mighty Mouse: Wolf! Wolf! dataset contains a Mighty Mouse video that has been watermarked. In this exercise you'll create an autoencoder to remove the watermark. First download and unzip the dataset. Student Solution
! chmod 600 kaggle.json && (ls ~/.kaggle 2>/dev/null || mkdir ~/.kaggle) && cp kaggle.json ~/.kaggle/ && echo 'Done' ! kaggle datasets download joshmcadams/mighty-mouse-wolf-wolf ! unzip mighty-mouse-wolf-wolf.zip ! ls
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
We'll use the smaller videos (80x60) in this exercise in order to fit within Colab's memory limits and in order to get our model to run faster. mighty_mouse_80x60_watermarked.mp4 contains the feature data. This is the watermarked video file. mighty_mouse_80x60.mp4 contains the target data. This is the video file before watermarking. Your task is to build an autoencoder that can be used to restore the watermarked file back to a non-watermarked state. Use as many code and text cells as you need to. Explain your reasoning and work.
# Your answer goes here
content/05_deep_learning/03_autoencoders/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0