markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
When we increase the degree to this extent, it's clear that the resulting fit is no longer reflecting the true underlying distribution, but is more sensitive to the noise in the training data. For this reason, we call it a high-variance model, and we say that it over-fits the data.
Just for fun, let's use IPython's int... | from ipywidgets import interact
def plot_fit(degree=1, Npts=50):
X, y = make_data(Npts, error=1)
X_test = np.linspace(-0.1, 1.1, 500)[:, None]
model = PolynomialRegression(degree=degree)
model.fit(X, y)
y_test = model.predict(X_test)
plt.scatter(X.ravel(), y)
plt.plot(X_test.ravel(), ... | present/bi2/2020/ubb/az_en_jupyter2_mappam/sklearn_tutorial/05-Validation.ipynb | csaladenes/csaladenes.github.io | mit |
Detecting Over-fitting with Validation Curves
Clearly, computing the error on the training data is not enough (we saw this previously). As above, we can use cross-validation to get a better handle on how the model fit is working.
Let's do this here, again using the validation_curve utility. To make things more clear, w... | X, y = make_data(120, error=1.0)
plt.scatter(X, y);
from sklearn.model_selection import validation_curve
def rms_error(model, X, y):
y_pred = model.predict(X)
return np.sqrt(np.mean((y - y_pred) ** 2))
degree = np.arange(0, 18)
val_train, val_test = validation_curve(PolynomialRegression(), X, y,
... | present/bi2/2020/ubb/az_en_jupyter2_mappam/sklearn_tutorial/05-Validation.ipynb | csaladenes/csaladenes.github.io | mit |
Now let's plot the validation curves: | def plot_with_err(x, data, **kwargs):
mu, std = data.mean(1), data.std(1)
lines = plt.plot(x, mu, '-', **kwargs)
plt.fill_between(x, mu - std, mu + std, edgecolor='none',
facecolor=lines[0].get_color(), alpha=0.2)
plot_with_err(degree, val_train, label='training scores')
plot_with_err(... | present/bi2/2020/ubb/az_en_jupyter2_mappam/sklearn_tutorial/05-Validation.ipynb | csaladenes/csaladenes.github.io | mit |
Notice the trend here, which is common for this type of plot.
For a small model complexity, the training error and validation error are very similar. This indicates that the model is under-fitting the data: it doesn't have enough complexity to represent the data. Another way of putting it is that this is a high-bias ... | model = PolynomialRegression(4).fit(X, y)
plt.scatter(X, y)
plt.plot(X_test, model.predict(X_test)); | present/bi2/2020/ubb/az_en_jupyter2_mappam/sklearn_tutorial/05-Validation.ipynb | csaladenes/csaladenes.github.io | mit |
Detecting Data Sufficiency with Learning Curves
As you might guess, the exact turning-point of the tradeoff between bias and variance is highly dependent on the number of training points used. Here we'll illustrate the use of learning curves, which display this property.
The idea is to plot the mean-squared-error for ... | from sklearn.model_selection import learning_curve
def plot_learning_curve(degree=3):
train_sizes = np.linspace(0.05, 1, 120)
N_train, val_train, val_test = learning_curve(PolynomialRegression(degree),
X, y, train_sizes, cv=5,
... | present/bi2/2020/ubb/az_en_jupyter2_mappam/sklearn_tutorial/05-Validation.ipynb | csaladenes/csaladenes.github.io | mit |
Let's see what the learning curves look like for a linear model: | plot_learning_curve(1) | present/bi2/2020/ubb/az_en_jupyter2_mappam/sklearn_tutorial/05-Validation.ipynb | csaladenes/csaladenes.github.io | mit |
This shows a typical learning curve: for very few training points, there is a large separation between the training and test error, which indicates over-fitting. Given the same model, for a large number of training points, the training and testing errors converge, which indicates potential under-fitting.
As you add mo... | plot_learning_curve(3) | present/bi2/2020/ubb/az_en_jupyter2_mappam/sklearn_tutorial/05-Validation.ipynb | csaladenes/csaladenes.github.io | mit |
Here we see that by adding more model complexity, we've managed to lower the level of convergence to an rms error of 1.0!
What if we get even more complex? | plot_learning_curve(10) | present/bi2/2020/ubb/az_en_jupyter2_mappam/sklearn_tutorial/05-Validation.ipynb | csaladenes/csaladenes.github.io | mit |
Try out Environment | BeraterEnv.showStep = True
BeraterEnv.showDone = True
env = BeraterEnv()
print(env)
observation = env.reset()
print(observation)
for t in range(1000):
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done:
print("Episode finished after {} timesteps".format(t... | notebooks/rl/berater-v4.ipynb | DJCordhose/ai | mit |
Train model
0.73 would be perfect total reward | !rm -r logs
!mkdir logs
!mkdir logs/berater
# https://github.com/openai/baselines/blob/master/baselines/deepq/experiments/train_pong.py
# log_dir = logger.get_dir()
log_dir = '/content/logs/berater/'
import gym
from baselines import deepq
from baselines import bench
from baselines import logger
from baselines.common... | notebooks/rl/berater-v4.ipynb | DJCordhose/ai | mit |
Visualizing Results
https://github.com/openai/baselines/blob/master/docs/viz/viz.ipynb | !ls -l $log_dir
from baselines.common import plot_util as pu
results = pu.load_results(log_dir)
import matplotlib.pyplot as plt
import numpy as np
r = results[0]
# plt.ylim(-1, 1)
# plt.plot(np.cumsum(r.monitor.l), r.monitor.r)
plt.plot(np.cumsum(r.monitor.l), pu.smooth(r.monitor.r, radius=100)) | notebooks/rl/berater-v4.ipynb | DJCordhose/ai | mit |
Enjoy model | import numpy as np
observation = env.reset()
state = np.zeros((1, 2*128))
dones = np.zeros((1))
BeraterEnv.showStep = True
BeraterEnv.showDone = False
for t in range(1000):
actions, _, state, _ = model.step(observation, S=state, M=dones)
observation, reward, done, info = env.step(actions[0])
if done:
... | notebooks/rl/berater-v4.ipynb | DJCordhose/ai | mit |
为了修正这个问题,你可以修改模式字符串,增加对换行的支持。比如: | comment = re.compile(r'/\*((?:.|\n)*?)\*/')
comment.findall(text2) | 02 strings and text/02.08 regexp for multiline partterns.ipynb | wuafeing/Python3-Tutorial | gpl-3.0 |
在这个模式中, (?:.|\n) 指定了一个非捕获组 (也就是它定义了一个仅仅用来做匹配,而不能通过单独捕获或者编号的组)。
讨论
re.compile() 函数接受一个标志参数叫 re.DOTALL ,在这里非常有用。 它可以让正则表达式中的点 (.) 匹配包括换行符在内的任意字符。比如: | comment = re.compile(r'/\*(.*?)\*/', re.DOTALL)
comment.findall(text2) | 02 strings and text/02.08 regexp for multiline partterns.ipynb | wuafeing/Python3-Tutorial | gpl-3.0 |
Python
Python is a widely used high-level programming language for general-purpose programming, created by Guido van Rossum and first released in 1991. An interpreted language, Python has a design philosophy which emphasizes code readability (notably using whitespace indentation to delimit code blocks rather than curly... | from __future__ import division # fix division
from __future__ import print_function # use print function
print('hello world') # single quotes
print("hello world") # double quotes
print('3/4 is', 3/4) # this prints 0.75
print('I am {} ... for {} yrs I have been training Jedhi'.format("Yoda", 853))
print('fl... | website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
Unicode
Unicode sucks in python 2.7, but if you want to use it:
alphabets
arrows
emoji | print(u'\u21e6 \u21e7 \u21e8 \u21e9')
print(u'\u2620')
# this is a dictionary, we will talk about it next ... sorry for the out of order
uni = {
'left': u'\u21e6',
'up': u'\u21e7',
'right': u'\u21e8',
'down': u'\u21e9',
}
print(u'\nYou must go {}'.format(uni['up'])) # notice all strings have u on the ... | website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
Data Types
Python isn't typed, so you don't really need to keep track of variables and delare them as ints, floats, doubles, unsigned, etc. There are a few places where this isn't true, but we will deal with those as we encounter them. | # bool
z = True # or False
# integers (default)
z = 3
# floats
z = 3.124
z = 5/2
print('z =', z)
# dictionary or hash tables
bob = {'a': 5, 'b': 6}
print('bob["a"]:', bob['a'])
# you can assign a new key/values pair
bob['c'] = 'this is a string!!'
print(bob)
print('len(bob) =', len(bob))
# you can also access wha... | website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
Flow Control
Logic Operators
Flow control is generally done via some math operator or boolean logic operator.
For Loop | # range(start, stop, step) # this only works for integer values
range(3,10) # jupyter cell will always print the last thing
# iterates from start (default 0) to less than the highest number
for i in range(5):
print(i)
# you can also create simple arrays like this:
bob = [2*x+3 for x in range(4)]
print('bob one-... | website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
if / elif / else | # classic if/then statements work the same as other languages.
# if the statement is True, then do something, if it is False, then skip over it.
if False:
print('should not get here')
elif True:
print('this should print')
else:
print('this is the default if all else fails')
n = 5
n = 3 if n==1 else n-1 # o... | website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
While | x = 3
while True: # while loop runs while value is True
if not x: # I will enter this if statement when x = False or 0
break # breaks me out of a loop
else:
print(x)
x -= 1
| website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
Exception Handling
When you write code you should think about how you could break it, then design it so you can't. Now, you don't necessary need to write bullet proof code ... that takes a lot of time (and time is money), but you should make an effort to reduce your debug time.
A list of Python 2.7 exceptions is here. ... | # exception handling ... use in your code in smart places
try:
a = (1,2,) # tupple ... notice the extra comma after the 2
a[0] = 1 # this won't work!
except: # this catches any exception thrown
print('you idiot ... you cannot modify a tuple!!')
# error
5/0
try:
5/0
except ZeroDivisionError as e:
... | website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
When would you want to use raise?
Why not always handle the error here?
What is different when the raise command is used? | # Honestly, I generally just use Exception from which most other exceptions
# are derived from, but I am lazy and it works fine for what I do
try:
5/0
except Exception as e:
print(e)
# all is right with the world ... these will work, nothing will print
assert True
assert 3 > 1
# this will fail ... and we can ... | website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
Libraries
We will need to import math to have access to trig and other functions. There will be other libraries like numpy, cv2, etc you will need to. | import math
print('messy', math.cos(math.pi/4))
# that looks clumbsy ... let's do this instead
from math import cos, pi
print('simpler math:', cos(pi/4))
# or we just want to shorten the name to reduce typing ... good programmers are lazy!
import numpy as np
# well what is in the math library I might want to use???... | website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
Functions
There isn't too much that is special about python functions, just the format. | def my_cool_function(x):
"""
This is my cool function which takes an argument x
and returns a value
"""
return 2*x/3
my_cool_function(6) # 2*6/3 = 4 | website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
Classes and Object Oriented Programming (OOP)
Ok, we don't have time to really teach you how to do this. It would be better if your real programming classes did this. So we are just going to kludge this together here, because these could be useful in this class. In fact I generally (and 99% of the world) does OOP.
Clas... | class ClassName(object):
"""
So this is my cool class
"""
def __init__(self, x):
"""
This is called a constructor in OOP. When I make an object
this function is called.
self = contains all of the objects values
x = an argument to pass something into the constructo... | website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
There are tons of things you can do with objects. Here is one example. Say we have a ball class and for some reason we want to be able to add balls together. | class Ball(object):
def __init__(self, color, radius):
# this ball always has this color and raduis below
self.radius = radius
self.color = color
def __str__(self):
"""
When something tries to turn this object into a string,
this function gets called
... | website/block_1_basics/lsn3/lsn3.ipynb | MarsUniversity/ece387 | mit |
Importamos los paquetes necesarios: | import numpy as np
import matplotlib.pyplot as plt | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
La biblioteca matplotlib es gigantesca y es difícil hacerse una idea global de todas sus posibilidades en una primera toma de contacto. Es recomendable tener a mano la documentación y la galería (http://matplotlib.org/gallery.html#pylab_examples):
Interfaz pyplot
La interfaz pyplot proporciona una serie de funciones qu... | plt
plt.plot([0.0, 0.1, 0.2, 0.7, 0.9], [1, -2, 3, 4, 1]) | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
La función plot recibe una sola lista (si queremos especificar los valores y) o dos listas (si especificamos x e y). Naturalmente si especificamos dos listas ambas tienen que tener la misma longitud.
La tarea más habitual a la hora de trabajar con matplotlib es representar una función. Lo que tendremos que hacer es def... | def f(x):
return np.exp(-x ** 2) | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
Definimos el dominio con la función np.linspace, que crea un vector de puntos equiespaciados: | x = np.linspace(-1, 3, 100) | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
Y representamos la función: | plt.plot(x, f(x), label="Función f(x)")
plt.xlabel("Eje $x$")
plt.ylabel("$f(x)$")
plt.legend()
plt.title("Función $f(x)$") | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
Notamos varias cosas:
Con diversas llamadas a funciones dentro de plt. se actualiza el gráfico actual. Esa es la forma de trabajar con la interfaz pyplot.
Podemos añadir etiquetas, y escribir $\LaTeX$ en ellas. Tan solo hay que encerrarlo entre signos de dólar $$.
Añadiendo como argumento label podemos definir una ley... | plt.plot(x, f(x), 'ro')
plt.plot(x, 1 - f(x), 'g--') | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
Esto en realidad son códigos abreviados, que se corresponden con argumentos de la función plot: | plt.plot(x, f(x), color='red', linestyle='', marker='o')
plt.plot(x, 1 - f(x), c='g', ls='--') | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
La lista de posibles argumentos y abreviaturas está disponible en la documentación de la función plot http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot.
Más personalización, pero a lo loco
Desde matplotlib 1.4 se puede manipular fácilmente la apariencia de la gráfica usando estilos. Para ver qué estilos ... | plt.style.available | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
No hay muchos pero podemos crear los nuestros. Para activar uno de ellos, usamos plt.style.use. ¡Aquí va el que uso yo! https://gist.github.com/Juanlu001/edb2bf7b583e7d56468a | #plt.style.use("ggplot") # Afecta a todos los plots | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
<div class="alert alert-warning">No he sido capaz de encontrar una manera fácil de volver a la apariencia por defecto en el notebook. A ver qué dicen los desarrolladores (https://github.com/ipython/ipython/issues/6707) ¡pero de momento si quieres volver a como estaba antes toca reiniciar el notebook!</div>
Para emplea... | with plt.style.context('ggplot'):
plt.plot(x, f(x))
plt.plot(x, 1 - f(x)) | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
Y hay otro tipo de personalización más loca todavía: | with plt.xkcd():
plt.plot(x, f(x))
plt.plot(x, 1 - f(x))
plt.xlabel("Eje x") | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
¡Nunca imitar a XKCD fue tan fácil! http://xkcd.com/353/
Otros tipo de gráficas
La función scatter muestra una nube de puntos, con posibilidad de variar también el tamaño y el color. | N = 100
x = np.random.randn(N)
y = np.random.randn(N)
plt.scatter(x, y) | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
Con s y c podemos modificar el tamaño y el color respectivamente. Para el color, a cada valor numérico se le asigna un color a través de un mapa de colores; ese mapa se puede cambiar con el argumento cmap. Esa correspondencia se puede visualizar llamando a la función colorbar. | s = np.abs(50 + 50 * np.random.randn(N))
c = np.random.randn(N)
plt.scatter(x, y, s=s, c=c, cmap=plt.cm.Blues)
plt.colorbar()
plt.scatter(x, y, s=s, c=c, cmap=plt.cm.Oranges)
plt.colorbar() | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
matplotlib trae por defecto muchos mapas de colores. En las SciPy Lecture Notes dan una lista de todos ellos (http://scipy-lectures.github.io/intro/matplotlib/matplotlib.html#colormaps)
La función contour se utiliza para visualizar las curvas de nivel de funciones de dos variables y está muy ligada a la función np.mes... | def f(x, y):
return x ** 2 - y ** 2
x = np.linspace(-2, 2)
y = np.linspace(-2, 2)
xx, yy = np.meshgrid(x, y)
zz = f(xx, yy)
plt.contour(xx, yy, zz)
plt.colorbar() | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
La función contourf es casi idéntica pero rellena el espacio entre niveles. Podemos especificar manualmente estos niveles usando el cuarto argumento: | plt.contourf(xx, yy, zz, np.linspace(-4, 4, 100))
plt.colorbar() | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
Para guardar las gráficas en archivos aparte podemos usar la función plt.savefig. matplotlib usará el tipo de archivo adecuado según la extensión que especifiquemos. Veremos esto con más detalle cuando hablemos de la interfaz orientada a objetos.
Varias figuras
Podemos crear figuras con varios sistemas de ejes, pasando... | x = np.linspace(-1, 7, 1000)
fig = plt.figure()
plt.subplot(211)
plt.plot(x, np.sin(x))
plt.grid(False)
plt.title("Función seno")
plt.subplot(212)
plt.plot(x, np.cos(x))
plt.grid(False)
plt.title("Función coseno") | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
<div class="alert alert-info">¿Cómo se ajusta el espacio entre gráficas para que no se solapen los textos? Buscamos en Google "plt.subplot adjust" en el primer resultado tenemos la respuesta http://stackoverflow.com/a/9827848</div>
Como hemos guardado la figura en una variable, puedo recuperarla más adelate y seguir e... | fig.tight_layout()
fig | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
<div class="alert alert-warning">Si queremos manipular la figura una vez hemos abandonado la celda donde la hemos definido, tendríamos que utilizar la interfaz orientada a objetos de matplotlib. Es un poco lioso porque algunas funciones cambian de nombre, así que en este curso no la vamos a ver. Si te interesa puedes v... | def frecuencias(f1=10.0, f2=100.0):
max_time = 0.5
times = np.linspace(0, max_time, 1000)
signal = np.sin(2 * np.pi * f1 * times) + np.sin(2 * np.pi * f2 * times)
with plt.style.context("ggplot"):
plt.plot(signal, label="Señal")
plt.xlabel("Tiempo ($t$)")
plt.title("Dos frecuenci... | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
Ejercicio
Representar las curvas de nivel de esta función:
$$g(x, y) = \cos{x} + \sin^2{y}$$
Para obtener este resultado: | def g(x, y):
return np.cos(x) + np.sin(y) ** 2
# Necesitamos muchos puntos en la malla, para que cuando se
# crucen las líneas no se vean irregularidades
x = np.linspace(-2, 3, 1000)
y = np.linspace(-2, 3, 1000)
xx, yy = np.meshgrid(x, y)
zz = g(xx, yy)
# Podemos ajustar el tamaño de la figura con figsize
fig =... | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
El truco final: componentes interactivos
No tenemos mucho tiempo pero vamos a ver algo interesante que se ha introducido hace poco en el notebook: componentes interactivos. | from IPython.html.widgets import interactive
interactive(frecuencias, f1=(10.0,200.0), f2=(10.0,200.0)) | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
Referencias
Guía de matplotlib para principiantes http://matplotlib.org/users/beginner.html
Tutorial de matplotlib en español http://pybonacci.org/tag/tutorial-matplotlib-pyplot/
Referencia rápida de matplotlib http://scipy-lectures.github.io/intro/matplotlib/matplotlib.html#quick-references | # Esta celda da el estilo al notebook
from IPython.core.display import HTML
css_file = './css/aeropython.css'
HTML(open(css_file, "r").read()) | 4-matplotlib.ipynb | eduardojvieira/Curso-Python-MEC-UCV | mit |
This example is an itinerary choice model built using the example
itinerary choice dataset included with Larch. We'll begin by loading
that example data. | from larch.data_warehouse import example_file
itin = pd.read_csv(example_file("arc"), index_col=['id_case','id_alt'])
d = larch.DataFrames(itin, ch='choice', crack=True, autoscale_weights=True) | book/example/legacy/301_itin_mnl.ipynb | jpn--/larch | gpl-3.0 |
Now let's make our model. We'll use a few variables to define our
linear-in-parameters utility function. | m = larch.Model(dataservice=d)
v = [
"timeperiod==2",
"timeperiod==3",
"timeperiod==4",
"timeperiod==5",
"timeperiod==6",
"timeperiod==7",
"timeperiod==8",
"timeperiod==9",
"carrier==2",
"carrier==3",
"carrier==4",
"carrier==5",
"equipment==2",
"fare_hy",
... | book/example/legacy/301_itin_mnl.ipynb | jpn--/larch | gpl-3.0 |
The larch.roles module defines a few convenient classes for declaring data and parameter.
One we will use here is PX which creates a linear-in-parameter term that represents one data
element (a column from our data, or an expression that can be evaluated on the data alone) multiplied
by a parameter with the same name. | from larch.roles import PX
m.utility_ca = sum(PX(i) for i in v)
m.choice_ca_var = 'choice' | book/example/legacy/301_itin_mnl.ipynb | jpn--/larch | gpl-3.0 |
Since we are estimating just an MNL model in this example, this is all we need to do to build
our model, and we're ready to go. To estimate the likelihood maximizing parameters, we give: | m.load_data()
m.maximize_loglike()
# TEST
result = _
from pytest import approx
assert result.loglike == approx(-777770.0688722526)
assert result.x['carrier==2'] == approx(0.11720047917232307)
assert result.logloss == approx(3.306873650593341) | book/example/legacy/301_itin_mnl.ipynb | jpn--/larch | gpl-3.0 |
TimeSide API
Timeside API is based on different core processing unit called processors :
Decoders (timeside.api.IDecoder) that enables to decode a giving audio source and split it up into frames for further processing
Analyzers (timeside.api.IAnalyzer) that provides some signal processing module to analyze incoming au... |
import timeside.core
from timeside.core import list_processors
list_processors(timeside.core.api.IDecoder) | docs/ipynb/01_Timeside_API.ipynb | Parisson/TimeSide | agpl-3.0 |
Analyzers | list_processors(timeside.core.api.IAnalyzer) | docs/ipynb/01_Timeside_API.ipynb | Parisson/TimeSide | agpl-3.0 |
Encoders | list_processors(timeside.core.api.IEncoder) | docs/ipynb/01_Timeside_API.ipynb | Parisson/TimeSide | agpl-3.0 |
Graphers | list_processors(timeside.core.api.IGrapher) | docs/ipynb/01_Timeside_API.ipynb | Parisson/TimeSide | agpl-3.0 |
Processors pipeline
All these processors can be chained to form a process pipeline.
Let first define a decoder that reads and decodes audio from a file | from timeside.core import get_processor
from timeside.core.tools.test_samples import samples
file_decoder = get_processor('file_decoder')(samples['C4_scale.wav']) | docs/ipynb/01_Timeside_API.ipynb | Parisson/TimeSide | agpl-3.0 |
And then some other processors | # analyzers
pitch = get_processor('aubio_pitch')()
level = get_processor('level')()
# Encoder
mp3 = get_processor('mp3_encoder')('/tmp/guitar.mp3', overwrite=True)
# Graphers
specgram = get_processor('spectrogram_lin')()
waveform = get_processor('waveform_simple')() | docs/ipynb/01_Timeside_API.ipynb | Parisson/TimeSide | agpl-3.0 |
Let's now define a process pipeline with all these processors and run it | pipe = (file_decoder | pitch | level | mp3 | specgram | waveform)
pipe.run() | docs/ipynb/01_Timeside_API.ipynb | Parisson/TimeSide | agpl-3.0 |
Analyzers results are available through the pipe: | pipe.results.keys() | docs/ipynb/01_Timeside_API.ipynb | Parisson/TimeSide | agpl-3.0 |
or from the analyzer: | pitch.results.keys()
pitch.results['aubio_pitch.pitch'].keys()
pitch.results['aubio_pitch.pitch'] | docs/ipynb/01_Timeside_API.ipynb | Parisson/TimeSide | agpl-3.0 |
Grapher result can also be display or save into a file | imshow(specgram.render(), origin='lower')
imshow(waveform.render(), origin='lower')
waveform.render('/tmp/waveform.png') | docs/ipynb/01_Timeside_API.ipynb | Parisson/TimeSide | agpl-3.0 |
And TimeSide can be embedded into a web page dynamically. For example, in Telemeta: | from IPython.display import HTML
HTML('<iframe width=1300 height=260 frameborder=0 scrolling=no marginheight=0 marginwidth=0 src=http://demo.telemeta.org/archives/items/6/player/1200x170></iframe>') | docs/ipynb/01_Timeside_API.ipynb | Parisson/TimeSide | agpl-3.0 |
# TensorFlow 编程概念
学习目标:
* 学习 TensorFlow 编程模型的基础知识,重点了解以下概念:
* 张量
* 指令
* 图
* 会话
* 构建一个简单的 TensorFlow 程序,使用该程序绘制一个默认图并创建一个运行该图的会话
注意:请仔细阅读本教程。TensorFlow 编程模型很可能与您遇到的其他模型不同,因此可能不如您期望的那样直观。
## 概念概览
TensorFlow 的名称源自张量,张量是任意维度的数组。借助 TensorFlow,您可以操控具有大量维度的张量。即便如此,在大多数情况下,您会使用以下一个或多个低维张量:
标量是零维数组(零阶张量)。例如... | import tensorflow as tf | ml/cc/prework/zh-CN/tensorflow_programming_concepts.ipynb | google/eng-edu | apache-2.0 |
请勿忘记执行前面的代码块(import 语句)。
其他常见的 import 语句包括:
import matplotlib.pyplot as plt # 数据集可视化。
import numpy as np # 低级数字 Python 库。
import pandas as pd # 较高级别的数字 Python 库。
TensorFlow 提供了一个默认图。不过,我们建议您明确创建自己的 Graph,以便跟踪状态(例如,您可能希望在每个单元格中使用一个不同的 Graph)。 | from __future__ import print_function
import tensorflow as tf
# Create a graph.
g = tf.Graph()
# Establish the graph as the "default" graph.
with g.as_default():
# Assemble a graph consisting of the following three operations:
# * Two tf.constant operations to create the operands.
# * One tf.add operation ... | ml/cc/prework/zh-CN/tensorflow_programming_concepts.ipynb | google/eng-edu | apache-2.0 |
## 练习:引入第三个运算数
修改上面的代码列表,以将三个整数(而不是两个)相加:
定义第三个标量整数常量 z,并为其分配一个值 4。
将 sum 与 z 相加,以得出一个新的和。
提示:请参阅有关 tf.add() 的 API 文档,了解有关其函数签名的更多详细信息。
重新运行修改后的代码块。该程序是否生成了正确的总和?
### 解决方案
点击下方,查看解决方案。 | # Create a graph.
g = tf.Graph()
# Establish our graph as the "default" graph.
with g.as_default():
# Assemble a graph consisting of three operations.
# (Creating a tensor is an operation.)
x = tf.constant(8, name="x_const")
y = tf.constant(5, name="y_const")
sum = tf.add(x, y, name="x_y_sum")
# Task 1... | ml/cc/prework/zh-CN/tensorflow_programming_concepts.ipynb | google/eng-edu | apache-2.0 |
2. Load the data | data = pd.read_csv("loan.csv", low_memory=False) | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
a. Data reduction for computation
From previous attempts to create a model matrix below and having the kernal crash, I'm going to reduce the data set size to compute better by selecting a random sample of 20% from the original dataset | # 5% of the data without replacement
data = data.sample(frac=0.05, replace=False, random_state=123) | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
3. Explore the data
visaully and descriptive methods | data.shape
data.head(n=5)
data.columns | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
The loan_status column is the target!
a. How many classes are there? | pd.unique(data['loan_status'].values.ravel())
print("Amount of Classes: ", len(pd.unique(data['loan_status'].values.ravel())))
len(pd.unique(data['zip_code'].values.ravel())) # want to make sure this was not too unique
len(pd.unique(data['url'].values.ravel())) # drop url
len(pd.unique(data['last_pymnt_d'].values.r... | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
b. Are there unique customers in the data or repeats? | len(pd.unique(data['member_id'].values.ravel())) == data.shape[0] | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
c. Drop some of the junk variables (id, member_id, ...)
Reasons: High Cardinality
pre-pre-processing 😃 | data = data.drop('id', 1) #
data = data.drop('member_id', 1)#
data = data.drop('url', 1)#
data = data.drop('purpose', 1)
data = data.drop('title', 1)#
data = data.drop('zip_code', 1)#
data = data.drop('emp_title', 1)#
data = data.drop('earliest_cr_line', 1)#
data = data.drop('term', 1)
data = data.drop('sub_grade', 1) ... | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
d. Exploratory Data Analysis: What is the distribution of the loan amount?
In general the loans amount was usually under $15,000 | data['loan_amnt'].plot(kind="hist", bins=10)
data['grade'].value_counts().plot(kind='bar')
data['emp_length'].value_counts().plot(kind='bar') | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
e. What is the distribution of target class?
Most of this dataset the loans are in a current state (in-payment?), or Fully paid off
Looks like a Poisson Distribution?! | data['loan_status'].value_counts().plot(kind='bar') | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
f. What are the numeric columns?
For pre-processing and scaling | data._get_numeric_data().columns
"There are {} numeric columns in the data set".format(len(data._get_numeric_data().columns) ) | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
g. What are the character columns?
For one-hot encoding into a model matrix | data.select_dtypes(include=['object']).columns
"There are {} Character columns in the data set (minus the target)".format(len(data.select_dtypes(include=['object']).columns) -1) | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
4. Pre-processing the data
a. Remove the target from the entire dataset | X = data.drop("loan_status", axis=1, inplace = False)
y = data.loan_status
y.head() | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
b. Transform the data into a model matrix with one-hot encoding
isolate the variables of char class | def model_matrix(df , columns):
dummified_cols = pd.get_dummies(df[columns])
df = df.drop(columns, axis = 1, inplace=False)
df_new = df.join(dummified_cols)
return df_new
X = model_matrix(X, ['grade', 'emp_length', 'home_ownership', 'verification_status',
'pymnt_plan', 'initial_list... | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
c. Scale the continuous variables use min max calculation | # impute rows with NaN with a 0 for now
X2 = X.fillna(value = 0)
X2.head()
from sklearn.preprocessing import MinMaxScaler
Scaler = MinMaxScaler()
X2[['loan_amnt', 'funded_amnt', 'funded_amnt_inv', 'int_rate',
'installment', 'annual_inc', 'dti', 'delinq_2yrs', 'inq_last_6mths',
'mths_since_last_delinq',... | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
d. Partition the data into train and testing | x_train, x_test, y_train, y_test = train_test_split(X2, y, test_size=.3, random_state=123)
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape) | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
5. Building the k Nearest Neighbor Classifier
experiment with different values for neighbors | # start out with the number of classes for neighbors
data_knn = KNeighborsClassifier(n_neighbors = 10, metric='euclidean')
data_knn
data_knn.fit(x_train, y_train) | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
a. predict on the test data using the knn model created above | data_knn.predict(x_test) | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
b. Evaluating the classifier model using R squared | # R-square from training and test data
rsquared_train = data_knn.score(x_train, y_train)
rsquared_test = data_knn.score(x_test, y_test)
print ('Training data R-squared:')
print(rsquared_train)
print ('Test data R-squared:')
print(rsquared_test) | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
c. Confusion Matrix | # confusion matrix
from sklearn.metrics import confusion_matrix
knn_confusion_matrix = confusion_matrix(y_true = y_test, y_pred = data_knn.predict(x_test))
print("The Confusion matrix:\n", knn_confusion_matrix)
# visualize the confusion matrix
# http://scikit-learn.org/stable/auto_examples/model_selection/plot_confus... | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
d. Classification Report | #Generate the classification report
from sklearn.metrics import classification_report
knn_classify_report = classification_report(y_true = y_test,
y_pred = data_knn.predict(x_test))
print(knn_classify_report) | post_data/final_project_jasmine_dumas.ipynb | jasdumas/jasdumas.github.io | mit |
semantic_version 2.4.2 : Python Package Index | list(apply_to_repos(repo_version,kwargs={'version_type':'patch'},repos=all_repos)) | rebuild_travis_on_repos.ipynb | rdhyee/nypl50 | apache-2.0 |
templates
template path?
variables to fill:
epub_title
encrypted_key
repo_name | def new_travis_template(repo, template, write_template=False):
"""
compute (and optionally write) .travis.yml based on the template and current metadata.yaml
"""
template_written = False
sh.cd(os.path.join(GITENBERG_DIR, repo))
metadata_path = os.path.join(GITENBERG_DIR, repo, "metadata.y... | rebuild_travis_on_repos.ipynb | rdhyee/nypl50 | apache-2.0 |
Divine Comedy
Divine-Comedy-Longfellow-s-Translation-Hell_1001 / /Users/raymondyee/C/src/gitenberg/Divine-Comedy-Longfellow-s-Translation-Hell_1001: there is a book.asciidoc but no .travis.yml
Let's do this by hand and document the process...
template | from second_folio import TRAVIS_TEMPLATE_URL
repo = "Divine-Comedy-Longfellow-s-Translation-Hell_1001"
title = "Divine Comedy, Longfellow's Translation, Hell"
slugify(title) | rebuild_travis_on_repos.ipynb | rdhyee/nypl50 | apache-2.0 |
Problem 1) Introduction to scikit-learn
At the most basic level, scikit-learn makes machine learning extremely easy within python. By way of example, here is a short piece of code that builds a complex, non-linear model to classify sources in the Iris data set that we learned about earlier:
from sklearn import datasets... | # execute dummy code here
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
iris = datasets.load_iris()
RFclf = RandomForestClassifier().fit(iris.data, iris.target) | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Generally speaking, the procedure for scikit-learn is uniform across all machine-learning algorithms. Models are accessed via the various modules (ensemble, SVM, neighbors, etc), with user-defined tuning parameters. The features (or data) for the models are stored in a 2D array, X, with rows representing individual sou... | # complete | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
You likely haven't encountered a scikit-learn Bunch before. It's functionality is essentially the same as a dictionary.
Problem 1b What are the keys of iris? | # complete | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Most importantly, iris contains data and target values. These are all you need for scikit-learn, though the feature and target names and description are useful.
Problem 1c What is the shape and content of the iris data? | print( # complete
# complete | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Problem 1d What is the shape and content of the iris target? | print( # complete
# complete | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Finally, as a baseline for the exercises that follow, we will now make a simple 2D plot showing the separation of the 3 classes in the iris dataset. This plot will serve as the reference for examining the quality of the clustering algorithms.
Problem 1e Make a scatter plot showing sepal length vs. sepal width for the ... | print(iris.feature_names) # shows that sepal length is first feature and sepal width is second feature
plt.scatter( # complete
# complete
# complete | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Problem 2) Unsupervised Machine Learning
Unsupervised machine learning, sometimes referred to as clustering or data mining, aims to group or classify sources in the multidimensional feature space. The "unsupervised" comes from the fact that there are no target labels provided to the algorithm, so the machine is asked t... | from sklearn.cluster import KMeans
Kcluster = KMeans( # complete
Kcluster.fit( # complete
plt.figure()
plt.scatter( # complete
# complete
# complete
# complete
# complete | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
With 3 clusters the algorithm does a good job of separating the three classes. However, without the a priori knowledge that there are 3 different types of iris, the 2 cluster solution would appear to be superior.
Problem 2b How do the results change if the 3 cluster model is called with n_init = 1 and init = 'random' ... | rs = 14
Kcluster1 = KMeans( # complete
# complete
# complete
# complete | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
A random aside that is not particularly relevant here
$k$-means evaluates the Euclidean distance between individual sources and cluster centers, thus, the magnitude of the individual features has a strong effect on the final clustering outcome.
Problem 2c Calculate the mean, standard deviation, min, and max of each fe... | print("feature\t\t\tmean\tstd\tmin\tmax")
for featnum, feat in enumerate(iris.feature_names):
print("{:s}\t{:.2f}\t{:.2f}\t{:.2f}\t{:.2f}".format(feat, np.mean(iris.data[:,featnum]),
np.std(iris.data[:,featnum]), np.min(iris.data[:,featnum]),
... | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Petal length has the largest range and standard deviation, thus, it will have the most "weight" when determining the $k$ clusters.
The truth is that the iris data set is fairly small and straightfoward. Nevertheless, we will now examine the clustering results after re-scaling the features. [Some algorithms, cough Supp... | from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit( # complete
# complete
# complete
# complete
# complete | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
These results are almost identical to those obtained without scaling. This is due to the simplicity of the iris data set.
How do I test the accuracy of my clusters?
Essentially - you don't. There are some methods that are available, but they essentially compare clusters to labeled samples, and if the samples are label... | # execute this cell
from sklearn.cluster import DBSCAN
dbs = DBSCAN(eps = 0.7, min_samples = 7)
dbs.fit(scaler.transform(iris.data)) # best to use re-scaled data since eps is in absolute units
dbs_outliers = dbs.labels_ == -1
plt.figure()
plt.scatter(iris.data[:,0], iris.data[:,1], c = dbs.labels_, s = 30, edgecolo... | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
I was unable to obtain 3 clusters with DBSCAN. While these results are, on the surface, worse than what we got with $k$-means, my suspicion is that the 4 features do not adequately separate the 3 classes. [See - a nayseyer can always make that argument.] This is not a problem for DBSCAN as an algorithm, but rather, evi... | from astroquery.sdss import SDSS # enables direct queries to the SDSS database
GALquery = """SELECT TOP 10000
p.dered_u - p.dered_g as ug, p.dered_g - p.dered_r as gr,
p.dered_g - p.dered_i as gi, p.dered_g - p.dered_z as gz,
p.petroRad_i, p.petroR50_i, p.deVAB_i
... | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
I have used my own domain knowledge to specifically choose features that may be useful when clustering galaxies. If you know a bit about SDSS and can think of other features that may be useful feel free to add them to the query.
One nice feature of astropy tables is that they can readily be turned into pandas DataFram... | # complete | Sessions/Session04/Day0/TooBriefMachLearn.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.