I am working on the file "T-Test vs Permutation Test, Sunday (Aug 27) .ipynb".
The first task is:
Permutation Test
Second, we do a permutation test (using scikit-learn).
huh and wat? Permutation test = resampling = get all the data points from datasets A and B and exchange the labels on each data point (e.g. say a data point from dataset A is 1.1, this data point could be labeled as being in dataset B or A when resampled) many many times.
This will generate a distribution of randomized labels on the data points. It's like exploring the "What if the data points weren't labeled as belonging to dataset A or B?"
Another frame of reference in understanding just what in the hell we're doing is through the combinations formula that we slept through in high school:
$$C(n,r) = C(25,10)$$
$$ $$
$$= \frac{25!}{10!(25-10)!}$$
$$ $$
$$=3,268,760$$
Given that dataset A has a N = 10 and dataset B has a N = 15, we have a total n (objects) = 10 + 15 = 25 , and a r (sample) of 10 , which means that we choose 10 of them to be label A. Watever is left is gonna be label B. The order doesn't matter, thus the combinations formula, and not the permutation formula.
But I only have a macbook, and not some Watson so ima just run 1,000 permutations and not 3 million and change permutations. They don't make much difference, and if you don't believe me, bug Ning, not me.
Note. As we increase group sizes: from only 2 groups to 3 or more, we don't necessarily increase the number of permutations a lot (like from 1,000 to 1,000,000) only for approximation. Rather, we increase the number or permutations based on both computation power and the log(combination formula) * 100.
The significance of all this rambling is that we have one assumption - independent sampling, which is the foundation of modern maths.
Unlike most parametric tests (which includes the high school quarterback t-test that we all admired but secretly wanted to ditch in a river bank), we don't assume any gender, sexual orientation, and most importantly normalization and equal variance.
So how do we do dis?
<br />First, we have to do some preprocessing.
1. p.s. Yo. So, a...
Can you write Python code for it?

# preprocessing
label1 = np.zeros(dataset_A.shape)
label2 = np.ones(dataset_B.shape)
data = np.concatenate([dataset_A,dataset_B])
label = np.concatenate([label1,label2])

# https://youtu.be/KQ6zr6kCPj8?t=3m39s
idx = np.arange(len(data))
for ii in range(100):
    shuffle(idx)

data = data[idx]
label = label[idx]

#let's define the machine learning model and the cross validation method
cv = StratifiedKFold(n_splits = 5, shuffle = True, random_state = 12345)
clf = LogisticRegression(random_state = 12345)
score, permutation_score, pval = permutation_test_score(clf, data.reshape(-1,1), label,
                                                        n_permutations = 1000, cv = cv,
                                                        random_state = 12345, scoring = 'roc_auc')

fig,ax = plt.subplots()
ax.hist(permutation_score, label = 'randomized scores', color = "red")
ax.axvline(score, label = 'true score : %.2f , pval : %.3f'%(score, pval))
ax.legend()
ax.set(title = "Histogram of Permutated Datasets A and B", xlabel = "Score",
       ylabel = "Count / Frequency of Permutation Scores")


I am working on the file "T-Test vs Permutation Test, Sunday (Aug 27) .ipynb".
The first task is:
T-test
Simulating datasets A and B, and making sure that they match teh criteria:

p value be close to like .05, like reallllllllllllly #fracking close (less than .001 difference)
all the data points in dataset A are positive
all the data points in dataset B are positive

Criteria 2 and 3 make sure that we don't have a normal distribution. Explanation: when mean is 1.24 and the standard deviation is 1.75, how. do. they. not. have. some. negative. data. points. amirite?
The reason why we did this is to make sure that we have data that violate the assumptions of doing t tests.
Reason why Ning was forced to use blue and yellow instead of blue and red like a normal human being:
Can you write Python code for it?

import IPython.display as dis

dis.YouTubeVideo("Jwjj5gowpLA",start=41)


I am working on the file "T1/.ipynb_checkpoints/test_external_library-checkpoint.ipynb".
The first task is:
Links
Install a jar file directly from an addressable location
http://datascience.ibm.com/docs/content/analyze-data/Package-Manager.html#installajarfiledirectly
attach a JAR library
https://forums.databricks.com/questions/706/how-can-i-attach-a-jar-library-to-the-cluster-that.html
Otro
https://forum.processing.org/two/discussion/7479/how-to-import-a-java-library
[_No?] How can i call and run another .pde file
https://forum.processing.org/two/discussion/4646/how-can-i-call-and-run-another-pde-file
Can you write Python code for it?

%%processing

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

class GOL {

  int w = 8;
  int columns, rows;

  // Game of life board
  int[][] board;

  GOL() {
    // Initialize rows, columns and set-up arrays
    columns = width/w;
    rows = height/w;
    board = new int[columns][rows];
    //next = new int[columns][rows];
    // Call function to fill array with random values 0 or 1
    init();
  }

  void init() {
    for (int i =1;i < columns-1;i++) {
      for (int j =1;j < rows-1;j++) {
        board[i][j] = int(random(2));
      }
    }
  }

  // The process of creating the new generation
  void generate() {

    int[][] next = new int[columns][rows];

    // Loop through every spot in our 2D array and check spots neighbors
    for (int x = 1; x < columns-1; x++) {
      for (int y = 1; y < rows-1; y++) {

        // Add up all the states in a 3x3 surrounding grid
        int neighbors = 0;
        for (int i = -1; i <= 1; i++) {
          for (int j = -1; j <= 1; j++) {
            neighbors += board[x+i][y+j];
          }
        }

        // A little trick to subtract the current cell's state since
        // we added it in the above loop
        neighbors -= board[x][y];

        // Rules of Life
        max_neighbors = 3
        min_neighbors = 2

        if      ((board[x][y] == 1) && (neighbors <  min_neighbors)) next[x][y] = 0;           // Loneliness
        else if ((board[x][y] == 1) && (neighbors >  max_neighbors)) next[x][y] = 0;           // Overpopulation
        else if ((board[x][y] == 0) && (neighbors == 3)) next[x][y] = 1;           // Reproduction
        else                                            next[x][y] = board[x][y];  // Stasis
      }
    }

    // Next is now our board
    board = next;
  }

  // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0'
  void display() {
    for ( int i = 0; i < columns;i++) {
      for ( int j = 0; j < rows;j++) {
        if ((board[i][j] == 1)) fill(0);
        else fill(255);
  ...


I am working on the file "T1/Tarea1.ipynb".
The first task is:
A diferencia de Ride Regression, Lasso permite anular coeficientes para cierto valores de $\lambda$. A esto se le denomina "soft thresholding". Se puede observar que para coeficientes de $\lambda$ pequeños se tiene una gran varianza entre los coeficientes de la regresión lineal. A medida que se aumenta $\lambda$, comienza a disminuir la la varianza entre los coeficientes, pero también la gran mayorìa de ellos comienza a desaparecer. Esto permite reducir la cantidad de atributos requeridos para realizar la estimación, disminuyendo las probabilidades de overfitting si se selecciona un valor de $\lambda$ adecuado.
c) Escogiendo uno de los dos métodos regularizadores anteriores, especificando el porqué, construya un
gráfico que muestre el error de entrenamiento y el error de pruebas como función del parámetro de
regularización. Discuta lo que observa.
Se utiliza Lasso para aprovechar la oportunidad de reducir ciertos coeficientes a 0 (los que están más cerca del valor $\lambda = 10^{-3}$) en lugar del suavizado que realiza Ridge Regression.
Can you write Python code for it?

Xtest = X2[np.logical_not(istrain)]
ytest = y[np.logical_not(istrain)]
alphas_ = np.logspace(0,-3,base=10)
coefs = []
model = Lasso(fit_intercept=True)
mse_test = []
mse_train = []
for a in alphas_:
    model.set_params(alpha=a)
    model.fit(Xtrain, ytrain)
    yhat_train = model.predict(Xtrain)
    yhat_test = model.predict(Xtest)
    mse_train.append(np.mean(np.power(yhat_train - ytrain, 2)))
    mse_test.append(np.mean(np.power(yhat_test - ytest, 2)))
ax = plt.gca()
ax.plot(alphas_,mse_train,label='train error ridge')
ax.plot(alphas_,mse_test,label='test error ridge')
plt.legend(loc=1)
ax.set_xscale('log')
ax.set_xlim(ax.get_xlim()[::-1])
plt.show()


I am working on the file "T1/Tarea1.ipynb".
The first task is:
Considerando la distribución $t_{(N - k - 1)}$ donde $k$ son los atributos filtrados. Tenemos entonces:
Can you write Python code for it?


I am working on the file "T1/Tarea1.ipynb".
The first task is:
Esto implica que los atributos seleccionados son similarmente importantes en la predicción del precio.
g) Estime el error de predicción del modelo usando validación cruzada con un número de folds igual a K
= 5 y K = 10. Recuerde que para que la estimación sea razonable, en cada configuración (fold) deberá
reajustar los pesos del modelo. Mida el error real del modelo sobre el conjunto de pruebas, compare y
concluya.
Asumiendo que se debe utilizar el modelo con la selección de atributos seleccionada anteriormente, se tiene:
Can you write Python code for it?

# Using the test sample conaining only selected predictors
yhat_test = linreg.predict(Xtest) # Predict
mse_test = np.mean(np.power(yhat_test - ytest, 2))

# Form the matrix from the RAW set
Xm = Xtrain.as_matrix()
ym = ytrain.as_matrix()

from sklearn.model_selection import KFold

def doKFold(Xt, yt, K):
    # Using K = 5
    kf = KFold(n_splits=K)
    mse_cv = 0
    for train_indeces, test_indeces in kf.split(Xm):
        # Select only the most relevant attributes
        # In the previous section we chose attributes with index 2, 9 and 10
        # as the most relevant.
        Xtrain_k = Xtrain.iloc[train_indeces].as_matrix()
        Xtest_k = Xtrain.iloc[test_indeces].as_matrix()
        ytrain_k = ytrain.iloc[train_indeces].as_matrix()
        ytest_k = ytrain.iloc[test_indeces].as_matrix()

        # Perform linear regression for this fold
        linreg = lm.LinearRegression(fit_intercept = False)
        linreg.fit(Xtrain_k, ytrain_k)

        # Get the yhat for our test set
        yhat_val = linreg.predict(Xtest_k)

        # Calculate the MSE for the error
        mse_fold = np.mean(np.power(yhat_val - ytest_k, 2))
        mse_cv += mse_fold
    mse_cv = mse_cv / K
    return mse_cv

{"mse5": doKFold(Xtrain, ytrain, 5), "mse10": doKFold(Xtrain, ytrain, 10), "mse_test": mse_test}


I am working on the file "T1/Tarea1.ipynb".
The first task is:
Lo cual indica que son relevantes aquellas variables con z-score $|z| > 1.96$. Esto es, las variables sqft_living y sqft_above.
Se puede observar que de todos los atributos existentes, la mayoría tiene z-scores marginales en comparaciñon a los atributos sqft_living, sqft_above y sqft_basement, por lo que su aporte es también marginal en la predicción del precio de la vivienda.
f) Proponga un método para corregir lo observado (Hint: inspírese en los métodos de feature engineering
de las siguiente secciones). Verifíquelo mediante los Z-score presentados en la pregunta e).
Basandonos en la sección anterior, podemos eliminar todos los atributos con z-score irrelevantes para la regresión lineal. Si consideramos a $z \rel 0.1$ como el umbral de selección, tenemos como resultado:
Can you write Python code for it?

import sklearn.linear_model as lm
X2 = df_scaled.iloc[:, 1:]
X2 = X2.iloc[:,[2, 9, 10]] #use .ix instead, in older pandas version
print(list(X2))
N = X2.shape[0]
X2.insert(X2.shape[1], 'intercept', np.ones(N))
y2 = df_scaled['price']
#mascara estatica con el 70% de los datos
mascara = np.zeros(len(X2))
limit = int(len(X2)*0.7)
mascara[:limit] = 1
istrain2 = mascara== 1
Xtrain2 = X2[istrain2]
ytrain2 = y2[istrain2]
Xtest2 = X2[np.logical_not(istrain2)]
ytest2 = y2[np.logical_not(istrain2)]
linreg2 = lm.LinearRegression(fit_intercept = False)
linreg2.fit(Xtrain2, ytrain2)
betas2 = linreg2.coef_
col2 = list(X2)
pd.DataFrame([betas2], columns=col2)
zscores2 = stats.zscore(betas2)
pd.DataFrame([zscores2], columns=col2)


