markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
An alternative approach
Suppose that we wanted to simulate the Solow model with different parameter values so that we could compare the simulations. Since we'd be doing the same basic steps multiple times using different numbers, it would make sense to define a function so that we could avoid repetition.
The code below... | def solow_example(A,alpha,delta,s,n,K0,L0,T):
'''Returns DataFrame with simulated values for a Solow model with labor growth and constant TFP'''
# Initialize a variable called capital as a (T+1)x1 array of zeros and set first value to k0
capital = np.zeros(T+1)
capital[0] = K0
# Initialize... | winter2017/econ129/python/Econ129_Class_09.ipynb | letsgoexploring/teaching | mit |
With solow_example() defined, we can redo the previous exercise quickly: | # Create the DataFrame with simulated values
df = solow_example(A=10,alpha=0.35,delta=0.1,s=0.15,n=0.01,K0=20,L0=1,T=100)
# Create a 2x2 grid of plots of the capital per worker, outputper worker, consumption per worker, and investment per worker
fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(2,2,1)
ax.plot(df['... | winter2017/econ129/python/Econ129_Class_09.ipynb | letsgoexploring/teaching | mit |
solow_example() can be used to perform multiple simulations. For example, suppose we want to see the effect of having two different initial values of capital: $k_0 = 20$ and $k_0'=10$. | df1 = solow_example(A=10,alpha=0.35,delta=0.1,s=0.15,n=0.01,K0=20,L0=1,T=100)
df2 = solow_example(A=10,alpha=0.35,delta=0.1,s=0.15,n=0.01,K0=10,L0=1,T=100)
# Create a 2x2 grid of plots of the capital per worker, outputper worker, consumption per worker, and investment per worker
fig = plt.figure(figsize=(12,8))
ax = f... | winter2017/econ129/python/Econ129_Class_09.ipynb | letsgoexploring/teaching | mit |
This dataset is borrowed from the PyCon tutorial of Brandon Rhodes (so all credit to him!). You can download these data from here: titles.csv and cast.csv and put them in the /data folder. | cast = pd.read_csv('data/cast.csv')
cast.head()
titles = pd.read_csv('data/titles.csv')
titles.head() | solved - 03b - Some more advanced indexing.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Setting columns as the index
Why is it useful to have an index?
Giving meaningful labels to your data -> easier to remember which data are where
Unleash some powerful methods, eg with a DatetimeIndex for time series
Easier and faster selection of data
It is this last one we are going to explore here!
Setting the titl... | c = cast.set_index('title')
c.head() | solved - 03b - Some more advanced indexing.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Instead of doing: | %%time
cast[cast['title'] == 'Hamlet'] | solved - 03b - Some more advanced indexing.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
we can now do: | %%time
c.loc['Hamlet'] | solved - 03b - Some more advanced indexing.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
But you can also have multiple columns as the index, leading to a multi-index or hierarchical index: | c = cast.set_index(['title', 'year'])
c.head()
%%time
c.loc[('Hamlet', 2000),:]
c2 = c.sort_index()
%%time
c2.loc[('Hamlet', 2000),:] | solved - 03b - Some more advanced indexing.ipynb | jorisvandenbossche/2015-EuroScipy-pandas-tutorial | bsd-2-clause |
Target information | names, p, v, w = load_clubs('clubs.csv')
cpi = 40e-3
T = 12
t_sim = np.arange(0, T, cpi)
t1, p1, v1 = calc_traj(p[0, :], v[0, :], w[0, :], t_sim)
t2, p2, v2 = calc_traj(p[-1, :], v[-1, :], w[-1, :], t_sim)
sensor_locations = np.array([[-10, 28.5, 1], [-15, 30.3, 3],
[200, 30, 1.5], [220,... | tracking/tracking.ipynb | scjrobertson/xRange | gpl-3.0 |
The Kalman Filter Model | N = 6
if pm_1.shape < pm_2.shape:
M, _ = pm_1.shape
pm_2 = pm_2[:M]
vm_2 = pm_2[:M]
else:
M, _ = pm_2.shape
pm_1 = pm_1[:M]
vm_1 = vm_2[:M]
print(M)
dt = cpi
g = 9.81
sigma_r = 2.5
sigma_q = 0.5
prior_var = 1 | tracking/tracking.ipynb | scjrobertson/xRange | gpl-3.0 |
Motion and measurement models | A = np.identity(N)
A[0, 3] = A[1, 4] = A[2, 5] = dt
B = np.zeros((N, N))
B[2, 2] = B[5, 5] = 1
R = np.identity(N)*sigma_r
C = np.identity(N)
Q = np.identity(N)*sigma_q
u = np.zeros((6, 1))
u[2] = -0.5*g*(dt**2)
u[5] = -g*dt | tracking/tracking.ipynb | scjrobertson/xRange | gpl-3.0 |
Priors | #Object 1
mu0_1 = np.zeros((N, 1))
mu0_1[:3, :] = p1[0, :].reshape(3, 1)
mu0_1[3:, :] = v[0, :].reshape(3, 1)
prec0_1 = np.linalg.inv(prior_var*np.identity(N))
h0_1 = (prec0_1)@(mu0_1)
g0_1 = -0.5*(mu0_1.T)@(prec0_1)@(mu0_1) -3*np.log(2*np.pi)
#Object 2
mu0_2 = np.zeros((N, 1))
mu0_2[:3, :] = p2[0, :].reshape(3, 1)
m... | tracking/tracking.ipynb | scjrobertson/xRange | gpl-3.0 |
Linear Kalman Filtering
Creating the model | z_t = np.empty((M, N))
z_t[:, :3] = pm_1
z_t[:, 3:] = vm_1
R_in = np.linalg.inv(R)
P_pred = np.bmat([[R_in, -(R_in)@(A)], [-(A.T)@(R_in), (A.T)@(R_in)@(A)]])
M_pred = np.zeros((2*N, 1))
M_pred[:N, :] = (B)@(u)
h_pred = (P_pred)@(M_pred)
g_pred = -0.5*(M_pred.T)@(P_pred)@(M_pred).flatten() -0.5*np.log( np.linalg.det(... | tracking/tracking.ipynb | scjrobertson/xRange | gpl-3.0 |
The Kalman Filter algorithm: Gaussian belief propagation | message_out = [C_X[0]]
prediction = [C_X[0]]
mean = np.zeros((N, L))
for i in np.arange(1, L):
#Kalman Filter Algorithm
C_Z[i].introduce_evidence([Z[i]], z_t[i, :])
marg = (message_out[i-1]*C_X[i]).marginalize([X[i-1]])
message_out.append(marg*C_Z[i])
mean[:, i] = (np.linalg.inv(message_out[... | tracking/tracking.ipynb | scjrobertson/xRange | gpl-3.0 |
<img src="images/kalman/kalman_hl.gif">
Two object tracking | fig = plt.figure(figsize=(25, 25))
ax = plt.axes(projection='3d')
ax.plot(p1[:, 0], p1[:, 1], p1[:, 2])
ax.plot(p2[:, 0], p2[:, 1], p2[:, 2], 'or')
ax.set_xlabel('x (m)', fontsize = '20')
ax.set_ylabel('y (m)', fontsize = '20')
ax.set_zlabel('z (m)', fontsize = '20')
ax.set_title('', fontsize = '20')
ax.set_ylim([-20... | tracking/tracking.ipynb | scjrobertson/xRange | gpl-3.0 |
Make Some Toy Data | import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x = np.random.randn(100) * 5
y = np.random.randn(100)
z = np.random.randn(100)
points = np.vstack([y,x,z]) | RPCA_Testing-3d.ipynb | fivetentaylor/rpyca | mit |
Add Some Outliers to Make Life Difficult | outliers = np.tile([15,-10,10], 10).reshape((-1,3))
pts = np.vstack([points.T, outliers]).T | RPCA_Testing-3d.ipynb | fivetentaylor/rpyca | mit |
Compute SVD on both the clean data and the outliery data | U,s,Vt = np.linalg.svd(points)
U_n,s_n,Vt_n = np.linalg.svd(pts) | RPCA_Testing-3d.ipynb | fivetentaylor/rpyca | mit |
Just 10 outliers can really screw up our line fit! | def randrange(n, vmin, vmax):
return (vmax-vmin)*np.random.rand(n) + vmin
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zl, zh)
ax.scatte... | RPCA_Testing-3d.ipynb | fivetentaylor/rpyca | mit |
Now the robust pca version! | import rpca
reload(rpca)
import logging
logger = logging.getLogger(rpca.__name__)
logger.setLevel(logging.INFO) | RPCA_Testing-3d.ipynb | fivetentaylor/rpyca | mit |
Factor the matrix into L (low rank) and S (sparse) parts | L,S = rpca.rpca(pts, eps=0.0000001, r=1) | RPCA_Testing-3d.ipynb | fivetentaylor/rpyca | mit |
Run SVD on the Low Rank Part | U,s,Vt = np.linalg.svd(L) | RPCA_Testing-3d.ipynb | fivetentaylor/rpyca | mit |
And have a look at this! | plt.ylim([-20,20])
plt.xlim([-20,20])
plt.scatter(*pts)
pts0 = np.dot(U[0].reshape((2,1)), np.array([-20,20]).reshape((1,2)))
plt.plot(*pts0)
plt.scatter(*L, c='red') | RPCA_Testing-3d.ipynb | fivetentaylor/rpyca | mit |
Have a look at the factored components... | plt.ylim([-20,20])
plt.xlim([-20,20])
plt.scatter(*L)
plt.scatter(*S, c='red') | RPCA_Testing-3d.ipynb | fivetentaylor/rpyca | mit |
It really does add back to the original matrix! | plt.ylim([-20,20])
plt.xlim([-20,20])
plt.scatter(*(L+S)) | RPCA_Testing-3d.ipynb | fivetentaylor/rpyca | mit |
We can open the fits file by fits.open() and check the info of the fits file by .info() | hdu_list = fits.open(image_file)
hdu_list.info() | notebooks/Feb2017/Astronomy/Astropy - Load fits.ipynb | ryan-leung/PHYS4650_Python_Tutorial | bsd-3-clause |
We get the data by the following command | image_data = hdu_list[0].data
print image_data | notebooks/Feb2017/Astronomy/Astropy - Load fits.ipynb | ryan-leung/PHYS4650_Python_Tutorial | bsd-3-clause |
We get the header by the following command | image_header = hdu_list[0].header
print image_header.items | notebooks/Feb2017/Astronomy/Astropy - Load fits.ipynb | ryan-leung/PHYS4650_Python_Tutorial | bsd-3-clause |
We can get individual header items by calling it as dictionary | print image_header['CRVAL1']
print image_header['CRVAL2'] | notebooks/Feb2017/Astronomy/Astropy - Load fits.ipynb | ryan-leung/PHYS4650_Python_Tutorial | bsd-3-clause |
This document is based heavily on this excellent resource from UCLA http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm
A categorical variable of K categories, or levels, usually enters a regression as a sequence of K-1 dummy variables. This amounts to a linear hypothesis on the level means. That is, each test s... | import pandas as pd
url = 'https://stats.idre.ucla.edu/stat/data/hsb2.csv'
hsb2 = pd.read_table(url, delimiter=",")
hsb2.head(10) | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
It will be instructive to look at the mean of the dependent variable, write, for each level of race ((1 = Hispanic, 2 = Asian, 3 = African American and 4 = Caucasian)). | hsb2.groupby('race')['write'].mean() | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
Treatment (Dummy) Coding
Dummy coding is likely the most well known coding scheme. It compares each level of the categorical variable to a base reference level. The base reference level is the value of the intercept. It is the default contrast in Patsy for unordered categorical factors. The Treatment contrast matrix fo... | from patsy.contrasts import Treatment
levels = [1,2,3,4]
contrast = Treatment(reference=0).code_without_intercept(levels)
print(contrast.matrix) | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
Here we used reference=0, which implies that the first level, Hispanic, is the reference category against which the other level effects are measured. As mentioned above, the columns do not sum to zero and are thus not independent of the intercept. To be explicit, let's look at how this would encode the race variable. | hsb2.race.head(10)
print(contrast.matrix[hsb2.race-1, :][:20])
sm.categorical(hsb2.race.values) | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
This is a bit of a trick, as the race category conveniently maps to zero-based indices. If it does not, this conversion happens under the hood, so this won't work in general but nonetheless is a useful exercise to fix ideas. The below illustrates the output using the three contrasts above | from statsmodels.formula.api import ols
mod = ols("write ~ C(race, Treatment)", data=hsb2)
res = mod.fit()
print(res.summary()) | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
We explicitly gave the contrast for race; however, since Treatment is the default, we could have omitted this.
Simple Coding
Like Treatment Coding, Simple Coding compares each level to a fixed reference level. However, with simple coding, the intercept is the grand mean of all the levels of the factors. Patsy doesn't h... | from patsy.contrasts import ContrastMatrix
def _name_levels(prefix, levels):
return ["[%s%s]" % (prefix, level) for level in levels]
class Simple(object):
def _simple_contrast(self, levels):
nlevels = len(levels)
contr = -1./nlevels * np.ones((nlevels, nlevels-1))
contr[1:][np.diag_ind... | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
Sum (Deviation) Coding
Sum coding compares the mean of the dependent variable for a given level to the overall mean of the dependent variable over all the levels. That is, it uses contrasts between each of the first k-1 levels and level k In this example, level 1 is compared to all the others, level 2 to all the others... | from patsy.contrasts import Sum
contrast = Sum().code_without_intercept(levels)
print(contrast.matrix)
mod = ols("write ~ C(race, Sum)", data=hsb2)
res = mod.fit()
print(res.summary()) | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
This corresponds to a parameterization that forces all the coefficients to sum to zero. Notice that the intercept here is the grand mean where the grand mean is the mean of means of the dependent variable by each level. | hsb2.groupby('race')['write'].mean().mean() | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
Backward Difference Coding
In backward difference coding, the mean of the dependent variable for a level is compared with the mean of the dependent variable for the prior level. This type of coding may be useful for a nominal or an ordinal variable. | from patsy.contrasts import Diff
contrast = Diff().code_without_intercept(levels)
print(contrast.matrix)
mod = ols("write ~ C(race, Diff)", data=hsb2)
res = mod.fit()
print(res.summary()) | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
For example, here the coefficient on level 1 is the mean of write at level 2 compared with the mean at level 1. Ie., | res.params["C(race, Diff)[D.1]"]
hsb2.groupby('race').mean()["write"][2] - \
hsb2.groupby('race').mean()["write"][1] | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
Helmert Coding
Our version of Helmert coding is sometimes referred to as Reverse Helmert Coding. The mean of the dependent variable for a level is compared to the mean of the dependent variable over all previous levels. Hence, the name 'reverse' being sometimes applied to differentiate from forward Helmert coding. This... | from patsy.contrasts import Helmert
contrast = Helmert().code_without_intercept(levels)
print(contrast.matrix)
mod = ols("write ~ C(race, Helmert)", data=hsb2)
res = mod.fit()
print(res.summary()) | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
To illustrate, the comparison on level 4 is the mean of the dependent variable at the previous three levels taken from the mean at level 4 | grouped = hsb2.groupby('race')
grouped.mean()["write"][4] - grouped.mean()["write"][:3].mean() | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
As you can see, these are only equal up to a constant. Other versions of the Helmert contrast give the actual difference in means. Regardless, the hypothesis tests are the same. | k = 4
1./k * (grouped.mean()["write"][k] - grouped.mean()["write"][:k-1].mean())
k = 3
1./k * (grouped.mean()["write"][k] - grouped.mean()["write"][:k-1].mean()) | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
Orthogonal Polynomial Coding
The coefficients taken on by polynomial coding for k=4 levels are the linear, quadratic, and cubic trends in the categorical variable. The categorical variable here is assumed to be represented by an underlying, equally spaced numeric variable. Therefore, this type of encoding is used only ... | hsb2['readcat'] = np.asarray(pd.cut(hsb2.read, bins=3))
hsb2.groupby('readcat').mean()['write']
from patsy.contrasts import Poly
levels = hsb2.readcat.unique().tolist()
contrast = Poly().code_without_intercept(levels)
print(contrast.matrix)
mod = ols("write ~ C(readcat, Poly)", data=hsb2)
res = mod.fit()
print(res.su... | examples/notebooks/contrasts.ipynb | ChadFulton/statsmodels | bsd-3-clause |
Simple Layouts
In order to add widgets or have multiple plots that are linked together, you must first be able to create documents that contain these separate objects. It is possible to accomplish this in your own custom templates using bokeh.embed.components. But, Bokeh also provides simple layout capability for grid ... | from bokeh.plotting import figure
from bokeh.io import gridplot
x = list(range(11))
y0, y1, y2 = x, [10-i for i in x], [abs(i-5) for i in x]
# create a new plot
s1 = figure(width=250, plot_height=250)
s1.circle(x, y0, size=10, color="navy", alpha=0.5)
# create another one
s2 = figure(width=250, height=250)
s2.triang... | 04 - interactions.ipynb | flaxandteal/python-course-lecturer-notebooks | mit |
Bokeh also provides the vplot and hplot functions to arrange plot objects in vertical or horizontal layouts. | # EXERCISE: use vplot to arrange a few plots vertically
| 04 - interactions.ipynb | flaxandteal/python-course-lecturer-notebooks | mit |
Linked Interactions
It is possible to link various interactions between different Bokeh plots. For instance, the ranges of two (or more) plots can be linked, so that when one of the plots is panned (or zoomed, or otherwise has its range changed) the other plots will update in unison. It is also possible to link selecti... | plot_options = dict(width=250, plot_height=250, title=None, tools='pan')
# create a new plot
s1 = figure(**plot_options)
s1.circle(x, y0, size=10, color="navy")
# create a new plot and share both ranges
s2 = figure(x_range=s1.x_range, y_range=s1.y_range, **plot_options)
s2.triangle(x, y1, size=10, color="firebrick")
... | 04 - interactions.ipynb | flaxandteal/python-course-lecturer-notebooks | mit |
Linked brushing
Linking selections is accomplished in a similar way, by sharing data sources between plots. Note that normally with bokeh.plotting and bokeh.charts creating a default data source for simple plots is handled automatically. However to share a data source, we must create them by hand and pass them explicit... | from bokeh.models import ColumnDataSource
x = list(range(-20, 21))
y0, y1 = [abs(xx) for xx in x], [xx**2 for xx in x]
# create a column data source for the plots to share
source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1))
TOOLS = "box_select,lasso_select,help"
# create a new plot and add a renderer
left = fig... | 04 - interactions.ipynb | flaxandteal/python-course-lecturer-notebooks | mit |
Hover Tools
Bokeh has a Hover Tool that allows additional information to be displayed in a popup whenever the uer howevers over a specific glyph. Basic hover tool configuration amounts to providing a list of (name, format) tuples. The full details can be found in the User's Guide here.
The example below shows some basi... | from bokeh.models import HoverTool
source = ColumnDataSource(
data=dict(
x=[1, 2, 3, 4, 5],
y=[2, 5, 8, 2, 7],
desc=['A', 'b', 'C', 'd', 'E'],
)
)
hover = HoverTool(
tooltips=[
("index", "$index"),
("(x,y)", "($x, $y)"),
... | 04 - interactions.ipynb | flaxandteal/python-course-lecturer-notebooks | mit |
Ipython Interactors
It is possible to use native IPython notebook interactors together with Bokeh. In the interactor update function, the push_notebook method can be used to update a data source (presumably based on the iteractor widget values) to cause a plot to update.
Warning: The current implementation of push_note... | import numpy as np
from bokeh.models import Line
x = np.linspace(0, 2*np.pi, 2000)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))
p = figure(title="simple line example", plot_height=300, plot_width=600, y_range=(-5, 5))
p.line(x, y, color="#2222aa", alpha=0.5, line_width=2, source=source, name="foo")
... | 04 - interactions.ipynb | flaxandteal/python-course-lecturer-notebooks | mit |
Widgets
Bokeh supports direct integration with a small basic widget set. Thse can be used in conjunction with a Bokeh Server, or with CustomJS models to add more interactive capability to your documents. You can see a complete list, with example code in the Adding Widgets section of the User's Guide.
To use the widget... | from bokeh.models.widgets import Slider
from bokeh.io import vform
slider = Slider(start=0, end=10, value=1, step=.1, title="foo")
show(vform(slider))
# EXERCISE: create and show a Select widget
| 04 - interactions.ipynb | flaxandteal/python-course-lecturer-notebooks | mit |
Callbacks | from bokeh.models import TapTool, CustomJS, ColumnDataSource
callback = CustomJS(code="alert('hello world')")
tap = TapTool(callback=callback)
p = figure(plot_width=600, plot_height=300, tools=[tap])
p.circle('x', 'y', size=20, source=ColumnDataSource(data=dict(x=[1, 2, 3, 4, 5], y=[2, 5, 8, 2, 7])))
show(p) | 04 - interactions.ipynb | flaxandteal/python-course-lecturer-notebooks | mit |
Lots of places to add callbacks
Widgets - Button, Toggle, Dropdown, TextInput, AutocompleteInput, Select, Multiselect, Slider, (DateRangeSlider), DatePicker,
Tools - TapTool, BoxSelectTool, HoverTool,
Selection - ColumnDataSource, AjaxDataSource, BlazeDataSource, ServerDataSource
Ranges - Range1d, DataRange1d, FactorR... | from bokeh.io import vform
from bokeh.models import CustomJS, ColumnDataSource, Slider
x = [x*0.005 for x in range(0, 200)]
y = x
source = ColumnDataSource(data=dict(x=x, y=y))
plot = figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
callback = CustomJS(args=di... | 04 - interactions.ipynb | flaxandteal/python-course-lecturer-notebooks | mit |
Calbacks for selections
It's also possible to make JavaScript actions that execute whenever a user selection (e.g., box, point, lasso) changes. This is done by attaching the same kind of CustomJS object to whatever data source the selection is made on.
The example below is a bit more sophisticaed, and demonstrates upda... | from random import random
x = [random() for x in range(500)]
y = [random() for y in range(500)]
color = ["navy"] * len(x)
s = ColumnDataSource(data=dict(x=x, y=y, color=color))
p = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here")
p.circle('x', 'y', color='color', size=8, source=s, al... | 04 - interactions.ipynb | flaxandteal/python-course-lecturer-notebooks | mit |
k-means
k-means clustering is an unsupervised machine learning algorithm that can be used to group items into clusters.
So far we have only worked with supervised algorithms. Supervised algorithms have training data with labels that identify the numeric value or class for each item. These algorithms use labeled data to... | ! chmod 600 kaggle.json && (ls ~/.kaggle 2>/dev/null || mkdir ~/.kaggle) && mv kaggle.json ~/.kaggle/ && echo 'Done' | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
And then use the Kaggle API to download the dataset. | ! kaggle datasets download uciml/mushroom-classification
! ls | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
Unzip the Data. | ! unzip mushroom-classification.zip
! ls | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
And finally, load the training data into a DataFrame. | import pandas as pd
data = pd.read_csv('mushrooms.csv')
data.sample(n=10) | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
Exploratory Data Analysis
Let's take a closer look at the data that we'll be working with, starting with a simple describe. | data.describe(include='all') | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
It doesn't look like any columns are missing data since we see counts of 8,124 for every column.
It does look like all of the data is categorical. We'll need to convert it into numeric values for the model to work. Let's do it for every column except class. We aren't trying to predict class, but we do want to see if we... | columns = [c for c in data.columns.values if c != 'class']
id_to_value_mappings = {}
value_to_id_mappings = {}
for column in columns:
i_to_v = sorted(data[column].unique())
v_to_i = { v:i for i, v in enumerate(i_to_v)}
numeric_column = column + '-id'
data[numeric_column] = [v_to_i[v] for v in data[column]]... | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
Perform Clustering
We now have numeric data that a model can handle. To run k-means clustering on the data, we simply load k-means from scikit-learn and ask the model to find a specific number of clusters for us.
Notice that we are scaling the data. The class IDs are integer values, and some columns have many more clas... | from sklearn.cluster import KMeans
from sklearn.preprocessing import scale
model = KMeans(n_clusters=10)
model.fit(scale(data[numeric_columns]))
print(model.inertia_) | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
We asked scikit-learn to create 10 clusters for us, and then we printed out the inertia_ for the resultant clusters. Inertia is the sum of the squared distances of samples to their closest cluster center. Typically, the smaller the inertia the better.
But why did we choose 10 clusters? And is the inertia that we receiv... | from sklearn.cluster import KMeans
from sklearn.preprocessing import scale
import matplotlib.pyplot as plt
clusters = list(range(5, 50, 5))
inertias = []
scaled_data = scale(data[numeric_columns])
for c in clusters:
model = KMeans(n_clusters=c)
model = model.fit(scaled_data)
inertias.append(model.inertia_)
pl... | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
The resulting graph should start high and to the left and curve down as the number of clusters grows. The initial slope is steep, but begins to level off. Your optimal number of clusters is somewhere in the ["elbow" of the graph](https://en.wikipedia.org/wiki/Elbow_method_(clustering) as the slope levels.
Once you have... | from sklearn.cluster import KMeans
from sklearn.preprocessing import scale
model = KMeans(n_clusters=15)
model.fit(scale(data[numeric_columns]))
print(model.inertia_) | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
Now let's see if we have any 'pure' clusters. These are clusters with all-edible or all-poisonous mushrooms. | import numpy as np
for cluster in sorted(np.unique(model.labels_)):
num_edible = np.sum(data[model.labels_ == cluster]['class'] == 'e')
total = np.sum(model.labels_ == cluster)
print(cluster, num_edible / total) | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
In our model we had clusters 0, 1, 6, and 10 be 100% edible. Clusters 2, 4, 7, and 12 were all poisonous. The remaining were a mix of the two.
Knowing this, let's look at one of the all-edible clusters and see what attributes we could look for to have confidence that we have an edible mushroom. | edible = data[model.labels_ == 1]
for column in edible.columns:
if column.endswith('-id'):
continue
print(column, edible[column].unique())
| content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
The mapping of the letter codes to more descriptive text can be found in the dataset description.
Example: Classification of Digits
Clustering for data exploration purposes can lead to interesting insights in to your data, but clustering can also be used for classification purposes.
In the example below, we'll try to u... | from sklearn.datasets import load_digits
digits = load_digits() | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
Scale the Data
It is good practice to scale the data to ensure that outliers don't have too big of an impact on the clustering. | from sklearn.preprocessing import scale
scaled_digits = scale(digits.data) | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
Fit a Model
We can then create a k-means model with 10 clusters. (We know there are 10 digits from 0 through 9.) | from sklearn.cluster import KMeans
model = KMeans(n_clusters=10)
model = model.fit(scaled_digits) | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
Make Predictions
We can then use the model to predict which category a data point belongs to.
In the case below, we'll just use some of the data that we trained with for illustrative purposes. The prediction will provide a numeric value. | cluster = model.predict([scaled_digits[0]])[0]
cluster | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
What is this value? Is it the predicted digit?
No. This number is the cluster that the model thinks the digit belongs to. To determine the predicted digit, we'll need to see what other digits are in the cluster and choose the most popular one for our classification. | import numpy as np
labels = digits.target
cluster_to_digit = [
np.argmax(
np.bincount(
np.array(
[labels[i] for i in range(len(model.labels_)) if model.labels_[i] == cluster]
)
)
) for cluster in range(10)
]
cluster_to_digit | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
Here we can see the digit that each cluster represents.
Measure Model Quality
If we do have labeled data, as is the case with our digits data, then we can measure the quality of our model using the homogeneity score and the completeness score. | from sklearn.metrics import homogeneity_score
from sklearn.metrics import completeness_score
homogeneity = homogeneity_score(labels, model.labels_)
completeness = completeness_score(labels, model.labels_)
homogeneity, completeness | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
Exercises
Exercise 1
Load the iris dataset, create a k-means model with three clusters, and then find the homogeneity and completeness scores for the model.
Student Solution | # Your code goes here | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
Exercise 2
Load the iris dataset, and then create a k-means model with three clusters using only two features. (Try to find the best two features for clustering.) Create a plot of the two features.
For each datapoint in the chart, use a marker to encode the actual/correct species. For instance, use a triangle for Setos... | # Your code goes here | content/06_other_models/01_k_means/colab.ipynb | google/applied-machine-learning-intensive | apache-2.0 |
The tables define the value and error as a string:
val (err)
which is a pain in the ass because now I have to parse the strings, which always takes much longer than it should because data wrangling is hard sometimes.
I define a function that takes a column name and a data frame and strips the output. | def strip_parentheses(col, df):
'''
splits single column strings of "value (error)" into two columns of value and error
input:
-string name of column to split in two
-dataframe to apply to
returns dataframe
'''
out1 = df[col].str.replace(")","").str.split(pat="(")
df_o... | notebooks/Patten2006.ipynb | BrownDwarf/ApJdataFrames | mit |
Table 1 - Basic data on sources | names = ["Name","R.A. (J2000.0)","Decl. (J2000.0)","Spectral Type","SpectralType Ref.","Parallax (error)(arcsec)",
"Parallax Ref.","J (error)","H (error)","Ks (error)","JHKRef.","PhotSys"]
tbl1 = pd.read_csv("http://iopscience.iop.org/0004-637X/651/1/502/fulltext/64991.tb1.txt",
sep='\t', ... | notebooks/Patten2006.ipynb | BrownDwarf/ApJdataFrames | mit |
Table 3- IRAC photometry | names = ["Name","Spectral Type","[3.6] (error)","n1","[4.5] (error)","n2",
"[5.8] (error)","n3","[8.0] (error)","n4","[3.6]-[4.5]","[4.5]-[5.8]","[5.8]-[8.0]","Notes"]
tbl3 = pd.read_csv("http://iopscience.iop.org/0004-637X/651/1/502/fulltext/64991.tb3.txt",
sep='\t', names=names, na_value... | notebooks/Patten2006.ipynb | BrownDwarf/ApJdataFrames | mit |
Convert spectral type to number | import gully_custom
patten2006["SpT_num"], _1, _2, _3= gully_custom.specTypePlus(patten2006["Spectral Type"]) | notebooks/Patten2006.ipynb | BrownDwarf/ApJdataFrames | mit |
Make a plot of mid-IR colors as a function of spectral type. | sns.set_context("notebook", font_scale=1.5)
for color in ["[3.6]-[4.5]", "[4.5]-[5.8]", "[5.8]-[8.0]"]:
plt.plot(patten2006["SpT_num"], patten2006[color], '.', label=color)
plt.xlabel(r'Spectral Type (M0 = 0)')
plt.ylabel(r'$[3.6]-[4.5]$')
plt.title("IRAC colors as a function of spectral type")
plt.legend(loc... | notebooks/Patten2006.ipynb | BrownDwarf/ApJdataFrames | mit |
Save the cleaned data. | patten2006.to_csv('../data/Patten2006/patten2006.csv', index=False) | notebooks/Patten2006.ipynb | BrownDwarf/ApJdataFrames | mit |
Part 1: Load Example Dataset
We start this exercise by using a small dataset that is easily to
visualize. | ex7data1 = scipy.io.loadmat('ex7data1.mat')
X = ex7data1['X']
def plot_data(X, ax):
ax.plot(X[:,0], X[:,1], 'bo')
fig, ax = plt.subplots()
plot_data(X, ax)
def normalize_features(X):
mu = np.mean(X, 0)
X_norm = X - mu
sigma = np.std(X_norm, 0)
X_norm = X_norm / sigma
return X_norm, mu, si... | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
Part 2: Principal Component Analysis
You should now implement PCA, a dimension reduction technique. You
should complete the following code. | def pca(X):
#PCA Run principal component analysis on the dataset X
# [U, S] = pca(X) computes eigenvectors of the covariance matrix of X
# Returns the eigenvectors U, the eigenvalues in S
#
m, n = X.shape
# You need to return the following variables correctly.
U = np.zeros((n, ... | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
Draw the eigenvectors centered at mean of data. These lines show the
directions of maximum variations in the dataset. | def draw_line(a, b, ax, *args):
ax.plot([a[0], b[0]], [a[1], b[1]], *args)
fig, ax = plt.subplots(figsize=(5,5))
ax.set_ylim(2, 8)
ax.set_xlim(0.5, 6.5)
ax.set_aspect('equal')
plot_data(X, ax)
ax.plot(mu[0], mu[1])
draw_line(mu, mu + 1.5 * S[0] * U[0, :], ax, '-k')
draw_line(mu, mu + 1.5 * S[1] * U[1, :], ax, '-k'... | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
The top eigenvector should be [-0.707107, -0.707107]. | U[0] | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
Part 3: Dimension Reduction
You should now implement the projection step to map the data onto the
first k eigenvectors. The code will then plot the data in this reduced
dimensional space. This will show you what the data looks like when
using only the corresponding eigenvectors to reconstruct it.
You should c... | def project_data(X, U, K):
#PROJECTDATA Computes the reduced data representation when projecting only
#on to the top k eigenvectors
# Z = projectData(X, U, K) computes the projection of
# the normalized inputs X into the reduced dimensional space spanned by
# the first K columns of U. It ret... | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
Projection of the first example: (should be about 1.49631261) | K = 1
Z = project_data(X_norm, U, K)
Z[0,0]
def recover_data(Z, U, K):
#RECOVERDATA Recovers an approximation of the original data when using the
#projected data
# X_rec = RECOVERDATA(Z, U, K) recovers an approximation the
# original data that has been reduced to K dimensions. It returns the
... | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
Approximation of the first example: (should be about [-1.05805279, -1.05805279]) | X_rec = recover_data(Z, U, K)
X_rec[0] | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
Draw lines connecting the projected points to the original points | fig, ax = plt.subplots(figsize=(5,5))
ax.set_ylim(-3, 3)
ax.set_xlim(-3, 3)
ax.set_aspect('equal')
plot_data(X_norm, ax)
ax.plot(X_rec[:,0], X_rec[:,1], 'ro')
for x_norm, x_rec in zip(X_norm, X_rec):
draw_line(x_norm, x_rec, ax, '--k') | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
Part 4: Loading and Visualizing Face Data
We start the exercise by first loading and visualizing the dataset.
The following code will load the dataset into your environment, and later display the first 100 faces in the dataset. | X = scipy.io.loadmat('ex7faces.mat')['X']
X.shape
def display_faces(X, example_width=None):
example_size = len(X[0])
if example_width is None:
example_width = int(np.sqrt(example_size))
num_examples = len(X)
figures_row_length = int(np.sqrt(num_examples))
fig, axes = plt.subplots(n... | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
Part 5: PCA on Face Data: Eigenfaces
Run PCA and visualize the eigenvectors which are in this case eigenfaces
We display the first 64 eigenfaces.
Before running PCA, it is important to first normalize X. | X_norm, mu, sigma = normalize_features(X)
U, S = pca(X_norm)
display_faces(U[:, :64].T) | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
Part 6: Dimension Reduction for Faces
Project images to the eigen space using the top k eigenvectors | K = 100
Z = project_data(X_norm, U, K)
Z.shape | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
Part 7: Visualization of Faces after PCA Dimension Reduction
Project images to the eigen space using the top K eigen vectors and
visualize only using those K dimensions.
Compare to the original input. | X_rec = recover_data(Z, U, K)
X_rec.shape
display_faces(X_rec[:100]) | ex7/ml-ex7-pca.ipynb | noammor/coursera-machinelearning-python | mit |
.. _tut_erp:
EEG processing and Event Related Potentials (ERPs)
For a generic introduction to the computation of ERP and ERF
see :ref:tut_epoching_and_averaging. Here we cover the specifics
of EEG, namely:
- setting the reference
- using standard montages :func:mne.channels.Montage
- Evoked arithmetic (e.g.... | import mne
from mne.datasets import sample | 0.12/_downloads/plot_eeg_erp.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Setup for reading the raw data | data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'
raw = mne.io.read_raw_fif(raw_fname, add_eeg_ref=True, preload=True) | 0.12/_downloads/plot_eeg_erp.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
And it's actually possible to plot the channel locations using
the :func:mne.io.Raw.plot_sensors method | raw.plot_sensors()
raw.plot_sensors('3d') # in 3D | 0.12/_downloads/plot_eeg_erp.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Setting EEG montage
In the case where your data don't have locations you can set them
using a :func:mne.channels.Montage. MNE comes with a set of default
montages. To read one of them do: | montage = mne.channels.read_montage('standard_1020')
print(montage) | 0.12/_downloads/plot_eeg_erp.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
To apply a montage on your data use the :func:mne.io.set_montage
function. Here don't actually call this function as our demo dataset
already contains good EEG channel locations.
Next we'll explore the definition of the reference.
Setting EEG reference
Let's first remove the reference from our Raw object.
This explicit... | raw_no_ref, _ = mne.io.set_eeg_reference(raw, []) | 0.12/_downloads/plot_eeg_erp.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Average reference: This is normally added by default, but can also
be added explicitly. | raw_car, _ = mne.io.set_eeg_reference(raw)
evoked_car = mne.Epochs(raw_car, **epochs_params).average()
del raw_car # save memory
title = 'EEG Average reference'
evoked_car.plot(titles=dict(eeg=title))
evoked_car.plot_topomap(times=[0.1], size=3., title=title) | 0.12/_downloads/plot_eeg_erp.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Custom reference: Use the mean of channels EEG 001 and EEG 002 as
a reference | raw_custom, _ = mne.io.set_eeg_reference(raw, ['EEG 001', 'EEG 002'])
evoked_custom = mne.Epochs(raw_custom, **epochs_params).average()
del raw_custom # save memory
title = 'EEG Custom reference'
evoked_custom.plot(titles=dict(eeg=title))
evoked_custom.plot_topomap(times=[0.1], size=3., title=title) | 0.12/_downloads/plot_eeg_erp.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Next, we create averages of stimulation-left vs stimulation-right trials.
We can use basic arithmetic to, for example, construct and plot
difference ERPs. | left, right = epochs["left"].average(), epochs["right"].average()
(left - right).plot_joint() # create and plot difference ERP | 0.12/_downloads/plot_eeg_erp.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Note that by default, this is a trial-weighted average. If you have
imbalanced trial numbers, consider either equalizing the number of events per
condition (using Epochs.equalize_event_counts), or the combine_evoked
function.
As an example, first, we create individual ERPs for each condition. | aud_l = epochs["auditory", "left"].average()
aud_r = epochs["auditory", "right"].average()
vis_l = epochs["visual", "left"].average()
vis_r = epochs["visual", "right"].average()
all_evokeds = [aud_l, aud_r, vis_l, vis_r]
# This could have been much simplified with a list comprehension:
# all_evokeds = [epochs[cond] f... | 0.12/_downloads/plot_eeg_erp.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Set Up Verta | HOST = 'app.verta.ai'
PROJECT_NAME = 'Film Review Classification'
EXPERIMENT_NAME = 'spaCy CNN'
# import os
# os.environ['VERTA_EMAIL'] =
# os.environ['VERTA_DEV_KEY'] =
from verta import Client
from verta.utils import ModelAPI
client = Client(HOST, use_git=False)
proj = client.set_project(PROJECT_NAME)
expt = c... | client/workflows/examples/text_classification_spacy.ipynb | mitdbg/modeldb | mit |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.