I am working on the file "T1/Tarea1.ipynb".
The first task is:
Los z-scores de los coeficientes corresponde a:
Can you write Python code for it?

# Obtención de los z-score
from scipy import stats
zscores = stats.zscore(betas)
zT = pd.DataFrame([zscores], columns=col)
zT


I am working on the file "T1/Tarea1.ipynb".
The first task is:
Para poder entender el gráfico generado, es necesario entender como funciona Ridge Regression. Este algoritmo penaliza los coeficientes de la regresión lineal a través de un coeficiente denominado $\lambda$. Esto permite "suavizar" la diferencia de valor entre los coeficientes al imponer restricciones sobre su valor máximo.
Cuando se elije un valor de $\lambda$ pequeño, se puede estar en presencia de underfitting, mientras que si es muy pequeño se sufre de overfitting. Por tanto, es importante elegir valores de $\lambda$ adecuados.
Se puede observar en el gráfico que valores de $\lambda$ cercanos a 0 (o bien, cercanos a $10^1$) generan coeficientes muy dispersos con alta varianza y bias. A medida que se aumenta el valor de $\lambda$ los valores de los coeficientes van disminuyendo proporcionalmente de forma que se genera una disminución en la varianza y el bias. Alrededor de un coeficiente de $10^6$, se tiene que los coeficientes comienzan a ser muy similares entre sí mismos, produciendo el efecto de overtfitting que disminuye la capacidad de generalización del modelo entrenado.
Se puede confirmar además que los atributos relevantes corresponden a sqft_lot (Metros cuadrados de la vivienda), grade y bathrooms, debido a que son los que más aportan a la varianza.
b) Ajuste un modelo lineal utilizando el método “Lasso”, es decir, regularizando con la norma $\ell_1$. Utilice
valores del parámetro de regularización $\lambda$ en el rango $[10^0, 10^{-3}]$. Para obtener el código, modifique
las líneas 7 y 9 del ejemplo anterior. Construya un gráfico que muestre los coeficientes obtenidos
como función del parámetro de regularización. Describa lo que observa. ¿Es más efectivo Lasso para
seleccionar atributos?
Can you write Python code for it?

from sklearn.linear_model import Lasso
alphas_ = np.logspace(0,-3,base=10)
X2 = X.drop('intercept', axis=1,inplace=False)
Xtrain = X2[istrain]
ytrain = y[istrain]
names_regressors = X2.columns
coefs = []
model = Lasso(fit_intercept=True)
for a in alphas_:
    model.set_params(alpha=a)
    model.fit(Xtrain, ytrain)
    coefs.append(model.coef_)
ax = plt.gca()
for y_arr, label in zip(np.squeeze(coefs).T, names_regressors):
    plt.plot(alphas_, y_arr, label=label)
plt.legend()
ax.set_xscale('log')
ax.set_xlim(ax.get_xlim()[::-1]) # reverse axis
plt.title('Regularization Path LASSO')
plt.axis('tight')
plt.legend(loc=2)
plt.show()


I am working on the file "T1/Tarea1.ipynb".
The first task is:
Por lo tanto, el parámetro ideal para Lasso corresponde a $10^{-3}$ que corresponde al intercepto entre el error de entrenamiento y el error de testing de menor valor, coincidiendo con el gráfico anterior.
4.- Drift
En esta sección se presentarán dos muestras del dataframe utilizado en la actividades anteriores, donde cada
una de estas tiene una propiedad distinta ya que son muestreadas en función del valor a predecir (logaritmo
del precio de la casa). Por una parte se tiene una pequeña muestra A, la cual es extraída directamente de
los datos con los que se trabaja (manteniendo la distribución de esta) y la muestra B, es generada con el
propósito de que en cada intervalo del rango de valores haya la misma cantidad de datos aproximadamente
(simulando una distribución uniforme). El objetivo es familiarizarse con el concepto de Transfer Learning.
En el siguiente código se generan las dos muestras con las que se trabajará.
Can you write Python code for it?

import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn import linear_model

df = pd.read_csv("kc_house_data.csv")
df.drop(['id','date','zipcode',],axis=1,inplace=True)
scaler = StandardScaler()
df_scaled = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)
df_scaled['price'] = np.log(df['price'])

df_A = df_scaled.sample(1000,random_state=11)
frames = []
valor = df_scaled.price
length = 0.3
for z in np.arange(int(np.min(valor)),int(np.max(valor))+1,length):
    #un maximo de 100 datos por intervalo
    aux = df_scaled[(df_scaled.price >= z) & (df_scaled.price < z+length)].head(100)
    frames.append(aux)
df_B = pd.concat(frames).sample(1000,random_state=11) #crea el dataframe


I am working on the file "T1/Tarea1.ipynb".
The first task is:
Se puede notar que a medida que se disminuye el valor del parámetro $\lambda$, comienzan a aumentar tanto el error de entrenamiento como el error de prueba. Esto ocurre debido a que en Lasso muchos de los coeficientes se anulan, evitando que sus atributos asociados aporten información a la predicción. Entre $10^0$ y $10^{-1}$ se llega a un punto crítico en donde se obtiene un error máximo debido a la elminicación de la mayoría de los atributos.
d) Estime el valor del parámetro de regularización en alguno de los modelos anteriores haciendo uso de
la técnica validación cruzada.
Utilizando Lasso, se tiene que:
Can you write Python code for it?

def MSE(y,yhat): return np.mean(np.power(y-yhat,2))
Xm = Xtrain.as_matrix()
ym = ytrain.as_matrix()

from sklearn.model_selection import KFold

kf = KFold(n_splits=10)
best_cv_mse = float("inf")
alphas_ = np.logspace(0,-3,base=10)
model = Lasso(fit_intercept=True)

for a in alphas_:
    model.set_params(alpha=a)
    mse_list_k10 = [MSE(model.fit(Xm[train], ym[train]).predict(Xm[val]), ym[val]) for train, val in kf.split(Xm)]
    if np.mean(mse_list_k10) < best_cv_mse:
        best_cv_mse = np.mean(mse_list_k10)
        best_alpha = a
        print("BEST PARAMETER=%f, MSE(CV)=%f"%(best_alpha,best_cv_mse))


I am working on the file "T1/Tarea1.ipynb".
The first task is:
Se puede observar que los errores obtenidos para cross validation con $K = 5$ y $K = 10$ son muy similares y tienen un valor aproximado de $0.0647$. El error obtenido del conjunto de prueba sobre los datos sin realización de selección de atributos corresponde a $0.065$ que es prácticamente el mismo.
Esto sucede debido a la gran cantidad de datos disponibles para el entrenamiento. Eligiendo todo el subconjunto de atributos, tenemos que $p$ es al menos 3 ordenes de magnitud menor que $N$, lo que permite tener una gran precisión utilizando cualquier método de validación.
h) Mida los errores de predicción para cada dato de entrenamiento. Utilizando un “quantile-quantile plot”
determine si es razonable la hipótesis de normalidad sobre los residuos del modelo.
Can you write Python code for it?

import scipy.stats as stats
import matplotlib.pyplot as plt
ypredicted = linreg.predict(Xtrain)
error = ypredicted - ytrain.as_matrix()
stats.probplot(error, dist='norm', plot=plt)
plt.ylabel('Error')
plt.title('Quantile-Quantile Plot')
plt.show()


I am working on the file "T1/Tarea1.ipynb".
The first task is:
a) Cree el conjunto de entrenamiento y otro de validación para trabajar cada muestra mediante la técnica
de hold out validation.
Can you write Python code for it?

X_A = df_A.iloc[:,1:].values
y_A = df_A.price
X_B = df_B.iloc[:,1:].values
y_B = df_B.price
from sklearn.model_selection import train_test_split
Xtrain_A,Xval_A,ytrain_A,yval_A = train_test_split(X_A, y_A, test_size=0.3, random_state=42)
Xtrain_B,Xval_B,ytrain_B,yval_B = train_test_split(X_B, y_B, test_size=0.3, random_state=42)


I am working on the file "T1000/Notebooks/Controls/DiscreteControlExamples.ipynb".
The first task is:
Check the ramp steady state error like we did before
Can you write Python code for it?

t, y = control.step_response(unit_step - CL * unit_step)
plt.plot(t, y)
plt.xlabel('Time')
plt.title('ramp steady state error')


I am working on the file "T1000/Notebooks/Controls/DiscreteControlExamples.ipynb".
The first task is:
Create the feedback system and find the closed loop transfer function.
Can you write Python code for it?


I am working on the file "T1000/Notebooks/Controls/DiscreteControlExamples.ipynb".
The first task is:
Discrete control #4: Discretize with the matched method Brian Douglas
Can you write Python code for it?

# No zeroes, poles at -1, -2, gain of 3
b, a = control.matlab.zpk2tf([], [-1, -2], 3)
print(b)
print(a) # Confirms this is same transfer function as written before.

zero_frequency_DC_gain = control.dcgain(control.tf([3], [1,3,2]))
print(zero_frequency_DC_gain)

Gz = control.TransferFunction([1], [1, -0.5032, 0.04978], 1)
print(Gz)

G = control.tf([3,], [1, 3, 2])
print(G)

mag, phase, omega = control.matlab.bode(G, Gz)

t, y = control.step_response(G)
plt.plot(t, y)
plt.xlabel('Time')
plt.title('Step Response')

t, y = control.step_response(Gz)
plt.plot(t, y)
plt.xlabel('Time')
plt.title('Step Response')

t, y = control.step_response(G)
tz, yz = control.step_response(Gz)
plt.plot(t, y)
plt.step(tz, yz)
plt.xlabel('Time')
plt.title('Step Response')

Gz_proper = control.tf([1, 1], [1, -0.5032, -.04978], 1)
mag, phase, omega = control.matlab.bode(G, Gz_proper)

mag, phase, omega = control.matlab.bode(G, Gz, Gz_proper)

t, y = control.step_response(G)
tz, yz = control.step_response(Gz)
tzp, yzp = control.step_response(Gz_proper)
plt.plot(t, y)
plt.step(tz, yz)
plt.step(tzp, yzp)
plt.xlabel('Time')
plt.title('Step Response')

import sympy
z = sympy.Symbol('z')
print( ((z - 0.3679) * (z -0.1353)).as_poly() )

print(G)
print(G.zero())
print(G.pole())
Gz_matched = control.matlab.c2d(G, 1.0, 'matched')
print(Gz_matched)

mag, phase, omega = control.matlab.bode(G, Gz_matched)

t, y = control.step_response(G)
tm, ym = control.step_response(Gz_matched)
plt.plot(t, y)
plt.step(tm, ym)
plt.xlabel('Time')
plt.title('Step Response')

Gz_matched_hand = control.tf([0.4099, 0.4099], [1, -0.5032, 0.04977], 1)
print(Gz_matched_hand)
mag, phase, omega = control.matlab.bode(G, Gz_matched_hand)

t, y = control.step_response(G)
tm, ym = control.step_response(Gz_matched_hand)
plt.plot(t, y)
plt.step(tm, ym)
plt.xlabel('Time')
plt.title('Step Response')


I am working on the file "T1000/Notebooks/Controls/DiscreteControlExamples.ipynb".
The first task is:
Discretization is done with the function c2d
Can you write Python code for it?

help(control.matlab.c2d)

Gz = control.matlab.c2d(G, Ts, 'zoh')
print(Gz)
Gf = control.matlab.c2d(G, Ts, 'foh')
print(Gf)
Gi = control.matlab.c2d(G, Ts, 'impulse')
print(Gi)
Gt = control.matlab.c2d(G, Ts, 'tustin')
print(Gt)
Gm = control.matlab.c2d(G, Ts, 'matched')
print(Gm)
Gb = control.matlab.c2d(G, Ts, 'bilinear')
print(Gb)

mag, phase, omega = control.matlab.bode(G, Gz, Gf, Gi, Gt, Gm)
plt.legend()

mag, phase, omega = control.bode_plot([G, Gz, Gf, Gi, Gt, Gm])
plt.gca().legend(('Continuous', 'ZOH', 'FOH', 'Impulse', 'Tustin', 'Matched'))

y, T = control.matlab.step(G)
yz, Tz = control.matlab.step(Gz)
yf, Tf = control.matlab.step(Gf)
yi, Ti = control.matlab.step(Gi)
yt, Tt = control.matlab.step(Gt)
ym, Tm = control.matlab.step(Gm)

plt.plot(T, y, 'r')
plt.plot(Tz, yz, 'b')
plt.plot(Tf, yf, 'g')
plt.plot(Ti, yi, 'y')
plt.plot(Tt, yt, 'p')
plt.plot(Tm, ym, 'o')
plt.show()

print(Gi)

T, y = control.impulse_response(G)
Ti, yi = control.impulse_response(Gi)
Tz, yz = control.impulse_response(Gz)
plt.plot(T, y, 'r')
plt.plot(Tz, yz, 'b')
plt.plot(Ti, yi, 'g')
plt.xlim([0.0, 0.7])
plt.show()


I am working on the file "T1000/Notebooks/Controls/DiscreteControlExamples.ipynb".
The first task is:
Steady State Error is 0.2 (20%), greater than say requirement of Steady State Error being < 2% for ramp input.
Since it's first-order, open loop, it has infinite phase margin, no amount of phase lag will make system unstable.
Create the controller transfer function.
Can you write Python code for it?


I am working on the file "T1000/Notebooks/Controls/DiscreteControlExamples.ipynb".
The first task is:
There's a jump in error in the beginning, and then goes to 2 percent error.
To find phase margin we look at Bode plot of open loop system.
Can you write Python code for it?

mag, phase, omega = control.matlab.bode(C*G)

# wpc = crossover frequency, wgc = gain crossover frequency (where gain crosses 1)
gain_margin, phase_margin, wpc, wgc = control.margin(C*G)
print(gain_margin)
print(phase_margin) # 50.5 phase margin, beating a 48 degree phase margin
print(wpc)
print(wgc)


I am working on the file "T1000/Notebooks/Controls/DiscreteControlExamples.ipynb".
The first task is:
We can convert to a discrete transfer function in the z domain using c2d()
Default conversion method is Zero Order Hold (ZOH)
Can you write Python code for it?

# c2d returns sysd - Discrete time system, with sampling rate Ts
Cz = control.matlab.c2d(C, 0.1) # default 'zoh'
print(Cz)

mag, phase, omega = control.matlab.bode(C, Cz)

gain_margin, phase_margin, wpc, wgc = control.margin(Cz)
print(gain_margin)
print(phase_margin)
print(wpc)
print(wgc)


I am working on the file "T1000/Notebooks/Controls/DiscreteControlExamples.ipynb".
The first task is:
cf. Discrete control #3: Designing for the zero-order hold Brian Douglas
Can you write Python code for it?

C = control.tf([500, 50], [100, 1, 0])
print(C)

Ct = control.matlab.c2d(C, 0.2, 'tustin')
print(Ct)


I am working on the file "T1000/Notebooks/Numerical/RungeKuttaMethods.ipynb".
The first task is:
An example for unit tests
cf. Runge-Kutta method from OK state
Depending on one's notation, for "stage" $s=4$, or $m=4$, then for
$\alpha_2 = \frac{1}{2}, \, \alpha_3 = \frac{1}{2}, \, \alpha_4 = 1 \equiv \
c_2 = \frac{1}{2}, \, c_3 = \frac{1}{2}, \, c_4 = 1$
$\beta_{21} = \frac{1}{2}, \, \beta_{31} = 0, \, \beta_{32} = \frac{1}{2}, \, \beta_{41} = \beta_{42} = 0, \, \beta_{43} = 1 \equiv \
a_{21} = \frac{1}{2}, \, a_{31} =0, \, a_{32} = \frac{1}{2}, \, a_{41} = a_{42} = 0, \, a_{43} = 1$
$c_1 = c_4 = \frac{1}{6}, \, c_2 = c_3 = \frac{1}{3} \equiv b_1 = b_4 = \frac{1}{6}, \, b_2 = b_3 = \frac{1}{3}$
(authors having different notation sucks).
Can you write Python code for it?


I am working on the file "T1000/Notebooks/Numerical/RungeKuttaMethods.ipynb".
The first task is:
Coefficients for dense output
cf. https://math.stackexchange.com/questions/2947231/how-can-i-derive-the-dense-output-of-ode45
Can you write Python code for it?

bs = DOPRI5Coefficients.b_coefficients
ds = DOPRI5Coefficients.dense_output_coefficients
cstars = DOPRI5Coefficients.cstar_coefficients

-2 * (1 + 4 * bs.get_ith_element(1) - 4 * cstars.get_ith_element(1))


I am working on the file "T1000/Notebooks/Numerical/RungeKuttaMethods.ipynb".
The first task is:
Consider example
$\begin{cases}
    & y'  =y -x^2 + 1\
    & y(0) = 0.5
\end{cases}$
Its exact solution is
$ y= y(x) = x^2 + 2x + 1 - \frac{1}{2} \exp{x}$
Can you write Python code for it?

def derivative(x, y):
    return y - np.power(x, 2) + 1.0

def exact_y(x):
    return np.power(x, 2) + 2.0 * x + 1.0 - 0.5 * np.exp(x)


I am working on the file "T1000/Notebooks/Numerical/RungeKuttaMethods.ipynb".
The first task is:
Step 2 $\quad \, x_2 = 1$
Can you write Python code for it?

ks1 = rk4._calculate_k_coefficients(y1, 0.5, 0.5, derivative)
print(ks1)
y2 = rk4.calculate_next_step(y1, 0.5, 0.5, derivative)
print(y2)
print(exact_y(1.))


I am working on the file "T1000/Notebooks/Numerical/RungeKuttaMethods.ipynb".
The first task is:
cf. Examples for solve_ivp
Can you write Python code for it?

def exponential_decay(t, y):
    return -0.5 * y

sol = solve_ivp(exponential_decay, [0, 10], [2, 4, 8])
print("t\n", sol.t)
print("y\n", sol.y)


I am working on the file "T1000/Notebooks/Numerical/RungeKuttaMethods.ipynb".
The first task is:
cf. pp. 916 17.2.2 Dense Output, Ch. 17. Integration of Ordinary Differential Equations of Numerical Recipes, 3rd Ed., and
Can you write Python code for it?

hermite_interpolation = (1 - theta) * y_n + theta * y_np1 + \
  theta * (theta - 1) * ((1 - 2 * theta) * (y_np1 - y_n) + (theta - 1) * h *f_n + theta * h * f_np1)
pprint(hermite_interpolation)

pprint(hermite_interpolation.expand())
pprint(sympy.collect(hermite_interpolation.expand(), y_n, evaluate=False)[y_n])
pprint(sympy.collect(hermite_interpolation.expand(), y_np1, evaluate=False)[y_np1])
pprint(sympy.collect(hermite_interpolation.expand(), f_n, evaluate=False)[f_n])
pprint(sympy.collect(hermite_interpolation.expand(), f_np1, evaluate=False)[f_np1])


I am working on the file "T1000/documents/NIST_Chemistry_Webbook.ipynb".
The first task is:
Scrape Species List (Download) page
cf. https://www.digitalocean.com/community/tutorials/how-to-scrape-web-pages-with-beautiful-soup-and-python-3
Can you write Python code for it?

species_list_columns = species_list.ScrapeWebpage.species_list_columns()

species_list_columns

ready_data = species_list.ReadAndClean.to_database_ready_data(species_list_columns)

ready_data[:5]


I am working on the file "T1000/documents/NIST_Chemistry_Webbook.ipynb".
The first task is:
Scratch work
Can you write Python code for it?

DataPaths().raw()

list(DataPaths().raw().glob('**/*'))

species_txt_path = \
    next(path for path in list(DataPaths().raw().glob('**/*')) if "species" in str(path) and ".txt" in str(path))

f = open(species_txt_path, 'r', newline='')
csv_reader = csv.reader(f, delimiter="\t")

csv_as_list = list(csv_reader)
f.close()

for row in csv_as_list:
    if len(row) != 3:
        print(len(row))


I am working on the file "T1000/documents/NIST_Chemistry_Webbook.ipynb".
The first task is:
Scratchwork
Can you write Python code for it?

NISTChemistryWebbookPaths.species_list()

page = requests.get(NISTChemistryWebbookPaths.species_list())

# Use Python's built-in html.parser
soup = BeautifulSoup(page.text, 'html.parser')

soup.find(id="main").find('ul').find_all('li')[0].text


I am working on the file "T1000/documents/NIST_Chemistry_Webbook.ipynb".
The first task is:
Setup to use Python libraries/modules
Can you write Python code for it?

from T1000.utilities.configure_paths import (DataPaths, NISTChemistryWebbookPaths)
from T1000.NistChemBook import species_list

from bs4 import BeautifulSoup

import csv
import requests


I am working on the file "T1000/documents/NIST_Chemistry_Webbook.ipynb".
The first task is:
Species List
Download the Species list.
https://webbook.nist.gov/chemistry/download/
Can you write Python code for it?

csv_as_list = species_list.ReadAndClean.to_list()

csv_as_list[:5]


I am working on the file "T1000/playground/CoolProp-examples.ipynb".
The first task is:
Examples from CoolProp's Examples
cf. www.coolprop.org/coolprop/examples.html
Can you write Python code for it?

# Print some information on the currently used version of coolprop
print(CoolProp.__version__, CoolProp.__gitrevision__)

# Density of carbon dioxide at 100 bar and 25 C
PropsSI('D', 'T', 298.15, 'P', 100e5, 'CO2')

# Saturated vapor enthalpy [J/kg] of R134a at 25 C
PropsSI('H', 'T', 298.15, 'Q', 1, 'R134a')


I am working on the file "T1000/playground/FromTdms.ipynb".
The first task is:
Typical read usage
cf. https://nptdms.readthedocs.io/en/latest/quickstart.html
Can you write Python code for it?

import nptdms
from nptdms import TdmsFile

example_tdms_file = TdmsFile(tdms_test_fixtures_path + "example.tdms")
print(type(example_tdms_file))
print(dir(example_tdms_file))
#example_channel = example_tdms_file.object('Group', 'Channel1')
#data = channel.data


I am working on the file "T1000/playground/FromTdms.ipynb".
The first task is:
With memmap
__init__(file, memmap_dir=None)
Initialise a new TDMS file object, reading all data.
memmap_dir – The directory to store memmapped data files in, or None to read data into memory. The data files are created as temporary files and are deleted when the channel data is no longer used. tempfile.gettempdir() can be used to get the default temporary file directory.
Can you write Python code for it?

print(os.path.abspath(os.getcwd()))

example_tdms_file = TdmsFile(tdms_test_fixtures_path + "example.tdms", os.path.abspath(os.getcwd()) + "/tempdata/")

print(example_tdms_file.memmap_dir)


I am working on the file "T1000/playground/FromTdms.ipynb".
The first task is:
cf. https://nptdms.readthedocs.io/en/latest/apireference.html
object(*path)
Get a TDMS object from a file.
.groups
Return the names of groups in the file.
Can you write Python code for it?


I am working on the file "T1P1-Finding-Lane-Lines/P1.ipynb".
The first task is:
At this point, if you were successful you probably have the Hough line segments drawn onto the road, but what about identifying the full extent of the lane and marking it clearly as in the example video (P1_example.mp4)?  Think about defining a line to run the full length of the visible lane based on the line segments you identified with the Hough Transform.  Modify your draw_lines function accordingly and try re-running your pipeline.
Now for the one with the solid yellow lane on the left. This one's more tricky!
Can you write Python code for it?

# Reset global variables
set_global_prev_left_line([])
set_global_prev_right_line([])

yellow_output = 'yellow.mp4'
clip2 = VideoFileClip('solidYellowLeft.mp4')
yellow_clip = clip2.fl_image(process_image)
%time yellow_clip.write_videofile(yellow_output, audio=False)

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(yellow_output))


I am working on the file "T1P1-Finding-Lane-Lines/P1.ipynb".
The first task is:
Let's try the one with the solid white lane on the right first ...
Can you write Python code for it?

# Reset global variables
set_global_prev_left_line([])
set_global_prev_right_line([])

white_output = 'white.mp4'
clip1 = VideoFileClip("solidWhiteRight.mp4")
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
%time white_clip.write_videofile(white_output, audio=False)


I am working on the file "T1P1-Finding-Lane-Lines/P1.ipynb".
The first task is:
Play the video inline, or if you prefer find the video in your filesystem (should be in the same directory) and play it in your video player of choice.
Can you write Python code for it?

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(white_output))


I am working on the file "T1P1-Finding-Lane-Lines/P1.ipynb".
The first task is:
Reflections
Congratulations on finding the lane lines!  As the final step in this project, we would like you to share your thoughts on your lane finding pipeline... specifically, how could you imagine making your algorithm better / more robust?  Where will your current algorithm be likely to fail?
Please add your thoughts below,  and if you're up for making your pipeline more robust, be sure to scroll down and check out the optional challenge video below!

Finding lane lines project is a starter project with relatively a simple use case. For a real self-driving car scenarios, finding lane lines need to take care of complex scenarios, real world road conditions, lights and other factors.

Finding lane lines project doesn't take into consideration of normal driving scenarios, like junctions, signals, lane changes etc.

Algorithms for finding lane lines project is based on successful canny and hough lines algorithms, any road conditions that may not work well for canny and hough lines requirements/assumptions will have challenges.

Making the algorithm work for a video (moving car) is much different from making it work for a single image. For video, the algorithm needs to consider prior frames and average the lane lines for smoother lane prediction/projections.

Tried 2 different approaches, first one based on largest line segment, extrapolating and averaging with prior frames. Second approach was to average both left and right segments, extrapolate and average with prior frames. Haven't found significant difference on the results for the given videos.

Averaging with prior frames needed to be balanced based on our tests. Noticed that, larger the number of prior frames for average, it affects the average lane line to be off the actual lane line on the frame.

To have a smooth lane line projection, average lane line to be drawn instead of that frame's lane line.

Many of the values and parameters could be further adjusted for better results, on the other hand, tuning to specific scenarios may not work...
Can you write Python code for it?

# Reset global variables
set_global_prev_left_line([])
set_global_prev_right_line([])

challenge_output = 'extra.mp4'
clip2 = VideoFileClip('challenge.mp4')
challenge_clip = clip2.fl_image(process_image)
%time challenge_clip.write_videofile(challenge_output, audio=False)

HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(challenge_output))


I am working on the file "T1P1-Finding-Lane-Lines/P1.ipynb".
The first task is:
Some OpenCV functions (beyond those introduced in the lesson) that might be useful for this project are:
cv2.inRange() for color selection
cv2.fillPoly() for regions selection
cv2.line() to draw lines on an image given endpoints
cv2.addWeighted() to coadd / overlay two images
cv2.cvtColor() to grayscale or change color
cv2.imwrite() to output images to file
cv2.bitwise_and() to apply a mask to an image
Check out the OpenCV documentation to learn about these and discover even more awesome functionality!
Below are some helper functions to help get you started. They should look familiar from the lesson!
Can you write Python code for it?

import math

def grayscale(img):
    """Applies the Grayscale transform
    This will return an image with only one color channel
    but NOTE: to see the returned image as grayscale
    (assuming your grayscaled image is called 'gray')
    you should call plt.imshow(gray, cmap='gray')"""
    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Or use BGR2GRAY if you read an image with cv2.imread()
    # return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

def canny(img, low_threshold, high_threshold):
    """Applies the Canny transform"""
    return cv2.Canny(img, low_threshold, high_threshold)

def gaussian_blur(img, kernel_size):
    """Applies a Gaussian Noise kernel"""
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

def region_of_interest(img, vertices):
    """
    Applies an image mask.

    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    """
    #defining a blank mask to start with
    mask = np.zeros_like(img)

    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255

    #filling pixels inside the polygon defined by "vertices" with the fill color
    cv2.fillPoly(mask, vertices, ignore_mask_color)

    #returning the image only where mask pixels are nonzero
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image

def draw_lines(img, lines, color=[255, 0, 0], thickness=2):
    """
    NOTE: this is the function you might want to use as a starting point once you want to
    average/extrapolate the line segments you detect to map out the full
    extent of the lane (going from the result shown in raw-lines-example.mp4
    to that shown in P1_example.mp4).

    Think about things like separating line segments by their
    slope...


I am working on the file "T1P1-Finding-Lane-Lines/P1.ipynb".
The first task is:
Test on Videos
You know what's cooler than drawing lanes over images? Drawing lanes over video!
We can test our solution on two provided videos:
solidWhiteRight.mp4
solidYellowLeft.mp4
Can you write Python code for it?

# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
A continuación, se visualizan brevemente los datos
Can you write Python code for it?

# Se grafican los datos obtenidos
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(12,6))
plt.scatter(X[:, 0], X[:,1], s=50, c=y, cmap=plt.cm.winter)
plt.title('Datos generados')
plt.show()


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
Como es de esperarse, este kernel no se comporta lo suficientemente bien en comparación a RBF debido a que la forma de las fronteras no es sigmoidal. Además, requiere de altos valores de $C$ para lograr errores pequeños lo que posiblemente indica overfitting del conjunto de datos
Árbol de decisión
Los árboles de decisión dividen el espacio de los atributos en subconjuntos con el fin de utilizar la heurística de dividir y conquistar, que es ampliamente utilizada en la cienca de la computación. Cada subespacio en el árbol contendrá la clase más probable dentro de una misma sección.
El hiperparámetro de este modelo corresponde al a profundidad del árbol que corresponde la número máximo de subcortes posibles del espacio original
Can you write Python code for it?

from sklearn.tree import DecisionTreeClassifier as Tree

def train_model(param):
    model = Tree() #edit the train_model function
    model.set_params(max_depth=param,criterion='gini',splitter='best')
    model.fit(X,y)
    return model

p_tree_min = 1
p_tree_max = 10
interactive(visualize_border_interactive,param=(p_tree_min,p_tree_max))

import matplotlib.pyplot as plt
params = np.arange(1, 10, 1)

train_errors = []
test_errors = []

for p in params:
    model= Tree()
    model.set_params(max_depth=p,criterion='gini',splitter='best')
    model.fit(X,y)

    y_train_pred = model.predict(X)
    y_test_pred = model.predict(Xtest)
    train_error = (1-accuracy_score(y, y_train_pred))
    test_error = (1-accuracy_score(ytest, y_test_pred))

    train_errors.append(train_error)
    test_errors.append(test_error)

plt.figure(figsize=(10, 8))
plt.plot(params, train_errors, label="Train Error")
plt.plot(params, test_errors, label="Test Error")
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.xlabel('Parámetro')
plt.ylabel('Error')
plt.show()


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
Con un error de clasificación de entrenamiento igual a:
Can you write Python code for it?


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
De manera teórica, el parametro $C$ en la SVM lineal controla el tradeoff entre maximizar el margen entre las dos clases y minimizar el error de clasificación del conjunto de entrenamiento (considerando una función objetivo dada por $CA+B$ y en dónde la constante está dada generalmente por $C = \frac{1}{\lambda}$). Si se utilizan valores de $C$ grandes, el modelo de SVM lineal intentará con más fuerza clasificar correctamente los puntos de entrenamiento, lo que disminuye la maximización del margen y lleva a posible overfitting.
Se puede observar en el gráfico interactivo que para un valor de $C$ cercano a 0, se tiene una frontera que clasifica menos puntos de la clase $AZUL$, mientras que valores cercanos a 1 consideran mayor puntos de la clase $AZUL$. Esto es esperado considerando el significado del parámetro $C$.
SVM No lineal
En el método de SVM no lineal, se utilizan kernels que realizan transformaciones al espacio original de datos a nuevos espacios en donde sea posible tener fronteras linealmente separables. El ejemplo clásico es cuando la frontera es de tipo circular. En este caso, se necesita utilizar un método de kernel para llevar a un espacio lineal a los datos. En este caso, una transformación teórica podria ser $z = x^2 + y^2$ que transforma el espacio euclidiano original en un espacio de coordenadas poalres. Aquí, si es posible encontrar una frontera linealmente separable.
A continuación se exploran las fronteras de decisión de distintos kernels (por tanto, supuestos de transformaciones distintos) sujetos a distintos valores del parámetro C. Estos kernels producen fronteras que no son lineales en el espacio original de los datos.
Kernel de función radial base o Radial basis function (RBF)
Can you write Python code for it?

from sklearn.svm import SVC as SVM #SVC is for classification
def train_model(param):
    model= SVM()
    model.set_params(C=param,kernel='rbf')
    model.fit(X,y)
    return model

p_min_svm_rbf = 0.001
p_max_svm_rbf = 1
interactive(visualize_border_interactive,param=(p_min_svm_rbf,p_max_svm_rbf, 0.001))

import matplotlib.pyplot as plt
params = np.arange(0.001, 1.0, 0.03)

train_errors = []
test_errors = []

for p in params:
    model= SVM()
    model.set_params(C=p,kernel='rbf')
    model.fit(X,y)

    y_train_pred = model.predict(X)
    y_test_pred = model.predict(Xtest)
    train_error = (1-accuracy_score(y, y_train_pred))
    test_error = (1-accuracy_score(ytest, y_test_pred))

    train_errors.append(train_error)
    test_errors.append(test_error)

plt.figure(figsize=(10, 8))
plt.plot(params, train_errors, label="Train Error")
plt.plot(params, test_errors, label="Test Error")
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.xlabel('Parámetro')
plt.ylabel('Error')
plt.show()


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
El parámetro $C$ es un parámetro de regularización que actua como el inverzo de la fuerza de regularización para la norma $l_2$ en donde valores cercanos a $1.0$ indican ausencia de regularización y valores cercanos a $0.0$ indican la máxima posible fuerza de regularización.
Cuando hay alta regularización, aumenta el número de outliers que son ignorados durante el entrenamiento.
SVM Lineal
El método de SVM (Support Vector Machine) busca una frontera de decisión maximizando el margen entre las distintas clases o labels del conjunto de datos, procurando a la vez clasificar la mayor cantidad de datos en el conjunto de entrenamiento correctamente. Existe un hiperparámetro que mide el tradeoff entre la maximización del margen y el error de entrenamiento que es generalmente denotado por C
Se construye el siguiente gráfico con la frontera seleccionada por una SVM de tipo lineal
Can you write Python code for it?

from sklearn.svm import SVC as SVM

def train_model(param):
    model= SVM()
    model.set_params(C=param,kernel='linear')
    model.fit(X,y)
    return model

p_min_svm_linear = 0.001
p_max_svm_linear = 1
interactive(visualize_border_interactive,param=(p_min_svm_linear,p_max_svm_linear, 0.001))

import numpy as np
import matplotlib.pyplot as plt
params = np.arange(0.001, 1.0, 0.03)

train_errors = []
test_errors = []

for p in params:
    model= SVM()
    model.set_params(C=p,kernel='linear')
    model.fit(X,y)

    y_train_pred = model.predict(X)
    y_test_pred = model.predict(Xtest)
    train_error = (1-accuracy_score(y, y_train_pred))
    test_error = (1-accuracy_score(ytest, y_test_pred))

    train_errors.append(train_error)
    test_errors.append(test_error)

plt.figure(figsize=(10, 8))
plt.plot(params, train_errors, label="Train Error")
plt.plot(params, test_errors, label="Test Error")
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.xlabel('Parámetro')
plt.ylabel('Error')
plt.show()


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
Linear Discriminant Analysis (LDA)
A continuación se entrena un modelo usando LDA. Este algoritmo asume que la función de densidad de cada clase es gaussiana y que además existe una matriz de covarianza $\Sigma$ igual entre las clases.
Can you write Python code for it?

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
model_LDA = LDA()
model_LDA.fit(X,y)
visualize_border(model_LDA,X,y,"LDAs")


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
QDA v/s LDA
Diferencia cualitativa y teórica
La principal diferencia entre QDA y LDA se puede visualizar en las secciones anteriores en donde se puede observar claramente que las fronteras calculadas por LDA son lineales, mientras que las de QDA y en honor a su nombre son cuadráticas.
Diferencia cuantitativa
A continuación se presentan los errores de clasificación para ambos modelos
Can you write Python code for it?

from sklearn.metrics import accuracy_score

y_pred_LDA = model_LDA.predict(X)
y_pred_QDA = model_QDA.predict(X)
print("Miss Classification Loss for LDA: %f"%(1-accuracy_score(y, y_pred_LDA)))
print("Miss Classification Loss for QDA: %f"%(1-accuracy_score(y, y_pred_QDA)))


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
Quadratic Discriminant Analysis (QDA)
A continuación se entrena un modelo usando QDA. Este algoritmo, al igual que LDA, asume que la densidad de los datos siguen distribuciones gaussianas, buscando encontrar la frontera que maximice la distancia entre dos distribuciones. A diferencia de LDA, QDA no asume nada respecto a las matrices de covarianza de dichas distribuciones.
Can you write Python code for it?

from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis as QDA
model_QDA = QDA()
model_QDA.fit(X,y)
visualize_border(model_QDA,X,y,"QDA")


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
Se puede apreciar que, en este caso particular, es QDA el que tiene mejor rendimiento en comparación a LDA. Esto no sorprende debido a la naturaleza de la distribución de los datos que hacen a QDA un mejor estimador.
Logistic Regression
La regresión logística es una forma de utilizar el concepto regresión lineal para clasificar datos según atributos dados. Esta función hace uso de la función Logit para transformar outputs numéricos a labels.
A continuación se define una función para graficar las fronteras seleccionadas por cada máquina permitiendo la interacción con distintos parámetros.
Can you write Python code for it?

from ipywidgets import interactive
def visualize_border_interactive(param):
    model = train_model(param)
    visualize_border(model,X,y,x_test=Xtest,y_test=ytest)


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
Se puede notar que con este kernel se pueden obtener bajos errores de clasificación debido a que la naturaleza de RBF permite envolver correctamente al conjunto sinusoidal de datos, generando una frontera ideal
Kernel polinomial
Can you write Python code for it?

from sklearn.svm import SVC as SVM #SVC is for classification
def train_model(param):
    model= SVM()
    model.set_params(C=param,kernel='poly')
    model.fit(X,y)
    return model

p_min_svm_poly = 0.001
p_max_svm_poly = 1
interactive(visualize_border_interactive,param=(p_min_svm_poly,p_max_svm_poly, 0.001))

import matplotlib.pyplot as plt
params = np.arange(0.001, 1.0, 0.03)

train_errors = []
test_errors = []

for p in params:
    model= SVM()
    model.set_params(C=p,kernel='poly')
    model.fit(X,y)

    y_train_pred = model.predict(X)
    y_test_pred = model.predict(Xtest)
    train_error = (1-accuracy_score(y, y_train_pred))
    test_error = (1-accuracy_score(ytest, y_test_pred))

    train_errors.append(train_error)
    test_errors.append(test_error)

plt.figure(figsize=(10, 8))
plt.plot(params, train_errors, label="Train Error")
plt.plot(params, test_errors, label="Test Error")
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.xlabel('Parámetro')
plt.ylabel('Error')
plt.show()


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
Se puede notar que es muy fácil que el árbol de decisión caiga en overfitting. En efecto, la profundidad óptima está entre 3 y 4, y valores mayores a estos manifiestan el efecto de overfitting
Clasificador de K-vecinos más cercano
Este modelo utiliza los k-vecinos más similares de cada dato de entrenamiento para calcular la frontera de decisión. Su parámetro corresponde al número de vecinos $K$.
Cuando $K$ es muy pequeño, restringimos la región de comparación haciendo que nuestro clsificador sea más "ciego" a la distribución global de los datos. Este valor pequeño conducirá a overfitting.
Cuando $K$ es grande, el clasificador se hace más resistente a outliers, pero se hará más propenso a underfitting
Can you write Python code for it?

from sklearn.neighbors import KNeighborsClassifier

def train_model(param):
    model = KNeighborsClassifier()
    model.set_params(n_neighbors=param)
    model.fit(X,y)
    return model

p_tree_min = 1
p_tree_max = 50
interactive(visualize_border_interactive,param=(p_tree_min,p_tree_max))

import matplotlib.pyplot as plt
params = np.arange(1, 50, 1)

train_errors = []
test_errors = []

for p in params:
    model = KNeighborsClassifier()
    model.set_params(n_neighbors=p)
    model.fit(X,y)

    y_train_pred = model.predict(X)
    y_test_pred = model.predict(Xtest)
    train_error = (1-accuracy_score(y, y_train_pred))
    test_error = (1-accuracy_score(ytest, y_test_pred))

    train_errors.append(train_error)
    test_errors.append(test_error)

plt.figure(figsize=(10, 8))
plt.plot(params, train_errors, label="Train Error")
plt.plot(params, test_errors, label="Test Error")
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.xlabel('Parámetro')
plt.ylabel('Error')
plt.show()


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
Y con un error de clasificación de testing igual a:
Can you write Python code for it?


I am working on the file "T2/Pregunta1.ipynb".
The first task is:
Y un error de testing igual a:
Can you write Python code for it?

(Xtest, ytest) = build_data(8000, noise_seed=7000)
1-model_LDA.score(Xtest, ytest)


I am working on the file "T2/Pregunta3.ipynb".
The first task is:
Ahora se hace la separación de test - train
Can you write Python code for it?

import numpy as np
np.random.seed(14)
msk = np.random.rand(len(df)) < 0.8

# manual preprocess
df_train = df[msk].copy()
df_test = df[~msk].copy()

# default countvector preprocess
df_train_df = df[msk].copy()

import matplotlib.pyplot as plt

length = df_train['norm_content'].map(len)

plt.figure(figsize=(20,10))
plt.hist(length, bins=(max(length)-min(length)+1), align='right')
plt.title("Tokens count distribution")
plt.xlabel("Lenght")
plt.ylabel("Frequency")
plt.xticks(np.arange(min(length), max(length)+1, 1))
plt.show()

length = df_train['norm_content'].map(len)
df_train = df_train[np.abs(length-length.mean())<=(3*length.std())]
length = df_train['norm_content'].map(len)

plt.figure(figsize=(20,10))
plt.hist(length, bins=(max(length)-min(length)+1), align='right')
plt.title("Tokens count distribution")
plt.xlabel("Lenght")
plt.ylabel("Frequency")
plt.xticks(np.arange(min(length), max(length)+1, 1))
plt.show()


I am working on the file "T2/Pregunta3.ipynb".
The first task is:
Cabe notar que modelos como SVM, LDA y QDA no fueron posibles de estudiar debido a que las operaciones entre matrices que estos necesitan son muy complicadas, probocando que el cálculo fuera muy largo o simplemente matando el kernel de python.
g) Utilice y explique las métricas que calcula la función classification report de la librería sklearn. En base a las distintas métricas calculadas ¿Cuáles clasificadores son los que mejor se comportan?
Can you write Python code for it?

from sklearn.metrics import classification_report
def score_report(model, X, y):
    name = model[0]
    acc_tr = model[2]
    acc_test = model[3]
    print("Scores for "+name+":")
    print("\tTraining Accuracy: %f"%(acc_tr))
    print("\tTest Accuracy: %f"%(acc_test))
    print("\tDetailed Analysis Testing Results ...")
    print(classification_report(y, model[1].predict(X), target_names=['+','-']))
    print("")

score_report(naive_bayes, X_test, df_test['clase'])
score_report(multi_naive_bayes, X_test, df_test['clase'])
score_report(logistic, X_test, df_test['clase'])
score_report(decision_tree, X_test, df_test['clase'])
score_report(gradient_descent, X_test, df_test['clase'])


I am working on the file "T2/Pregunta3.ipynb".
The first task is:
Gráfico en item m)
k) Utilice clasificadores binarios que pueden ser extendidos a través de otras técnicas, tal como One vs One y One vs All/Rest.
Can you write Python code for it?

from sklearn.multiclass import OneVsRestClassifier
from sklearn.multiclass import OneVsOneClassifier

nb_ovo = train("Naive Bayes OVO",OneVsOneClassifier(BernoulliNB()), X_train, df_train['sentiment_class'], X_test, df_test['sentiment_class'])
nb_ovr = train("Naive Bayes OVR",OneVsRestClassifier(BernoulliNB()), X_train, df_train['sentiment_class'], X_test, df_test['sentiment_class'])


I am working on the file "T2/Pregunta3.ipynb".
The first task is:
d) Haga una reducción binaria al problema, para trabajarlo como un problema de clasificación de dos clases. Para esto, agrupe las distintas emociones como positivas y negativas (defina un criterio), se recomienda codificar las clases como +1 y −1 respectivamente. Recuerde tener presente que el desbalanceo de los datos puede afectar considerablemente al modelo.
Can you write Python code for it?

pos = ['neutral', 'happiness', 'love', 'surprise', 'fun', 'relief', 'enthusiasm']

df_train['clase'] = df_train['sentiment'].apply(lambda x: 1 if x in pos else -1)
df_train_df['clase'] = df_train_df['sentiment'].apply(lambda x: 1 if x in pos else -1)

df_test['clase'] = df_test['sentiment'].apply(lambda x: 1 if x in pos else -1)

print(df_train_df['clase'].value_counts()+df_test['clase'].value_counts())
df.head()


I am working on the file "T2/Pregunta3.ipynb".
The first task is:
f1-score puede ser interpretado como el promedio poderado de precision y recall, siendo 0 y 1 los puntajes mínimos y máximos respectivamente.
precision está definido por $\frac{tp}{tp+fp}$, donde $tp$ y $fp$ son la cantidad de verdaderos y falsos positivos respectivamente, es decir, precision denota la habilidad del clasificador de no etiquetar una muestra que es negativa como positiva.
recall está definido por $\frac{tp}{tp+fn}$, donde $fn$ es el número de falsos negativos. recall denota la habilidad del clasificador de etiquetar todas las muestras positivas como positiva.
support corresponde al número de ocurrencias de cada clase.
h) [Opcional] Visualice las predicciones de algún modelo generativo (probabilístico) definido anteriormente, tomando un subconjunto aleatorio de tweets de pruebas y explorando las probabilidades que asigna el clasificador a cada clase.
Can you write Python code for it?

import random
random.seed(14)
test_pred = logistic[1].predict_proba(X_test)
spl = random.sample(range(len(test_pred)), 15)

print("Negative \t Positive \t Tweet")
for i in spl:
    print(test_pred[i],"\t",df_test.reset_index()['content'][i])


I am working on the file "T2/Pregunta3.ipynb".
The first task is:
j) Utilice los clasificadores que son extendidos por defecto a múltiples clases para detectar emociones en cada tweet, muestre sus desempeños a través del error de pruebas en un gráfico resumen.
Can you write Python code for it?

dt = train("DecisionTree",DecisionTreeClassifier(), X_train, df_train['sentiment_class'], X_test, df_test['sentiment_class'])
mnb = train("MultinomialNB",MultinomialNB(), X_train, df_train['sentiment_class'], X_test, df_test['sentiment_class'])
#mlp = train("Multi-layer Perceptron",MLPClassifier(), X_train, df_train['sentiment_class'], X_test, df_test['sentiment_class'])


I am working on the file "T2/Pregunta3.ipynb".
The first task is:
l) Para el caso de la Regresión Logística compare sus dos métodos para ser extendidos a múltiples clases. Uno a través de One vs Rest y otro definiendo que la variable a predecir se distribuye Multinomial.
Can you write Python code for it?

lr_mtn = train("Log. Reg. Multi",LogisticRegression(), X_train, df_train['sentiment_class'], X_test, df_test['sentiment_class'])
lr_ovr = train("Log. Reg. OVR",OneVsRestClassifier(LogisticRegression()), X_train, df_train['sentiment_class'], X_test, df_test['sentiment_class'])

plot_scores("Modelos Multi-clase",[dt, mnb, nb_ovo, nb_ovr, lr_mtn, lr_ovr])


I am working on the file "T3/.ipynb_checkpoints/T3.2-Self-Organized-Criticality-checkpoint.ipynb".
The first task is:
3. Bak-Sneppen model
The Bak–Sneppen model is a simple model of co-evolution between interacting species. It was developed to show how self-organized criticality may explain key features of the fossil record, such as the distribution of sizes of extinction events and the phenomenon of punctuated equilibrium.
The model dynamics repeatedly eliminates the least adapted species and mutates it and its neighbors to recreate the interaction between species. We consider N species, which are associated with a fitness factor f(i). They are indexed by integers i around a ring. The algorithm consists in choosing the least fit species, and then replacing it and its two closest neighbors (previous and next species) by new species, with a new random fitness. After a long run there will be a minimum required fitness, below which species don't survive. These "long-run" events are referred to as avalanches, and the model proceeds through these avalanches until it reaches a state of relative stability where all species' fitness are above a certain threshold.
As well, we can use this model to study the scientific progress. Accounts of scientific change roughly fall into two categories, one and two-process change. One-process views hold that scientific change is a single process of movement toward an independent point of reference, for example Popper's cycle of conjecture and refutation. One of the most influential criticisms of this view is by Thomas Kuhn, who held that standards for science are not independent, but coevolve with scientific activity itself. Science then does not evolve toward a goal but away from goals previously set, with no fixed point left to compare its progress against. Without such a stable ‘‘Archimedean platform,’’ there is nothing against which science can meaningfully be said to cumulate. Moreover, changes of standard caused by scientific change can cause cascades of further scientific changes.

3.1. Exercise
This example displays the Bak-Sneppel model. Every column represents one specie. As the...
Can you write Python code for it?

%%HTML
<iframe src="https://www.openprocessing.org/sketch/434390/embed/" width="500" height="400"></iframe>


I am working on the file "T3/.ipynb_checkpoints/T3.2-Self-Organized-Criticality-checkpoint.ipynb".
The first task is:
3.2. Exercise
To analyse the Bak-Sneppen model, we will perform some calculations on Python. Take a look at the code and try to understand the steps.
Can you write Python code for it?

import math
import numpy as np

# Bak-Sneppen algorithm
def bak_sneppen(N, maxGenerations, neighbourSize, neighbourProb):
    agesSize = 300
    ages      = np.zeros(N)
    agesStart = np.zeros((agesSize, N))
    agesEnd   = np.zeros((agesSize, N))
    x = np.zeros(maxGenerations)
    f = np.random.rand(N)
    p1 = 1
    # Iterate on generations
    for t in range(maxGenerations):
        ages= ages + 1
        # Search for the specie with least fitness
        ind = np.argmin(f)
        # Randomly disposal of the least fitness specie and its neighbours
        if np.random.rand(1)<p1:
            f[ind]=np.random.rand(1)
            ages[ind] = 0
            for d in range(1,1+math.floor(neighbourSize/2)):
                if np.random.rand(1) < neighbourProb:
                    f[(ind+d)%N] = np.random.rand(1)
                    ages[(ind+d)%N] = 0
                if np.random.rand(1) < neighbourProb:
                    f[(ind-d)%N] = np.random.rand(1)
                    ages[(ind+d)%N] = 0
        x[t]=np.mean(f)
        # Store the values of firsts generations
        if t<agesSize:
            for iCell in range(N):
                agesStart[t, iCell] = ages[iCell]
        # Store the values of lasts generations
        if maxGenerations-t<agesSize:
            for iCell in range(N):
                agesEnd[t-maxGenerations, iCell] = ages[iCell]
    return [x, agesStart, agesEnd]


I am working on the file "T3/.ipynb_checkpoints/T3.2-Self-Organized-Criticality-checkpoint.ipynb".
The first task is:
4.2. Exercise
Now, we will analyse the results of the Bak-Sneppen model to check if they are fractal. To do so, we will obtain the fractal index of the avalanche size.
- Does the Bak-Sneppen model have a fractal behaviour?
- Modify the parameters of the model, i.e. the neighbourSize and neighbourProb variables, and relate the changes to the fractal index.
- Tip: The maxGenerations value cannot be small because the DFA algorithm needs a long serie to accurately perform the calculations.
Can you write Python code for it?

from pyeeg import dfa as dfa
import numpy as np
import math

N              = 64 # Number of cells in one generation
maxGenerations = 10000 # Calculate up to this maximum number of generations
neighbourSize  = 2 # Number (even) of eliminated neighbours (2 = left and right neighbours)
neighbourProb  = 0 # Probability of the neighbours to be eliminated (0 < neighbourProb < 1)
# Figure size
plt.figure(figsize=(16, 4), dpi=72)

# Calculate the simulation
[meanFitness, agesStart, agesEnd] = bak_sneppen(N, maxGenerations, neighbourSize, neighbourProb)
plt.subplot(1,2,1)
plt.xlabel('Generation')
plt.ylabel('Fitness average')
plt.plot(meanFitness)

# Calculate the fractal index
plt.subplot(1,2,2)
[alpha, n, F] = plot_dfa(meanFitness[int(maxGenerations/2):], 2, 0.2, maxGenerations/5)


I am working on the file "T3/.ipynb_checkpoints/T3.2-Self-Organized-Criticality-checkpoint.ipynb".
The first task is:
Now, we will call the function with different parameters to analize the results. We will plot the resulting board, the size of avalanches in every timestep, and the histogram of those avalanches. The histogram let us know how many avalanches there has been with a certain size range.

Sometimes, the linear scales of the plots are not convenient when the data vary logarithmically. In the code, look for the lines that switch the scales from linear to logarithmic and uncomment them. E.g.: ax.set_yscale('log')
As well, the chronogram graph of the avalanches may show too many data. To display only a certain range of time, look for the commented line that set the limits of the horizontal axis and modify it to your convenience.
Different types of grain (e.g. rounded or elliptical) may alter the number of grains that can stand in a pile before it falls down. Modify the value of the "MAX_GRAINS" variable to simulate different types of grain. Don't worry to break the grains in pieces to distribute them homogeneously among the adjacent piles. Are the results similar or different?
Some grains may remain in the pile after the avalanche, so not falling down to its neighbours. We can simulate these cases with the "FALL_PROBABILITY" variable, whose values go from zero (no grains fall) and 1 (all the grains of the pile are distributed among the neighbours).
Can you write Python code for it?

%matplotlib inline
from matplotlib import pyplot as plt

# Width of the board (height is equal to width)
BOARD_SIZE = 100
# Maximum number of stackable sand grains that will cause an avalanche
MAX_GRAINS = 4
# Probablity of grains falling down to adjacent piles during an avalanche
FALL_PROBABILITY = 1
# Total number of grains dropped on the board
NUM_GRAINS = 10000
# Calculate the simulation
[board, avalancheSize] = sandPileModel(BOARD_SIZE, NUM_GRAINS, MAX_GRAINS, FALL_PROBABILITY)

# Figures
fig = plt.figure(figsize=(15,4))

# Sandpile boad
plt.subplot(1,3,1)
plt.axis('off')
plt.imshow(board / np.max(board), cmap='gray', vmin=0, vmax=1)

# Chronogram of avalanches
ax = plt.subplot(1,3,2)
plt.xlabel('Time')
plt.ylabel('Avalanche size')
#ax.set_xscale('log')
#ax.set_yscale('log')
#ax.set_xlim([max(0, len(avalancheSize)-300), len(avalancheSize)])
plt.plot(range(len(avalancheSize)), avalancheSize)

# Histogram of avalanches
ax = plt.subplot(1,3,3)
plt.xlabel('Avalanche size')
plt.ylabel('Number of ocurrences')
#plt.hist(avalancheSize, bins=np.linspace(0,max(avalancheSize),num=10), rwidth=0.9)
ax.set_xscale('log')
ax.set_yscale('log')
plt.hist(avalancheSize, bins=np.logspace(np.log10(MAX_GRAINS),np.log10(max(avalancheSize)),num=10), rwidth=0.9)

plt.show()


I am working on the file "T3/.ipynb_checkpoints/T3.2-Self-Organized-Criticality-checkpoint.ipynb".
The first task is:
The example below plots the mean fitness in a chronogram. At first, this number will grow along time because the least fitness specie is eliminated on each generation. As time goes by, an increasingly random growth is expected because old species will have an almost constant high value of fitness while the new species are introduced with random fitness.

Modify the number of eliminated neighbours and their probability of being eliminated, and analyse the rise of the mean fitness along time according the modifications.
For better visualizations, try to display the graphs on logarithmic scale.
Can you write Python code for it?

%matplotlib inline
from matplotlib import pyplot as plt

N              = 100 # Number of cells in one generation
maxGenerations = 10000 # Calculate up to this maximum number of generations
neighbourSize  = 2 # Number (even) of eliminated neighbours (2 = left and right neighbours)
neighbourProb  = 1 # Probability of the neighbours to be eliminated (0 < neighbourProb < 1)

# Calculate the simulation
[agesEliminated, agesStart, agesEnd] = bak_sneppen(N, maxGenerations, neighbourSize, neighbourProb)
agesImage = agesStart

# Figures
plt.figure(figsize=(15,4))

# Ages of the eliminated specie
ax = plt.subplot(1,3,1)
plt.title('Age')
plt.xlabel('Species')
plt.ylabel('Generations')
ax.set_xticks([])
plt.imshow(agesImage / np.max(agesImage), cmap='hot_r', vmin=0, vmax=1)

# Chronogram
ax = plt.subplot(1,3,2)
plt.xlabel('Generations')
plt.ylabel('Fitness average')
ax.set_xscale('log')
ax.set_yscale('log')
plt.plot(range(len(agesEliminated)), agesEliminated)

# Histogram
ax = plt.subplot(1,3,3)
plt.xlabel('Fitness average')
plt.ylabel('Number of ocurrences')
#plt.hist(agesEliminated, bins=np.linspace(1e-4,max(agesEliminated),num=10), rwidth=0.9)
plt.hist(agesEliminated, bins=np.logspace(-0.6, np.log10(max(agesEliminated)),num=10), rwidth=0.9)
ax.set_xscale('log')
ax.set_yscale('log')

plt.show()


I am working on the file "T3/.ipynb_checkpoints/T3.3-Social-Minimal-Interaction-checkpoint.ipynb".
The first task is:
3. Fractal analysis
Despite of its apparent simplicity, the perceptual crossing paradigm comprises several embedded of dynamic interaction, resulting on auto-correlations of the signals over different time scales.
Critical systems typically display temporal and spatial scale invariance in the form of fractals and 1/f noise, reflecting the process of propagation of long-range interactions based on local effects. For the complex systems approach to cognitive science, self-organized criticallity is appealing because it allows us to imagine systems that are able to self-regulate coordinated behaviours at different scales in a distributed manner and without a central controller.
We argue that 1/f noise analysis can account not only for the integratedness the behaviour of an agencial system (e.g. the mental, psychological characteristics of human behaviour) but also can characterize the nature of social interaction process. In our experimental setup we have a broad range of kinds of social interaction: humans recognizing each others as such, humans interacting with bots with artificial behaviour, humans failing to recognize other humans, bots tricking humans... Can we characterize when genuine social interaction emerges? And if so, where does it lies?
For analyzing fractal exponents in the dynamics of social interaction we use the Detrended Fluctuation Analysis (DFA). Since the slope of the fluctuations in a logarithmic plot is not always linear for all scales, we check if there is any cutoff value in which a transition to a linear relation starts. We do this by searching for negative peaks in the second derivate of F (n). We only do this on the right half of the values of n in the plot, in order to find only the cutoffs at larger scales. Once the cutoff is found, we analyze the slope of the function in the decade inferior to the cutoff value. In the cases where there is no cutoff value (as in Figure 2.c) we analyze the interval $n \in [10^{-0.5},10^{0.5}]$.
3.1. Exercise
Run a DFA analysis to obtain the fractal...
Can you write Python code for it?

def plot_dfa_perceptual(x, precision, title, drawPlot):
    ix = np.arange(np.log2(len(x)/4), 4, -precision)
    n = np.round(2**ix)
    [_, n, F] = dfa(x, L=n)
    n = n/115 # Time (seconds) = samples / sample_frequency
    indexes = (n>10**-0.5)&(n<10**0.5) # Time interval for calculating the slope
    P = np.polyfit(np.log(n[indexes]),np.log(F[indexes]), 1)
    beta = 2*P[0]-1 # beta=2*alpha-1
    if drawPlot:
        plt.title(title+r" ($\beta$ = {:0.2f})".format(beta))
        plt.xlabel('n')
        plt.ylabel('F(n)')
        plt.loglog(n, F)
        plt.loglog(n[indexes], np.power(n[indexes], P[0])*np.exp(P[1]), 'r')
    return [beta, n, F]

# Plot figures
series = vel_player
plt.figure(figsize=(16, 4), dpi=72)
indexExamples = [60, 7, 11];
for i,ex in enumerate(indexExamples):
    x = series[ex,:]
    ax = plt.subplot(1,3,(i+1))
    plot_dfa_perceptual(x, 0.1, oppTypes[ex], True);


I am working on the file "T3/.ipynb_checkpoints/T3.3-Social-Minimal-Interaction-checkpoint.ipynb".
The first task is:
The boxplot displays statistical differences.
When the opponent is the oscillatory agent (dashed lines), we find that the values of β in the time series is around 1.5. This means that the interactions are closer to a brown noise structure, meaning that the interaction is more rigid and structured than in the other cases. This makes sense since the movement of the oscillatory agent is going constrain the interactions into its cyclic movement structure.
On the other hand, when the opponent is the shadow agent (dash-dot lines), we have the opposite situation, and the interaction dynamics tends to display values of β greater but close to 0. This means that the history of interaction is more random and uncorrelated. Finally, when the opponent is other human player (solid lines), the exponents of the interactions dynamic are around a value of β close to 1, indicating that they follow a pink noise structure between randomness and coherence. This suggest that the dynamics emerge from a situation where the movement of both players is softly assembled into a coherent coordination.
The 1/f spectrum results show that the changes in the relative position of the player to its opponent show that the interaction process is completely different when genuine social interaction is happening than when the player is interacting with object with trivial (oscillatory) or complex (shadow) patterns of movement. It is interesting that pink noise only emerges for a collective variable (the derivative of the distance) only in the case of human-human interaction, suggesting the hypothesis that social interaction is based on the emergence of the soft assembling of the activity of the pair of players. In the cases when this assembling is more rigid or too weak the emergent system disappears.
Can you write Python code for it?

# Data
series = vel_relative

# Plot figures
plt.figure(figsize=(16, 4), dpi=72)
indexExamples = [0];
for i in range(len(series)):
    [beta,_,_] = plot_dfa_perceptual(series[i,:], 0.5, oppTypes[i], False)
    betas[i] = beta
dataBox = [betas[indexOscill], betas[indexShadow], betas[indexHuman]]
plt.boxplot(dataBox);
plt.ylabel(r'$\beta$');
plt.xticks([1, 2, 3], ['Oscillatory', 'Shadow', 'Human']);


I am working on the file "T3/.ipynb_checkpoints/T3.3-Social-Minimal-Interaction-checkpoint.ipynb".
The first task is:
We can display the box-plot of the velocity to check if there are differences between groups.
- Try other velocity variables looking for differences between groups, e.g. velocity of opponent, relative velocity
Can you write Python code for it?

# Calculate the average velocity of each serie
vel_stats=np.std(vel_player,axis=1) # velocity of the player
#vel_stats=np.std(vel_opponent,axis=1) # velocity of the opponent
#vel_stats=np.std(vel_relative,axis=1) # relative velocity between player

# Plot figure
plt.figure(figsize=(16, 4), dpi=72)
dataBox = [vel_stats[indexOscill], vel_stats[indexShadow], vel_stats[indexHuman]]
plt.boxplot(dataBox);
plt.ylabel("Average velocity")
plt.xticks([1, 2, 3], ['Oscillatory', 'Shadow', 'Human']);


I am working on the file "T3/Pregunta2.ipynb".
The first task is:
b) Entrene un árbol de regresión para resolver el problema usando parámetros por defecto. Con este fin, construya una matriz $X_{train}$ de forma $n_{train} \times d_1$ que contenga los datos de entrenamiento en sus filas, seleccionando las columnas que desee/pueda utilizar para el entrenamiento. Implemente además, la función de evaluación que hemos definido anteriormente para este problema. Evalúe el árbol de regresión ajustado a los datos de entrenamiento sobre el conjunto de entrenamiento y pruebas. Construya un gráfico que compare las predicciones con los valores reales. En este punto usted debiese tener un modelo con puntaje del orden de 0.59, lo que lo dejará más o menos en la posición 2140 de la competencia.
Can you write Python code for it?

from sklearn.tree import DecisionTreeRegressor
import matplotlib.pyplot as plt

def eval_bikemodel(y_predict,y_true,**kwargs):
    diff = np.log(y_predict+1.0) - np.log(y_true+1.0)
    return np.sqrt(np.sum(np.square(diff))/len(y_predict))

model = DecisionTreeRegressor(random_state=0)
model.fit(X_train,Y_train)
score_test = model.score(X_test,Y_test)
print("SCORE TEST=%f"%score_test)

Y_pred_train = model.predict(X_train)
Y_pred_val = model.predict(X_val)
Y_pred_test = model.predict(X_test)
kagg_train = eval_bikemodel(Y_pred_train,Y_train)
kagg_val = eval_bikemodel(Y_pred_val,Y_val)
kagg_test = eval_bikemodel(Y_pred_test,Y_test)

print("KAGG EVAL TRAIN =%f"%kagg_train)
print("KAGG EVAL TEST =%f"%kagg_test)
plt.plot(Y_test,Y_pred_test,'.')
plt.show()


I am working on the file "T3/Pregunta2.ipynb".
The first task is:
e) Entrene una SVM no lineal para resolver el problema midiendo el efecto de las distintas representaciones que haya descubierto hasta este punto. Un detalle importante es que antes de entrenar la SVM sería aconsejable hacer dos tipos de pre-procesamiento adicional de los datos: (i) codificar las variables categóricas en un modo apropiado - por ejemplo como vector binario con un 1 en la posición del valor adoptado-, (ii) escalar los atributos de modo que queden centrados y con rangos comparables. Usando parámetros por defecto para la SVM debiese obtener un score del orden de $0.344$, quedando definitivamente en el top-10 de la competencia.
Can you write Python code for it?

def eval_bikemodel_log(y_predict,y_true,**kwargs):
    y_predict = np.exp(y_predict)-1
    y_true    = np.exp(y_true)-1
    diff = np.log(y_predict+1.0) - np.log(y_true+1.0)
    return np.sqrt(np.sum(np.square(diff))/len(y_predict))

df['day_weekend'] = df['tday'].apply(lambda x: x==0)
df['day_holiday'] = df['tday'].apply(lambda x: x==1)
df['day_work'] = df['tday'].apply(lambda x: x==2)

df['season_spr'] = df['season'].apply(lambda x: x==1)
df['season_sum'] = df['season'].apply(lambda x: x==2)
df['season_fal'] = df['season'].apply(lambda x: x==3)
df['season_win'] = df['season'].apply(lambda x: x==4)

df['weather_clear'] = df['weather'].apply(lambda x: x==1)
df['weather_mist'] = df['weather'].apply(lambda x: x==2)
df['weather_snow'] = df['weather'].apply(lambda x: x==3)
df['weather_rain'] = df['weather'].apply(lambda x: x==4)

Xdf=pd.get_dummies(df[['humidity','windspeed','hour','atemp','tday','season','weather','cmonth','cyear']],
                   columns=['hour','season','weather','tday','cmonth','cyear'])

Ydf=np.log(df.loc[:,'count']+1)
X_train = Xdf[0:ntrain].values
X_val = Xdf[ntrain:nval].values
X_test = Xdf[nval:].values
Y_train = Ydf[0:ntrain].values
Y_val = Ydf[ntrain:nval].values
Y_test = Ydf[nval:].values

from sklearn.preprocessing import StandardScaler
scalerX = StandardScaler()
X_train = scalerX.fit_transform(X_train)
X_val = scalerX.fit_transform(X_val)
X_test = scalerX.transform(X_test)

from sklearn.svm import SVR
model = SVR()
model.fit(X_train,Y_train)
Y_pred_train = model.predict(X_train)
Y_pred_val = model.predict(X_val)
Y_pred_test = model.predict(X_test)

print("Top BikeScore Test: %f"%eval_bikemodel_log(Y_pred_test,Y_test))


I am working on the file "T3/Pregunta2.ipynb".
The first task is:
f) Mejore la SVM definida en el punto anterior haciendo modificaciones a los hiper-parámetros de la máquina ($C$, $\epsilon$ o la misma función de kernel). Naturalmente, está absolutamente prohibido tomar este tipo de decisiones de diseño mirando el resultado de pruebas. Debe realizar estas elecciones evaluando sobre el conjunto de validación. Si no desea utilizarlo, y prefiere implementar validación cruzada u otra técnica automática, tiene la ventaja de poder usar el conjunto de validación como parte del entrenamiento.
Can you write Python code for it?

def train_svm(X,Y,kernel=['linear','poly','rbf','sigmoid','precomputed'],max_c=20,min_c=1):
    scores = []
    c_range = range(min_c,max_c+1)
    e_range = [0.01,0.05,0.1,0.5,1,10]
    count,current = len(kernel)*len(c_range)*len(e_range),1
    for c in c_range:
        for k in kernel:
            for e in e_range:
                model = SVR(C=c,epsilon=e,kernel=k)
                score_bike = cross_val_score(model, X,Y, scoring=make_scorer(eval_bikemodel_log)).mean()
                scores.append([model, 0, score_bike, c, k, e])
                print ("%d/%d"%(current,count), end="\r")
                current+=1
    return scores

def top_scores_svm(scores):
    sorted_scores = sorted(scores, key=(lambda x: -x[2]), reverse=False)
    print("Top 10 models:")
    for s in sorted_scores[-10:]:
        print("  BikeScore:%f \t C: %d \t kernel: %s \t e: %f"%(s[2],s[3],s[4],s[5]))
    return sorted_scores[-1]

scores_svm = train_svm(np.concatenate([X_train,X_val]),np.concatenate([Y_train,Y_val]),kernel=['rbf'],max_c=5)
t=top_scores_svm(scores_svm)

model = SVR(C=t[3],kernel=t[4],epsilon=t[5])
model.fit(np.concatenate([X_train,X_val]),np.concatenate([Y_train,Y_val]))
Y_pred_test_svr = model.predict(X_test)
print("Top BikeScore Test: %f"%eval_bikemodel_log(Y_pred_test_svr,Y_test))


I am working on the file "T3/Pregunta2.ipynb".
The first task is:
g)  Evaúe el efecto de utilizar el dataset de validación para entrenamiento y seleccionar los parámetros estructurales del árbol de clasificación y la SVM usando validación cruzada. El código de ejemplo para esto ha sido proporcionado en las tareas 1 y 2, pero se adjunta de nuevo a continuación
Hecho en f)
h)  Evalúe el efecto de utilizar un ensamblado de 2 máquinas de aprendizaje para predecir la demanda total de bicicletas. Un modelo se especializará en la predicción de la demanda de bicicletas de parte de usuarios registrados y otra en la predicción de la demanda de usuarios casuales. Hay razones claras para pensar que los patrones son distintos.
Can you write Python code for it?

# Ydf=df.ix[:,'count'] #demanda total
Ydf_r=np.log(df['registered']+1) #demanda registrada
Yr_train = Ydf_r[0:ntrain].values
Yr_val = Ydf_r[ntrain:nval].values
Yr_test = Ydf_r[nval:].values

Ydf_c=np.log(df['casual']+1) #demanda casual
Yc_train = Ydf_c[0:ntrain].values
Yc_val = Ydf_c[ntrain:nval].values
Yc_test = Ydf_c[nval:].values

model_r = SVR(C=t[3],kernel=t[4],epsilon=t[5])
model_c = SVR(C=t[3],kernel=t[4],epsilon=t[5])

model_r.fit(np.concatenate([X_train,X_val]),np.concatenate([Yr_train,Yr_val]))
model_c.fit(np.concatenate([X_train,X_val]),np.concatenate([Yc_train,Yc_val]))

Yr_pred_test = model_r.predict(X_test)
Yc_pred_test = model_c.predict(X_test)

print("Top BikeScore Test registered: %f"%eval_bikemodel_log(Yr_pred_test,Yr_test))
print("Top BikeScore Test casual    : %f"%eval_bikemodel_log(Yc_pred_test,Yc_test))
print("Top BikeScore Test count     : %f"%eval_bikemodel_log(Yr_pred_test+Yc_pred_test,Y_test))


I am working on the file "T3/Pregunta2.ipynb".
The first task is:
i)  Evalúe el efecto de utilizar un algoritmo genérico para ensamblar máquinas de aprendizaje para predecir la demanda total de bicicletas. Puede experimentar con una sola técnica (e.g. Random Forest), discuta la evolución a medida que aumenta el número de máquinas.
Can you write Python code for it?

from sklearn.ensemble import RandomForestRegressor

def train_random_forest(Xtrain,Ytrain,Xeval,Yval, estimators, depth):
    model = RandomForestRegressor(n_estimators=estimators,max_depth=depth, random_state=0)
    model.fit(Xtrain, Ytrain)

    Yeval_pred = model.predict(Xeval)

    return (model,eval_bikemodel_log(Yeval_pred,Yval),estimators,depth)

def top_scores_rfr(scores):
    sorted_scores = sorted(scores, key=(lambda x: -x[1]), reverse=False)
    print("Top 10 models:")
    for s in sorted_scores[-10:]:
        print("  BikeScore:%f \t estimators: %d \t depth: %d"%(s[1],s[2],s[3]))
    return sorted_scores[-1]

scores = []
count,current=20*20,1
for e in range(1,20):
    for d in range(1,20):
        scores.append(train_random_forest(X_train,Y_train,X_val,Y_val, e, d))
        print ("%d/%d"%(current,count), end="\r")
        current+=1

t=top_scores_rfr(scores)

t[0].fit(X_train,Y_train)
Ytest_pred = t[0].predict(X_test)

print("Top BikeScore Test: %f"%eval_bikemodel_log(Ytest_pred,Y_test))


I am working on the file "T3/Pregunta3.ipynb".
The first task is:
3.- Calidad de un vino
El objetivo corresponde a predecir la calidad observada por los consumidores de un vino en particular, utilizando un dataset de distintos vinos con distintos atributos químicos que han sido evaluados anteriormente.
Descripción del dataset
Primero, se carga el dataset dentro de un DataFrame de Pandas para describirlo y visualizar su distribución
Can you write Python code for it?

import pandas as pd
df_red = pd.read_csv("data/winequality-red.csv",sep=";")
df_white = pd.read_csv("data/winequality-white.csv",sep=";")

# Add the type column
df_red['type'] = 1
df_white['type'] = 0
df = pd.concat([df_red,df_white], axis=0)
df.describe()


I am working on the file "T3/Pregunta3.ipynb".
The first task is:
A diferencia de Random Forest, se necesita una cantidad menor de estimadores para alcanzar el puntaje máximo del conjunto de testing, y se mantiene la tendencia que indica que es la profundidad del árbol, o bien, los hiperparámetros de los algoritmos que se utilizan para el ensamblado los que impactan más en la calidad del aprendizaje
Otra máquina de aprendizaje
De forma comparativa, se utilizará una SVM no lineal utilizando RBF para contrastar con los métodos anteriores
Can you write Python code for it?

from sklearn.svm import SVC as SVM
from sklearn.metrics import accuracy_score
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import f1_score

def train_svm(param, Xtrain, Ytrain, Xtest, Ytest):
    # Entrenar el árbol
    model= SVM()
    model.set_params(C=param, kernel='rbf')
    model.fit(Xtrain, Ytrain)

    Ytrain_pred = model.predict(Xtrain)
    Ytest_pred = model.predict(Xtest)

    train_error = (1-accuracy_score(Ytrain, Ytrain_pred))
    test_error = (1-accuracy_score(Ytest, Ytest_pred))
    train_f1 = f1_score(Ytrain, Ytrain_pred)
    test_f2 = f1_score(Ytest, Ytest_pred)

    return (train_error, test_error, train_f1, test_f2)

def graph_svm_range(params):
    train_errors = []
    test_errors = []
    f1_train_errors = []
    f1_test_errors = []

    for depth in params:
        (train, test, f1train, f1test) = train_svm(depth, Xtrain, Ytrain, Xtest, Ytest)
        train_errors.append(train)
        test_errors.append(test)
        f1_train_errors.append(f1train)
        f1_test_errors.append(f1test)

    plt.figure(figsize=(10, 8))
    plt.plot(params, f1_train_errors, label="Train F1 Score")
    plt.plot(params, f1_test_errors, label="Test F1 Score")
    plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    plt.xlabel('RBF')
    plt.ylabel('Error')
    plt.show()

params = np.arange(0.001, 1.0, 0.03)
graph_svm_range(params)


I am working on the file "T3/Pregunta3.ipynb".
The first task is:
Así, el mejor algoritmo corresponde a RandomForest
Definición de importancia de atributos
Utilizando la métrica de feature_importances_ disponible en skearn, se obtiene el siguiente ranking de atributos:
Can you write Python code for it?

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import pandas as pd

model = RandomForestClassifier(n_estimators=10, max_depth=18,n_jobs=-1)
model.fit(Xtrain, Ytrain)
cols = model.feature_importances_
labels = df_randomized.columns.tolist()
joined = []
for i in range(0, len(df_randomized.columns)-1):
    joined.append((df_randomized.columns[i], model.feature_importances_[i]))
importance = pd.DataFrame(joined)

importance.sort_values(by=1, ascending=False)


I am working on the file "T3/Pregunta3.ipynb".
The first task is:
El puntaje más alto sobre el conjunto de testing corresponde a 0.88 y ocurre con alrededor de 90 estimadores y una profundidad superior a 15.
Se puede observar que el mayor impacto sobre el error de testing corresponde a la profunidad de los árboles utilizados en random forest. Como el puntaje del conjunto de entrenamiento es casi 1, no es posible obtener mejor resultados que el error anterior.
Ensamblador de árboles de múltiples niveles (AdaBoost)
A diferencia de Random Forest, Ada Boost se enfoca en aquellas instancias que son "difíciles", es decir, que tienen mayor error de clasificación.
El algoritmo no funciona correctamente cuando hay mucho ruido en el conjunto de datos y Random Forest tiende a ser mejo en caso de que hubiese.
Can you write Python code for it?

from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import accuracy_score
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import f1_score

def train_ada_boost(estimators, depth, Xtrain, Ytrain, Xtest, Ytest):
    # Entrenar el árbol
    model = AdaBoostClassifier(base_estimator=Tree(max_depth=depth), n_estimators=estimators)
    model.fit(Xtrain, Ytrain)

    Ytrain_pred = model.predict(Xtrain)
    Ytest_pred = model.predict(Xtest)

    train_error = (1-accuracy_score(Ytrain, Ytrain_pred))
    test_error = (1-accuracy_score(Ytest, Ytest_pred))
    train_f1 = f1_score(Ytrain, Ytrain_pred)
    test_f2 = f1_score(Ytest, Ytest_pred)

    return (train_error, test_error, train_f1, test_f2)

def graph_ada_boost_range(estimator_params, depth_params, z_to_show, upper_estimator, upper_depth):

    points = []
    for a in estimator_params:
        for b in depth_params:
            points.append((a, b))

    train_errors = []
    test_errors = []
    f1_train_errors = []
    f1_test_errors = []

    for point in points:
        (train, test, f1train, f1test) = train_ada_boost(point[0], point[1], Xtrain, Ytrain, Xtest, Ytest)
        train_errors.append(train)
        test_errors.append(test)
        f1_train_errors.append(f1train)
        f1_test_errors.append(f1test)

    x = []
    y = []
    z1 = []
    z2 = []

    for pt in points:
        index = points.index(pt)
        x.append(pt[0])
        y.append(pt[1])
        z1.append(f1_train_errors[index])
        z2.append(f1_test_errors[index])

    z = None
    if z_to_show == 0:
        z = z1
    else:
        z = z2

    fig = plt.figure(figsize=(10, 8))
    ax = fig.add_subplot(1, 1, 1)
    cmap = matplotlib.cm.get_cmap("jet")
    pts = ax.scatter(x, y, s=60, c=z, cmap=cmap,
                 linewidth=0)
    cbar = fig.colorbar(pts, ax=ax)
    fig.axes[-1].set_ylabel("Z")

    ax.grid()

    ax.set_xlabel("Número de estimadores")
    ax.set_ylabel("Profundidad...


I am working on the file "T3/Pregunta3.ipynb".
The first task is:
Las variables de entrada utilizadas se basan en pruebas fisioquímicas de los vinos y corresponden a:
* 1 - fixed acidity: Acidez volátil
* 2 - volatile acidity: Acidez volátil
* 3 - citric acid: Acidez cítrica
* 4 - residual sugar: Azúcar residual
* 5 - chlorides: Cloros
* 6 - free sulfur dioxide: Dioxidos de sulfuro libres
* 7 - total sulfur dioxide: Dioxidos de sulfuro totales
* 8 - density: Densidad
* 9 - pH: pH
* 10 - sulphates: Cantidad de Sulfatos
* 11 - alcohol: Cantidad de alcohol
* 12 - type: Tipo de vino (generado para diferenciar). 1 es vino tinto y 0 es vino blanco
Y por último se tiene la variable de salida que es la calidad de los vinos que tiene un puntaje entre 0 y 10, siendo 0 la peor calidad y 10 la mejor calidad.
* 13 - quality (score between 0 and 10)
Por último, se tiene un total de 6497 vinos en el dataset
A continuación se visualizan los datos:
Can you write Python code for it?

import matplotlib.pyplot as plt
df.boxplot()
plt.show()

import matplotlib.pyplot as plt
import matplotlib
df.hist()
plt.show()


I am working on the file "T3/Pregunta3.ipynb".
The first task is:
Se tiene que la SVM obtiene un puntaje de testing cercano a 0.795, para luego tener un puntaje de testing mayor al de entrenamiento que se puede considerar como overfitting. El accuracy es mucho menor que el puntaje obtenido por los ensambladores. En general, los ensambladores tendran un mejor desempeño que los algoritmos clásicos, siempre y cuando los algoritmos que se utilizan para el ensamblado funcionen correctamente en el conjunto de datos seleccionado.
Selección de mejor modelo
Utilizando los mejores parámetros de cada máquina, se utiliza el conjunto de validación para seleccionar el mejor método. Se tiene entonces las siguientes 4 máquinas:

Árbol de clasificación con profundidad 16
Random forest con profunidad 16 y 60 estimadores
Adaboost con profundidad 16 y 60
SVM lineal con kernel RBF y parámetro C=0.3
Can you write Python code for it?

print(train_tree(16, Xtrain, Ytrain, Xvalidation, Yvalidation)[3])
print(train_random_forest(60, 16, Xtrain, Ytrain, Xvalidation, Yvalidation)[3])
print(train_ada_boost(60, 16, Xtrain, Ytrain, Xvalidation, Yvalidation)[3])
print(train_svm(0.3, Xtrain, Ytrain, Xvalidation, Yvalidation)[3])


I am working on the file "T3/Pregunta4.ipynb".
The first task is:
Con procesamiento
Can you write Python code for it?

x_tr_scaled = center_and_scale(x_tr)
x_t_scaled = center_and_scale(x_t)
x_v_scaled = center_and_scale(x_v)

params = np.arange(1, 30, 1)
graph_tree_range(params, x_tr_scaled, y_tr, x_v_scaled, y_v)


I am working on the file "T3/Pregunta4.ipynb".
The first task is:
Con un accuracy de validación igual a 0.4204
Red neuronal 2
Esta red corresponde a una serie de capas relu con una cantidad de neuronas decreciente y qu siguen potencias de 2. Además, utiliza el mismo optimizador que la red anterior
Can you write Python code for it?

from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD
from keras.utils.np_utils import to_categorical

# Creación de red secuencial
model2 = Sequential()
model2.add(Dense(512, input_dim=x_tr.shape[1], init='uniform', activation='relu'))
model2.add(Dense(256, init='uniform', activation='relu'))
model2.add(Dense(128, init='uniform', activation='relu'))
model2.add(Dense(64, init='uniform', activation='relu'))
model2.add(Dense(32, init='uniform', activation='relu'))
model2.add(Dense(25, init='uniform', activation='softmax'))
model2.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model2.fit(x_tr.values, to_categorical(y_tr), nb_epoch=100, batch_size=128, verbose=1,
validation_data=(x_v.values,to_categorical(y_v)))


I am working on the file "T3/Pregunta4.ipynb".
The first task is:
Con un accuracy de validación igual a 0.7837
Por tanto, la mejor red neuronal corresponde a la segunda
Matriz de confusión
La matriz de confusión de la red neuronal obtenida en el tercer modelo corresponde a:
Can you write Python code for it?

import itertools
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
y_t_predict = model2.predict_classes(x_t.values)
cm = confusion_matrix(y_t, y_t_predict)

plt.figure(figsize=(15, 15))
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
plt.colorbar()
plt.show()


I am working on the file "T3/Pregunta4.ipynb".
The first task is:
Con un accuracy de validación igual a 0.9341
Red neuronal 3
La misma red neuronal que en 2, pero con menos épocas (60).
Can you write Python code for it?

from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD
from keras.utils.np_utils import to_categorical

# Creación de red secuencial
model3 = Sequential()
model3.add(Dense(512, input_dim=x_tr.shape[1], init='uniform', activation='relu'))
model3.add(Dense(256, init='uniform', activation='relu'))
model3.add(Dense(128, init='uniform', activation='relu'))
model3.add(Dense(64, init='uniform', activation='relu'))
model3.add(Dense(32, init='uniform', activation='relu'))
model3.add(Dense(25, init='uniform', activation='softmax'))
model3.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model3.fit(x_tr.values, to_categorical(y_tr), nb_epoch=60, batch_size=128, verbose=1,
validation_data=(x_v.values,to_categorical(y_v)))


I am working on the file "T3/Pregunta4.ipynb".
The first task is:
Diseño de redes neuronales
A continuación se presentan propuestas de diseño de redes neuronales para el problema y se selecciona aquel con mejor accuracy sobre el conjunto de validación
Red neuronal 1
Esta red neuronal consiste en la misma red dada en el enunciado, pero que utilizar rmsProps como optimizador
Can you write Python code for it?

from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD
from keras.utils.np_utils import to_categorical

# Creación de red secuencial
model1 = Sequential()
model1.add(Dense(30, input_dim=x_tr.shape[1], init='uniform', activation='relu'))
model1.add(Dense(30, init='uniform', activation='relu'))
model1.add(Dense(25, init='uniform', activation='softmax'))
model1.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model1.fit(x_tr.values, to_categorical(y_tr), nb_epoch=100, batch_size=128, verbose=1,
validation_data=(x_v.values,to_categorical(y_v)))