I am working on the file "04_training_linear_models.ipynb".
The first task is:
And that's it! The Softmax model is trained. Let's look at the model parameters:
Can you write Python code for it?


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Because of the additional $\ell_2$ penalty, the loss seems greater than earlier, but perhaps this model will perform better? Let's find out:
Can you write Python code for it?

logits = X_valid.dot(Theta)
Y_proba = softmax(logits)
y_predict = np.argmax(Y_proba, axis=1)

accuracy_score = np.mean(y_predict == y_valid)
accuracy_score


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Cool, perfect accuracy! We probably just got lucky with this validation set, but still, it's pleasant.
Now let's add early stopping. For this we just need to measure the loss on the validation set at every iteration and stop when the error starts growing.
Can you write Python code for it?

eta = 0.1
n_iterations = 5001
m = len(X_train)
epsilon = 1e-7
alpha = 0.1  # regularization hyperparameter
best_loss = np.infty

Theta = np.random.randn(n_inputs, n_outputs)

for iteration in range(n_iterations):
    logits = X_train.dot(Theta)
    Y_proba = softmax(logits)
    xentropy_loss = -np.mean(np.sum(Y_train_one_hot * np.log(Y_proba + epsilon), axis=1))
    l2_loss = 1/2 * np.sum(np.square(Theta[1:]))
    loss = xentropy_loss + alpha * l2_loss
    error = Y_proba - Y_train_one_hot
    gradients = 1/m * X_train.T.dot(error) + np.r_[np.zeros([1, n_outputs]), alpha * Theta[1:]]
    Theta = Theta - eta * gradients

    logits = X_valid.dot(Theta)
    Y_proba = softmax(logits)
    xentropy_loss = -np.mean(np.sum(Y_valid_one_hot * np.log(Y_proba + epsilon), axis=1))
    l2_loss = 1/2 * np.sum(np.square(Theta[1:]))
    loss = xentropy_loss + alpha * l2_loss
    if iteration % 500 == 0:
        print(iteration, loss)
    if loss < best_loss:
        best_loss = loss
    else:
        print(iteration - 1, best_loss)
        print(iteration, loss, "early stopping!")
        break

logits = X_valid.dot(Theta)
Y_proba = softmax(logits)
y_predict = np.argmax(Y_proba, axis=1)

accuracy_score = np.mean(y_predict == y_valid)
accuracy_score


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Exercise solutions
1. to 11.
See appendix A.
12. Batch Gradient Descent with early stopping for Softmax Regression
(without using Scikit-Learn)
Let's start by loading the data. We will just reuse the Iris dataset we loaded earlier.
Can you write Python code for it?

X = iris["data"][:, (2, 3)]  # petal length, petal width
y = iris["target"]


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Let's test this function on the first 10 instances:
Can you write Python code for it?


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Linear regression using batch gradient descent
Can you write Python code for it?

eta = 0.1
n_iterations = 1000
m = 100
theta = np.random.randn(2,1)

for iteration in range(n_iterations):
    gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
    theta = theta - eta * gradients

theta

X_new_b.dot(theta)

theta_path_bgd = []

def plot_gradient_descent(theta, eta, theta_path=None):
    m = len(X_b)
    plt.plot(X, y, "b.")
    n_iterations = 1000
    for iteration in range(n_iterations):
        if iteration < 10:
            y_predict = X_new_b.dot(theta)
            style = "b-" if iteration > 0 else "r--"
            plt.plot(X_new, y_predict, style)
        gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
        theta = theta - eta * gradients
        if theta_path is not None:
            theta_path.append(theta)
    plt.xlabel("$x_1$", fontsize=18)
    plt.axis([0, 2, 0, 15])
    plt.title(r"$\eta = {}$".format(eta), fontsize=16)

np.random.seed(42)
theta = np.random.randn(2,1)  # random initialization

plt.figure(figsize=(10,4))
plt.subplot(131); plot_gradient_descent(theta, eta=0.02)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.subplot(132); plot_gradient_descent(theta, eta=0.1, theta_path=theta_path_bgd)
plt.subplot(133); plot_gradient_descent(theta, eta=0.5)

save_fig("gradient_descent_plot")
plt.show()

np.random.seed(42)
theta = np.random.randn(2,1)  # random initialization

plt.figure(figsize=(10,4))
plt.subplot(131); plot_gradient_descent(theta, eta=0.02)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.subplot(132); plot_gradient_descent(theta, eta=0.1, theta_path=theta_path_bgd)
plt.subplot(133); plot_gradient_descent(theta, eta=0.5)

save_fig("gradient_descent_plot")
plt.show()


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Linear regression using the Normal Equation
Can you write Python code for it?

import numpy as np

X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)

plt.plot(X, y, "b.")
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.axis([0, 2, 0, 15])
save_fig("generated_data_plot")
plt.show()

X_b = np.c_[np.ones((100, 1)), X]  # add x0 = 1 to each instance
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)

theta_best

X_new = np.array([[0], [2]])
X_new_b = np.c_[np.ones((2, 1)), X_new]  # add x0 = 1 to each instance
y_predict = X_new_b.dot(theta_best)
y_predict

plt.plot(X_new, y_predict, "r-")
plt.plot(X, y, "b.")
plt.axis([0, 2, 0, 15])
plt.show()


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Logistic regression
Can you write Python code for it?

t = np.linspace(-10, 10, 100)
sig = 1 / (1 + np.exp(-t))
plt.figure(figsize=(9, 3))
plt.plot([-10, 10], [0, 0], "k-")
plt.plot([-10, 10], [0.5, 0.5], "k:")
plt.plot([-10, 10], [1, 1], "k:")
plt.plot([0, 0], [-1.1, 1.1], "k-")
plt.plot(t, sig, "b-", linewidth=2, label=r"$\sigma(t) = \frac{1}{1 + e^{-t}}$")
plt.xlabel("t")
plt.legend(loc="upper left", fontsize=20)
plt.axis([-10, 10, -0.1, 1.1])
save_fig("logistic_function_plot")
plt.show()

from sklearn import datasets
iris = datasets.load_iris()
list(iris.keys())

print(iris.DESCR)

X = iris["data"][:, 3:]  # petal width
y = (iris["target"] == 2).astype(np.int)  # 1 if Iris-Virginica, else 0

from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression(solver="liblinear", random_state=42)
log_reg.fit(X, y)

X_new = np.linspace(0, 3, 1000).reshape(-1, 1)
y_proba = log_reg.predict_proba(X_new)

plt.plot(X_new, y_proba[:, 1], "g-", linewidth=2, label="Iris-Virginica")
plt.plot(X_new, y_proba[:, 0], "b--", linewidth=2, label="Not Iris-Virginica")


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Looks good, so let's create the target class probabilities matrix for the training set and the test set:
Can you write Python code for it?

Y_train_one_hot = to_one_hot(y_train)
Y_valid_one_hot = to_one_hot(y_valid)
Y_test_one_hot = to_one_hot(y_test)


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Mini-batch gradient descent
Can you write Python code for it?

theta_path_mgd = []

n_iterations = 50
minibatch_size = 20

np.random.seed(42)
theta = np.random.randn(2,1)  # random initialization

t0, t1 = 10, 1000
def learning_schedule(t):
    return t0 / (t + t1)

t = 0
for epoch in range(n_iterations):
    shuffled_indices = np.random.permutation(m)
    X_b_shuffled = X_b[shuffled_indices]
    y_shuffled = y[shuffled_indices]
    for i in range(0, m, minibatch_size):
        t += 1
        xi = X_b_shuffled[i:i+minibatch_size]
        yi = y_shuffled[i:i+minibatch_size]
        gradients = 2 * xi.T.dot(xi.dot(theta) - yi)
        eta = learning_schedule(t)
        theta = theta - eta * gradients
        theta_path_mgd.append(theta)

theta

theta_path_bgd = np.array(theta_path_bgd)
theta_path_sgd = np.array(theta_path_sgd)
theta_path_mgd = np.array(theta_path_mgd)

plt.figure(figsize=(7,4))
plt.plot(theta_path_sgd[:, 0], theta_path_sgd[:, 1], "r-s", linewidth=1, label="Stochastic")
plt.plot(theta_path_mgd[:, 0], theta_path_mgd[:, 1], "g-+", linewidth=2, label="Mini-batch")
plt.plot(theta_path_bgd[:, 0], theta_path_bgd[:, 1], "b-o", linewidth=3, label="Batch")
plt.legend(loc="upper left", fontsize=16)
plt.xlabel(r"$\theta_0$", fontsize=20)
plt.ylabel(r"$\theta_1$   ", fontsize=20, rotation=0)
plt.axis([2.5, 4.5, 2.3, 3.9])
save_fig("gradient_descent_paths_plot")
plt.show()


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Note: the first releases of the book implied that the LinearRegression class was based on the Normal Equation. This was an error, my apologies: as explained above, it is based on the pseudoinverse, which ultimately relies on the SVD matrix decomposition of $\mathbf{X}$ (see chapter 8 for details about the SVD decomposition). Its time complexity is $O(n^2)$ and it works even when $m < n$ or when some features are linear combinations of other features (in these cases, $\mathbf{X}^T \mathbf{X}$ is not invertible so the Normal Equation fails), see issue #184 for more details. However, this does not change the rest of the description of the LinearRegression class, in particular, it is based on an analytical solution, it does not scale well with the number of features, it scales linearly with the number of instances, all the data must fit in memory, it does not require feature scaling and the order of the instances in the training set does not matter.
Linear regression using batch gradient descent
Can you write Python code for it?

eta = 0.1
n_iterations = 1000
m = 100
theta = np.random.randn(2,1)

for iteration in range(n_iterations):
    gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
    theta = theta - eta * gradients

theta

X_new_b.dot(theta)

theta_path_bgd = []

def plot_gradient_descent(theta, eta, theta_path=None):
    m = len(X_b)
    plt.plot(X, y, "b.")
    n_iterations = 1000
    for iteration in range(n_iterations):
        if iteration < 10:
            y_predict = X_new_b.dot(theta)
            style = "b-" if iteration > 0 else "r--"
            plt.plot(X_new, y_predict, style)
        gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
        theta = theta - eta * gradients
        if theta_path is not None:
            theta_path.append(theta)
    plt.xlabel("$x_1$", fontsize=18)
    plt.axis([0, 2, 0, 15])
    plt.title(r"$\eta = {}$".format(eta), fontsize=16)

np.random.seed(42)
theta = np.random.randn(2,1)  # random initialization

plt.figure(figsize=(10,4))
plt.subplot(131); plot_gradient_descent(theta, eta=0.02)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.subplot(132); plot_gradient_descent(theta, eta=0.1, theta_path=theta_path_bgd)
plt.subplot(133); plot_gradient_descent(theta, eta=0.5)

save_fig("gradient_descent_plot")
plt.show()


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Now here comes the hardest part: training! Theoretically, it's simple: it's just a matter of translating the math equations into Python code. But in practice, it can be quite tricky: in particular, it's easy to mix up the order of the terms, or the indices. You can even end up with code that looks like it's working but is actually not computing exactly the right thing. When unsure, you should write down the shape of each term in the equation and make sure the corresponding terms in your code match closely. It can also help to evaluate each term independently and print them out. The good news it that you won't have to do this everyday, since all this is well implemented by Scikit-Learn, but it will help you understand what's going on under the hood.
So the equations we will need are the cost function:
$J(\mathbf{\Theta}) =
- \dfrac{1}{m}\sum\limits_{i=1}^{m}\sum\limits_{k=1}^{K}{y_k^{(i)}\log\left(\hat{p}_k^{(i)}\right)}$
And the equation for the gradients:
$\nabla_{\mathbf{\theta}^{(k)}} \, J(\mathbf{\Theta}) = \dfrac{1}{m} \sum\limits_{i=1}^{m}{ \left ( \hat{p}^{(i)}_k - y_k^{(i)} \right ) \mathbf{x}^{(i)}}$
Note that $\log\left(\hat{p}_k^{(i)}\right)$ may not be computable if $\hat{p}_k^{(i)} = 0$. So we will add a tiny value $\epsilon$ to $\log\left(\hat{p}_k^{(i)}\right)$ to avoid getting nan values.
Can you write Python code for it?

eta = 0.01
n_iterations = 5001
m = len(X_train)
epsilon = 1e-7

Theta = np.random.randn(n_inputs, n_outputs)

for iteration in range(n_iterations):
    logits = X_train.dot(Theta)
    Y_proba = softmax(logits)
    loss = -np.mean(np.sum(Y_train_one_hot * np.log(Y_proba + epsilon), axis=1))
    error = Y_proba - Y_train_one_hot
    if iteration % 500 == 0:
        print(iteration, loss)
    gradients = 1/m * X_train.T.dot(error)
    Theta = Theta - eta * gradients


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Now let's implement the Softmax function. Recall that it is defined by the following equation:
$\sigma\left(\mathbf{s}(\mathbf{x})\right)k = \dfrac{\exp\left(s_k(\mathbf{x})\right)}{\sum\limits{j=1}^{K}{\exp\left(s_j(\mathbf{x})\right)}}$
Can you write Python code for it?

def softmax(logits):
    exps = np.exp(logits)
    exp_sums = np.sum(exps, axis=1, keepdims=True)
    return exps / exp_sums


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Polynomial regression
Can you write Python code for it?

import numpy as np
import numpy.random as rnd

np.random.seed(42)

m = 100
X = 6 * np.random.rand(m, 1) - 3
y = 0.5 * X**2 + X + 2 + np.random.randn(m, 1)

plt.plot(X, y, "b.")
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.axis([-3, 3, 0, 10])
save_fig("quadratic_data_plot")
plt.show()

from sklearn.preprocessing import PolynomialFeatures
poly_features = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly_features.fit_transform(X)
X[0]

X_poly[0]

lin_reg = LinearRegression()
lin_reg.fit(X_poly, y)
lin_reg.intercept_, lin_reg.coef_

X_new=np.linspace(-3, 3, 100).reshape(100, 1)
X_new_poly = poly_features.transform(X_new)
y_new = lin_reg.predict(X_new_poly)
plt.plot(X, y, "b.")
plt.plot(X_new, y_new, "r-", linewidth=2, label="Predictions")
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.legend(loc="upper left", fontsize=14)
plt.axis([-3, 3, 0, 10])
save_fig("quadratic_predictions_plot")
plt.show()

from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

for style, width, degree in (("g-", 1, 300), ("b--", 2, 2), ("r-+", 2, 1)):
    polybig_features = PolynomialFeatures(degree=degree, include_bias=False)
    std_scaler = StandardScaler()
    lin_reg = LinearRegression()
    polynomial_regression = Pipeline([
            ("poly_features", polybig_features),
            ("std_scaler", std_scaler),
            ("lin_reg", lin_reg),
        ])
    polynomial_regression.fit(X, y)
    y_newbig = polynomial_regression.predict(X_new)
    plt.plot(X_new, y_newbig, style, label=str(degree), linewidth=width)

plt.plot(X, y, "b.", linewidth=3)
plt.legend(loc="upper left")
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.axis([-3, 3, 0, 10])
save_fig("high_degree_polynomials_plot")
plt.show()

from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

def plot_learning_curves(model, X, y):
    X_train, X_val, y_train, y_val = train_test_split(X,...


import numpy as np
import numpy.random as rnd

np.random.seed(42)

m = 100
X = 6 * np.random.rand(m, 1) - 3
y = 0.5 * X**2 + X + 2 + np.random.randn(m, 1)

plt.plot(X, y, "b.")
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.axis([-3, 3, 0, 10])
save_fig("quadratic_data_plot")
plt.show()

from sklearn.preprocessing import PolynomialFeatures
poly_features = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly_features.fit_transform(X)
X[0]

X_poly[0]

lin_reg = LinearRegression()
lin_reg.fit(X_poly, y)
lin_reg.intercept_, lin_reg.coef_

X_new=np.linspace(-3, 3, 100).reshape(100, 1)
X_new_poly = poly_features.transform(X_new)
y_new = lin_reg.predict(X_new_poly)
plt.plot(X, y, "b.")
plt.plot(X_new, y_new, "r-", linewidth=2, label="Predictions")
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.legend(loc="upper left", fontsize=14)
plt.axis([-3, 3, 0, 10])
save_fig("quadratic_predictions_plot")
plt.show()

from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

for style, width, degree in (("g-", 1, 300), ("b--", 2, 2), ("r-+", 2, 1)):
    polybig_features = PolynomialFeatures(degree=degree, include_bias=False)
    std_scaler = StandardScaler()
    lin_reg = LinearRegression()
    polynomial_regression = Pipeline((
            ("poly_features", polybig_features),
            ("std_scaler", std_scaler),
            ("lin_reg", lin_reg),
        ))
    polynomial_regression.fit(X, y)
    y_newbig = polynomial_regression.predict(X_new)
    plt.plot(X_new, y_newbig, style, label=str(degree), linewidth=width)

plt.plot(X, y, "b.", linewidth=3)
plt.legend(loc="upper left")
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.axis([-3, 3, 0, 10])
save_fig("high_degree_polynomials_plot")
plt.show()

from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

def plot_learning_curves(model, X, y):
    X_train, X_val, y_train, y_val = train_test_split(X,...


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Regularized models
Can you write Python code for it?

from sklearn.linear_model import Ridge

np.random.seed(42)
m = 20
X = 3 * np.random.rand(m, 1)
y = 1 + 0.5 * X + np.random.randn(m, 1) / 1.5
X_new = np.linspace(0, 3, 100).reshape(100, 1)

def plot_model(model_class, polynomial, alphas, **model_kargs):
    for alpha, style in zip(alphas, ("b-", "g--", "r:")):
        model = model_class(alpha, **model_kargs) if alpha > 0 else LinearRegression()
        if polynomial:
            model = Pipeline([
                    ("poly_features", PolynomialFeatures(degree=10, include_bias=False)),
                    ("std_scaler", StandardScaler()),
                    ("regul_reg", model),
                ])
        model.fit(X, y)
        y_new_regul = model.predict(X_new)
        lw = 2 if alpha > 0 else 1
        plt.plot(X_new, y_new_regul, style, linewidth=lw, label=r"$\alpha = {}$".format(alpha))
    plt.plot(X, y, "b.", linewidth=3)
    plt.legend(loc="upper left", fontsize=15)
    plt.xlabel("$x_1$", fontsize=18)
    plt.axis([0, 3, 0, 4])

plt.figure(figsize=(8,4))
plt.subplot(121)
plot_model(Ridge, polynomial=False, alphas=(0, 10, 100), random_state=42)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.subplot(122)
plot_model(Ridge, polynomial=True, alphas=(0, 10**-5, 1), random_state=42)

save_fig("ridge_regression_plot")
plt.show()

from sklearn.linear_model import Ridge
ridge_reg = Ridge(alpha=1, solver="cholesky", random_state=42)
ridge_reg.fit(X, y)
ridge_reg.predict([[1.5]])

sgd_reg = SGDRegressor(max_iter=50, tol=-np.infty, penalty="l2", random_state=42)
sgd_reg.fit(X, y.ravel())
sgd_reg.predict([[1.5]])

ridge_reg = Ridge(alpha=1, solver="sag", random_state=42)
ridge_reg.fit(X, y)
ridge_reg.predict([[1.5]])

from sklearn.linear_model import Lasso

plt.figure(figsize=(8,4))
plt.subplot(121)
plot_model(Lasso, polynomial=False, alphas=(0, 0.1, 1), random_state=42)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.subplot(122)
plot_model(Lasso, polynomial=True, alphas=(0, 10**-7, 1), tol=1, random_state=42)

save_fig("lasso_regression_plot")
plt.show()

from...


from sklearn.linear_model import Ridge

np.random.seed(42)
m = 20
X = 3 * np.random.rand(m, 1)
y = 1 + 0.5 * X + np.random.randn(m, 1) / 1.5
X_new = np.linspace(0, 3, 100).reshape(100, 1)

def plot_model(model_class, polynomial, alphas, **model_kargs):
    for alpha, style in zip(alphas, ("b-", "g--", "r:")):
        model = model_class(alpha, **model_kargs) if alpha > 0 else LinearRegression()
        if polynomial:
            model = Pipeline((
                    ("poly_features", PolynomialFeatures(degree=10, include_bias=False)),
                    ("std_scaler", StandardScaler()),
                    ("regul_reg", model),
                ))
        model.fit(X, y)
        y_new_regul = model.predict(X_new)
        lw = 2 if alpha > 0 else 1
        plt.plot(X_new, y_new_regul, style, linewidth=lw, label=r"$\alpha = {}$".format(alpha))
    plt.plot(X, y, "b.", linewidth=3)
    plt.legend(loc="upper left", fontsize=15)
    plt.xlabel("$x_1$", fontsize=18)
    plt.axis([0, 3, 0, 4])

plt.figure(figsize=(8,4))
plt.subplot(121)
plot_model(Ridge, polynomial=False, alphas=(0, 10, 100), random_state=42)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.subplot(122)
plot_model(Ridge, polynomial=True, alphas=(0, 10**-5, 1), random_state=42)

save_fig("ridge_regression_plot")
plt.show()

from sklearn.linear_model import Ridge
ridge_reg = Ridge(alpha=1, solver="cholesky", random_state=42)
ridge_reg.fit(X, y)
ridge_reg.predict([[1.5]])

sgd_reg = SGDRegressor(penalty="l2", random_state=42)
sgd_reg.fit(X, y.ravel())
sgd_reg.predict([[1.5]])

ridge_reg = Ridge(alpha=1, solver="sag", random_state=42)
ridge_reg.fit(X, y)
ridge_reg.predict([[1.5]])

from sklearn.linear_model import Lasso

plt.figure(figsize=(8,4))
plt.subplot(121)
plot_model(Lasso, polynomial=False, alphas=(0, 0.1, 1), random_state=42)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.subplot(122)
plot_model(Lasso, polynomial=True, alphas=(0, 10**-7, 1), tol=1, random_state=42)

save_fig("lasso_regression_plot")
plt.show()

from sklearn.linear_model import...


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Still perfect, but faster.
Now let's plot the model's predictions on the whole dataset:
Can you write Python code for it?

x0, x1 = np.meshgrid(
        np.linspace(0, 8, 500).reshape(-1, 1),
        np.linspace(0, 3.5, 200).reshape(-1, 1),
    )
X_new = np.c_[x0.ravel(), x1.ravel()]
X_new_with_bias = np.c_[np.ones([len(X_new), 1]), X_new]

logits = X_new_with_bias.dot(Theta)
Y_proba = softmax(logits)
y_predict = np.argmax(Y_proba, axis=1)

zz1 = Y_proba[:, 1].reshape(x0.shape)
zz = y_predict.reshape(x0.shape)

plt.figure(figsize=(10, 4))
plt.plot(X[y==2, 0], X[y==2, 1], "g^", label="Iris-Virginica")
plt.plot(X[y==1, 0], X[y==1, 1], "bs", label="Iris-Versicolor")
plt.plot(X[y==0, 0], X[y==0, 1], "yo", label="Iris-Setosa")

from matplotlib.colors import ListedColormap
custom_cmap = ListedColormap(['#fafab0','#9898ff','#a0faa0'])

plt.contourf(x0, x1, zz, cmap=custom_cmap, linewidth=5)
contour = plt.contour(x0, x1, zz1, cmap=plt.cm.brg)
plt.clabel(contour, inline=1, fontsize=12)
plt.xlabel("Petal length", fontsize=14)
plt.ylabel("Petal width", fontsize=14)
plt.legend(loc="upper left", fontsize=14)
plt.axis([0, 7, 0, 3.5])
plt.show()


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Stochastic Gradient Descent
Can you write Python code for it?

theta_path_sgd = []
m = len(X_b)
np.random.seed(42)

n_epochs = 50
t0, t1 = 5, 50  # learning schedule hyperparameters

def learning_schedule(t):
    return t0 / (t + t1)

theta = np.random.randn(2,1)  # random initialization

for epoch in range(n_epochs):
    for i in range(m):
        if epoch == 0 and i < 20:                    # not shown in the book
            y_predict = X_new_b.dot(theta)           # not shown
            style = "b-" if i > 0 else "r--"         # not shown
            plt.plot(X_new, y_predict, style)        # not shown
        random_index = np.random.randint(m)
        xi = X_b[random_index:random_index+1]
        yi = y[random_index:random_index+1]
        gradients = 2 * xi.T.dot(xi.dot(theta) - yi)
        eta = learning_schedule(epoch * m + i)
        theta = theta - eta * gradients
        theta_path_sgd.append(theta)                 # not shown

plt.plot(X, y, "b.")                                 # not shown
plt.xlabel("$x_1$", fontsize=18)                     # not shown
plt.ylabel("$y$", rotation=0, fontsize=18)           # not shown
plt.axis([0, 2, 0, 15])                              # not shown
save_fig("sgd_plot")                                 # not shown
plt.show()                                           # not shown

theta

from sklearn.linear_model import SGDRegressor
sgd_reg = SGDRegressor(n_iter=50, penalty=None, eta0=0.1, random_state=42)
sgd_reg.fit(X, y.ravel())

sgd_reg.intercept_, sgd_reg.coef_


I am working on the file "04_training_linear_models.ipynb".
The first task is:
The easiest option to split the dataset into a training set, a validation set and a test set would be to use Scikit-Learn's train_test_split() function, but the point of this exercise is to try understand the algorithms by implementing them manually. So here is one possible implementation:
Can you write Python code for it?

test_ratio = 0.2
validation_ratio = 0.2
total_size = len(X_with_bias)

test_size = int(total_size * test_ratio)
validation_size = int(total_size * validation_ratio)
train_size = total_size - test_size - validation_size

rnd_indices = np.random.permutation(total_size)

X_train = X_with_bias[rnd_indices[:train_size]]
y_train = y[rnd_indices[:train_size]]
X_valid = X_with_bias[rnd_indices[train_size:-test_size]]
y_valid = y[rnd_indices[train_size:-test_size]]
X_test = X_with_bias[rnd_indices[-test_size:]]
y_test = y[rnd_indices[-test_size:]]


I am working on the file "04_training_linear_models.ipynb".
The first task is:
The figure in the book actually corresponds to the following code, with a legend and axis labels:
Can you write Python code for it?

plt.plot(X_new, y_predict, "r-", linewidth=2, label="Predictions")
plt.plot(X, y, "b.")
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.legend(loc="upper left", fontsize=14)
plt.axis([0, 2, 0, 15])
save_fig("linear_model_predictions")
plt.show()

from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X, y)
lin_reg.intercept_, lin_reg.coef_

lin_reg.predict(X_new)


I am working on the file "04_training_linear_models.ipynb".
The first task is:
The figure in the book actually is actually a bit fancier:
Can you write Python code for it?

X_new = np.linspace(0, 3, 1000).reshape(-1, 1)
y_proba = log_reg.predict_proba(X_new)
decision_boundary = X_new[y_proba[:, 1] >= 0.5][0]

plt.figure(figsize=(8, 3))
plt.plot(X[y==0], y[y==0], "bs")
plt.plot(X[y==1], y[y==1], "g^")
plt.plot([decision_boundary, decision_boundary], [-1, 2], "k:", linewidth=2)
plt.plot(X_new, y_proba[:, 1], "g-", linewidth=2, label="Iris-Virginica")
plt.plot(X_new, y_proba[:, 0], "b--", linewidth=2, label="Not Iris-Virginica")
plt.text(decision_boundary+0.02, 0.15, "Decision  boundary", fontsize=14, color="k", ha="center")
plt.arrow(decision_boundary, 0.08, -0.3, 0, head_width=0.05, head_length=0.1, fc='b', ec='b')
plt.arrow(decision_boundary, 0.92, 0.3, 0, head_width=0.05, head_length=0.1, fc='g', ec='g')
plt.xlabel("Petal width (cm)", fontsize=14)
plt.ylabel("Probability", fontsize=14)
plt.legend(loc="center left", fontsize=14)
plt.axis([0, 3, -0.02, 1.02])
save_fig("logistic_regression_plot")
plt.show()

decision_boundary

log_reg.predict([[1.7], [1.5]])

from sklearn.linear_model import LogisticRegression

X = iris["data"][:, (2, 3)]  # petal length, petal width
y = (iris["target"] == 2).astype(np.int)

log_reg = LogisticRegression(C=10**10, random_state=42)
log_reg.fit(X, y)

x0, x1 = np.meshgrid(
        np.linspace(2.9, 7, 500).reshape(-1, 1),
        np.linspace(0.8, 2.7, 200).reshape(-1, 1),
    )
X_new = np.c_[x0.ravel(), x1.ravel()]

y_proba = log_reg.predict_proba(X_new)

plt.figure(figsize=(10, 4))
plt.plot(X[y==0, 0], X[y==0, 1], "bs")
plt.plot(X[y==1, 0], X[y==1, 1], "g^")

zz = y_proba[:, 1].reshape(x0.shape)
contour = plt.contour(x0, x1, zz, cmap=plt.cm.brg)

left_right = np.array([2.9, 7])
boundary = -(log_reg.coef_[0][0] * left_right + log_reg.intercept_[0]) / log_reg.coef_[0][1]

plt.clabel(contour, inline=1, fontsize=12)
plt.plot(left_right, boundary, "k--", linewidth=3)
plt.text(3.5, 1.5, "Not Iris-Virginica", fontsize=14, color="b", ha="center")
plt.text(6.5, 2.3, "Iris-Virginica", fontsize=14, color="g", ha="center")
plt.xlabel("Petal length",...


I am working on the file "04_training_linear_models.ipynb".
The first task is:
The targets are currently class indices (0, 1 or 2), but we need target class probabilities to train the Softmax Regression model. Each instance will have target class probabilities equal to 0.0 for all classes except for the target class which will have a probability of 1.0 (in other words, the vector of class probabilities for ay given instance is a one-hot vector). Let's write a small function to convert the vector of class indices into a matrix containing a one-hot vector for each instance:
Can you write Python code for it?

def to_one_hot(y):
    n_classes = y.max() + 1
    m = len(y)
    Y_one_hot = np.zeros((m, n_classes))
    Y_one_hot[np.arange(m), y] = 1
    return Y_one_hot


I am working on the file "04_training_linear_models.ipynb".
The first task is:
This function computes $\mathbf{X}^+\mathbf{y}$, where $\mathbf{X}^{+}$ is the pseudoinverse of $\mathbf{X}$ (specifically the Moore-Penrose inverse). You can use np.linalg.pinv() to compute the pseudoinverse directly:
Can you write Python code for it?


I am working on the file "04_training_linear_models.ipynb".
The first task is:
We are almost ready to start training. Let's define the number of inputs and outputs:
Can you write Python code for it?

n_inputs = X_train.shape[1] # == 3 (2 features plus the bias term)
n_outputs = len(np.unique(y_train))   # == 3 (3 iris classes)


I am working on the file "04_training_linear_models.ipynb".
The first task is:
We need to add the bias term for every instance ($x_0 = 1$):
Can you write Python code for it?

X_with_bias = np.c_[np.ones([len(X), 1]), X]


I am working on the file "04_training_linear_models.ipynb".
The first task is:
Well, this model looks pretty good. For the sake of the exercise, let's add a bit of $\ell_2$ regularization. The following training code is similar to the one above, but the loss now has an additional $\ell_2$ penalty, and the gradients have the proper additional term (note that we don't regularize the first element of Theta since this corresponds to the bias term). Also, let's try increasing the learning rate eta.
Can you write Python code for it?

eta = 0.1
n_iterations = 5001
m = len(X_train)
epsilon = 1e-7
alpha = 0.1  # regularization hyperparameter

Theta = np.random.randn(n_inputs, n_outputs)

for iteration in range(n_iterations):
    logits = X_train.dot(Theta)
    Y_proba = softmax(logits)
    xentropy_loss = -np.mean(np.sum(Y_train_one_hot * np.log(Y_proba + epsilon), axis=1))
    l2_loss = 1/2 * np.sum(np.square(Theta[1:]))
    loss = xentropy_loss + alpha * l2_loss
    error = Y_proba - Y_train_one_hot
    if iteration % 500 == 0:
        print(iteration, loss)
    gradients = 1/m * X_train.T.dot(error) + np.r_[np.zeros([1, n_inputs]), alpha * Theta[1:]]
    Theta = Theta - eta * gradients


eta = 0.1
n_iterations = 5001
m = len(X_train)
epsilon = 1e-7
alpha = 0.1  # regularization hyperparameter

Theta = np.random.randn(n_inputs, n_outputs)

for iteration in range(n_iterations):
    logits = X_train.dot(Theta)
    Y_proba = softmax(logits)
    xentropy_loss = -np.mean(np.sum(Y_train_one_hot * np.log(Y_proba + epsilon), axis=1))
    l2_loss = 1/2 * np.sum(np.square(Theta[1:]))
    loss = xentropy_loss + alpha * l2_loss
    error = Y_proba - Y_train_one_hot
    if iteration % 500 == 0:
        print(iteration, loss)
    gradients = 1/m * X_train.T.dot(error) + np.r_[np.zeros([1, n_outputs]), alpha * Theta[1:]]
    Theta = Theta - eta * gradients


I am working on the file "04_word2vec.ipynb".
The first task is:
2. 사전 설정
* model
* loss
* opimizer
Can you write Python code for it?

class _model(nn.Module) :
    def __init__(self):
        super(_model, self).__init__()
        self.embedding = nn.Embedding(len(word_list), 2)
        self.linear = nn.Linear(2, len(word_list), bias=True)

    def forward(self, x):
        x = self.embedding(x)
        x = self.linear(x)
        return F.log_softmax(x)

model = _model()
loss_fn = nn.NLLLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)


I am working on the file "04_word2vec.ipynb".
The first task is:
3. Trainning loop
* (입력 생성)
* model 생성
* loss 생성
* zeroGrad
* backpropagation
* optimizer step (update model parameter)
Can you write Python code for it?

model.train()
for epoch in range(100):
    for data, target in train_loader:
        data, target = Variable(data), Variable(target) #(입력 생성)
        output = model(data) # model 생성
        loss = F.nll_loss(output, target) #loss 생성
        optimizer.zero_grad() # zeroGrad
        loss.backward() # calc backward gradients
        optimizer.step() # update parameters


I am working on the file "04_word2vec.ipynb".
The first task is:
4. Predict & Evaluate
Can you write Python code for it?

model.eval()

invDic = { i : w for w, i in word_dict.items()}
print('Input : true : pred')

for x, y in test_loader :
    x, y = Variable(x.squeeze()), y.squeeze()
    y_pred = model(x).max(1)[1].data[0][0]
    print('{:s} : {:s} : {:s}'.format(invDic[x.data[0]], invDic[y[0]], invDic[y_pred]))


I am working on the file "04_word2vec.ipynb".
The first task is:
5. plot embedding space
Can you write Python code for it?

import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
matplotlib.rc('font', family="NanumGothic")

for i in label :
    x = Variable(torch.LongTensor([i]))
    fx, fy = model.embedding(x).squeeze().data
    plt.scatter(fx, fy)
    plt.annotate(invDic[i], xy=(fx, fy), xytext=(5, 2),
                 textcoords='offset points', ha='right', va='bottom')


I am working on the file "04_word2vec.ipynb".
The first task is:
Dataset Loader 설정
Can you write Python code for it?

label = torch.LongTensor(skip_grams)[:, 0].contiguous()
context = torch.LongTensor(skip_grams)[:, 1].contiguous()
skip_grams_dataset = data_utils.TensorDataset(label, context)
train_loader = torch.utils.data.DataLoader(skip_grams_dataset, batch_size=8, shuffle=True)
test_loader = torch.utils.data.DataLoader(skip_grams_dataset, batch_size=1, shuffle=False)


I am working on the file "04a_CorrectiveSolutions.ipynb".
The first task is:
Compute H matrix
Can you write Python code for it?


I am working on the file "04a_CorrectiveSolutions.ipynb".
The first task is:
Compute eigenvalues and eigenvectors as:
$$\left| \mathbf{I} \cdot \beta^2 - \mathbf{\tilde{K}}^{-1} \cdot \mathbf{\tilde{M}} \right| = 0$$
We substitute some numerical values to simplify the expressions
Can you write Python code for it?


I am working on the file "04a_CorrectiveSolutions.ipynb".
The first task is:
Define section and perform first calculations
Can you write Python code for it?

S1 = Section(stringers, panels)

S1.cycles


I am working on the file "04a_CorrectiveSolutions.ipynb".
The first task is:
Define sympy parameters used for geometric description of sections
Can you write Python code for it?

A, A0, t, t0, a, b, h, L, E, G = sympy.symbols('A A_0 t t_0 a b h L E G', positive=True)


I am working on the file "04a_CorrectiveSolutions.ipynb".
The first task is:
First example: Simple rectangular symmetric section
Define graph describing the section:
1) stringers are nodes with parameters:
- x coordinate
- y coordinate
- Area
2) panels are oriented edges with parameters:
- thickness
- lenght which is automatically calculated
Can you write Python code for it?


I am working on the file "04a_CorrectiveSolutions.ipynb".
The first task is:
Import Section class, which contains all calculations
Can you write Python code for it?


I am working on the file "04a_CorrectiveSolutions.ipynb".
The first task is:
Initialization of sympy symbolic tool and pint for dimension analysis (not really implemented rn as not directly compatible with sympy)
Can you write Python code for it?

ureg = UnitRegistry()
sympy.init_printing()


I am working on the file "04a_CorrectiveSolutions.ipynb".
The first task is:
Plot of S1 section in inertial reference Frame
Section is plotted wrt center of gravity and rotated (if necessary) so that x and y are principal axes.
Center of Gravity and Shear Center are drawn
Can you write Python code for it?

positions={ii: [float(S1.g.node[ii]['pos'][i].subs(datav)) for i in range(2)] for ii in S1.g.nodes() }

x_ct, y_ct = S1.ct.subs(datav)

plt.figure(figsize=(12,8),dpi=300)
nx.draw(S1.g,with_labels=True, pos=positions)
plt.plot([0],[0],'o',ms=12,label='CG')
plt.plot([x_ct],[y_ct],'^',ms=12, label='SC')
#plt.text(0,0, 'CG', fontsize=24)
#plt.text(x_ct,y_ct, 'SC', fontsize=24)
plt.legend(loc='lower right', shadow=True)
plt.axis('equal')
plt.title("Section in pricipal reference Frame",fontsize=16);


I am working on the file "04a_CorrectiveSolutions.ipynb".
The first task is:
Plot of S1 section in original reference frame
Define a dictionary of coordinates used by Networkx to plot section as a Directed graph.
Note that arrows are actually just thicker stubs
Can you write Python code for it?

start_pos={ii: [float(S1.g.node[ii]['ip'][i].subs(datav)) for i in range(2)] for ii in S1.g.nodes() }

plt.figure(figsize=(12,8),dpi=300)
nx.draw(S1.g,with_labels=True, arrows= True, pos=start_pos)
plt.arrow(0,0,20,0)
plt.arrow(0,0,0,20)
#plt.text(0,0, 'CG', fontsize=24)
plt.axis('equal')
plt.title("Section in starting reference Frame",fontsize=16);


I am working on the file "04a_CorrectiveSolutions.ipynb".
The first task is:
Standard Solution
Can you write Python code for it?

Tx, Ty, Nz, Mx, My, Mz, F, ry, ry, mz = sympy.symbols('T_x T_y N_z M_x M_y M_z F r_y r_x m_z')

S1.set_loads(_Tx=0, _Ty=Ty, _Nz=0, _Mx=Mx, _My=0, _Mz=Mz)


I am working on the file "04a_exploratory_pandas.ipynb".
The first task is:
Converting all NaNs to 0.0
Can you write Python code for it?

np.where(np.isnan(df['value']), 0.0, df['value'])
df[np.isnan(df['value'])]


I am working on the file "04a_exploratory_pandas.ipynb".
The first task is:
Converting the value column to floats

Pandas is good in dealing with missing values
Assigns a NaN to missing values
Can you write Python code for it?

df['value'] = df['value'].convert_objects(convert_numeric='force')
df.dtypes

df[np.isnan(df['value'])]


I am working on the file "04a_exploratory_pandas.ipynb".
The first task is:
Doing PCA on the error matrix to reduce dimensionality
Can you write Python code for it?

from sklearn.preprocessing import scale
from sklearn.decomposition import PCA

A = metrics.as_matrix()
A.shape

A[np.where(np.isnan(A))] = 0.0

B = scale(A)
pca = PCA().fit(B)

B_reduced = pca.transform(B)

pca.explained_variance_ratio_[0:20]


I am working on the file "04a_exploratory_pandas.ipynb".
The first task is:
Pandas
Python module for manipulating tabular data
pandas

Provides python a DataFrame
Structured manipulation tools
Built on top of numpy
Huge growth from 2011-2012
Very efficient
Great for medium data

Resources

pandas.pydata.org
Python for Data Analysis by Wes McKinney
Data Wrangling Kung Fu with Pandas by Wes McKinney
Cheat sheet by Quandl

Why pandas?

80% of the effort in data analysis is spent cleaning data. Hadley Wickham

Efficency

Different views of data
Tidy data by Hadley Wickham

Raw data is often in the wrong format

How often to you download an array ready for array-oriented computing?
e.g. scikit-learn interface

Storage may be best in a different format

Sparse representations
Upload to database

Converting a Lustre Debug File into a Pandas Data Frame
Debug file contains the following columns
- 0-1 describe subsystem ID
- 2-3 timestamp
- 4-6 PIDs
- 7 relevant code module
- 8 code line
- 9 function and message
Steps we are taking:
- Read the file
- Add the column names
- print the first few elements
Can you write Python code for it?

log_filename = 'lustre_debug.out'
log_df = pd.read_csv(log_filename, sep=':', names=['debug mask', 'subsys', 't1', 't2', 'pid1', 'pid2',
                                                   'code', 'func', 'line', 'message1', 'message2'])
log_df.head()

log_df.dtypes


I am working on the file "04a_exploratory_pandas.ipynb".
The first task is:
Ratio of lines containing the mfd to all lines
Can you write Python code for it?


I am working on the file "04a_exploratory_pandas.ipynb".
The first task is:
Reshaping the data

Columns are the metric
Rows are timestamp and host
pivot_table function
Can you write Python code for it?

metrics = pd.pivot_table(df, index=['time stamp', 'host'], columns='metric', values='value')

metrics.head()

idx = pd.IndexSlice
metrics.loc[idx[:,'oss03'],:]


I am working on the file "04a_odvod.ipynb".
The first task is:
Lepši izpis tabele dobimo, če uporabimo knjižnico za delo s tabelami in podatki Pandas.
Can you write Python code for it?

from pandas import DataFrame
DataFrame(tabela,columns={"$f(x)$","$f'(x)$"})

# za lepši izpis uporabimo funkcijo latex
tabela =[['$$%s$$' % latex(f),'$$%s$$'% latex(diff(f,x))] for f in funkcije ]
DataFrame(tabela,columns={"funkcija $f(x)$","odvod $f'(x)$"})


I am working on the file "04a_odvod.ipynb".
The first task is:
Pravila za odvajanje
Sestavimo še tabelo pravil za odvajanje. Vključimo pravila za osnovne operacije in kompozitum.
Can you write Python code for it?

%%javascript
MathJax.Hub.Config({
    "HTML-CSS": { linebreaks: {automatic: false } }
  });
// preprečimo prelom vrstic v tabeli

f,g = symbols("f,g")
import pandas as pd
pd.options.display.max_colwidth=1000

operacije = [f(x)+g(x), f(x)*g(x), f(x)/g(x), f(x)**g(x),f(g(x))]
fmt = "$$%s$$"
tabela = [[fmt % latex(op),fmt % latex(simplify(diff(op,x)))] for op in operacije]
df_tabela = DataFrame(tabela, columns=["funkcija $f(x)$","pravilo za odvod $f'(x)$"],)
df_tabela


I am working on the file "04a_tangenta.ipynb".
The first task is:
<< nazaj: funkcije
Can you write Python code for it?

import disqus
%reload_ext disqus
%disqus matpy


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:

    EXERCISE: Add a column to the `cast` dataframe that indicates the number of roles for the film.

Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations21.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:

    EXERCISE: Calculate the ratio of leading actor and actress roles to the total number of leading roles per decade.

Tip: you can to do a groupby twice in two steps,  once calculating the numbers, and then the ratios.
Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations22.py

# %load snippets/04b - Advanced groupby operations23.py

# %load snippets/04b - Advanced groupby operations24.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:

    EXERCISE: How many leading (n=1) roles were available to actors, and how many to actresses, in the 1950s? And in 2000s?

Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations41.py

# %load snippets/04b - Advanced groupby operations42.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:

    EXERCISE: List each of the characters that Frank Oz has portrayed at least twice.

Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations15.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:

    EXERCISE: List the 10 actors/actresses that have the most leading roles (n=1) since the 1990's.

Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations11.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:

    EXERCISE: List the 10 movie titles with the longest name.

Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations31.py

# %load snippets/04b - Advanced groupby operations32.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:

    EXERCISE: Plot the number of "Hamlet" films made each decade.

Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations36.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:

    EXERCISE: Use groupby() to determine how many roles are listed for each of The Pink Panther movies.

Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations12.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:

    EXERCISE: Using groupby(), plot the number of films that have been released each decade in the history of cinema.

Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations8.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:

    EXERCISE: What are the 10 most film titles roles that start with the word "The Life"?

Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations40.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:

    EXERCISE: What are the 11 most common character names in movie history?

Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations37.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:
And now applying this on some real data
These exercises are based on the PyCon tutorial of Brandon Rhodes (so all credit to him!) and the datasets he prepared for that. You can download these data from here: titles.csv and cast.csv and put them in the /data folder.
cast dataset: different roles played by actors/actresses in films

title: title of the film
name: name of the actor/actress
type: actor/actress
n: the order of the role (n=1: leading role)
Can you write Python code for it?

cast = pd.read_csv('data/cast.csv')
cast.head()

titles = pd.read_csv('data/titles.csv')
titles.head()


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:
For an overview of all string methods, see: http://pandas.pydata.org/pandas-docs/stable/api.html#string-handling

    EXERCISE: We already plotted the number of 'Hamlet' films released each decade, but not all titles are exactly called 'Hamlet'. Give an overview of the titles that contain 'Hamlet', and that start with 'Hamlet':

Can you write Python code for it?

# %load snippets/04b - Advanced groupby operations29.py

# %load snippets/04b - Advanced groupby operations30.py


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:
Intermezzo: string manipulations
Python strings have a lot of useful methods available to manipulate or check the content of the string:
Can you write Python code for it?


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:
Recap: the groupby operation (split-apply-combine)
The "group by" concept: we want to apply the same function on subsets of your dataframe, based on some key to split the dataframe in subsets
This operation is also referred to as the "split-apply-combine" operation, involving the following steps:

Splitting the data into groups based on some criteria
Applying a function to each group independently
Combining the results into a data structure

<img src="img/splitApplyCombine.png">
Similar to SQL GROUP BY
The example of the image in pandas syntax:
Can you write Python code for it?


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:
Using the filtering and reductions operations we have seen in the previous notebooks, we could do something like:
df[df['key'] == "A"].sum()
df[df['key'] == "B"].sum()
...

But pandas provides the groupby method to do this:
Can you write Python code for it?

df.groupby('key').aggregate('sum')  # np.sum

df.groupby('key').sum()


I am working on the file "04b - Advanced groupby operations.ipynb".
The first task is:
Value counts
A useful shortcut to calculate the number of occurences of certain values is value_counts (this is somewhat equivalent to df.groupby(key).size()))
For example, what are the most occuring movie titles?
Can you write Python code for it?


I am working on the file "04b Writing a custom layer.ipynb".
The first task is:
Build your own layer
Instead of importing the neon supplied Affine Layer, we will instead build our own.
Note: Affine is actually a compound layer; it bundles a linear layer with a bias transform and an activation function.  The Linear layer is what implements a fully connected layer.
First, lets build our own linear layer, called MyLinear, and then we will wrap that layer in a compound layer MyAffine.
There are several important components to a layer in neon:
* configure: during model initialization, this layer will receive the previous layer's object and use it to set this model's in_shape and out_shape attributes.
* allocate: after each layer's shape is configured, this layer's shape information will be used to allocate memory for the output activations from fprop.
* fprop: forward propagation. Should return a tensor with shape equal to the layer's out_shape attribute.
* bprop: backward propagation.
In the implementation below, fprop is implemented using element-wise operations.  It will be very slow.  Try replacing it with the neon backend implementation of compound_dot, such as in the bprop function.
Can you write Python code for it?

from neon.layers.layer import ParameterLayer, interpret_in_shape

# Subclass from ParameterLayer, which handles the allocation
# of memory buffers for the output activations, weights, and
# bprop deltas.
class MyLinear(ParameterLayer):

    def __init__(self, nout, init, name=None):
        super(MyLinear, self).__init__(init, name, "Disabled")
        self.nout = nout

    def __str__(self):
        return "Linear Layer '%s': %d inputs, %d outputs" % (
               self.name, self.nin, self.nout)

    def configure(self, in_obj):
        super(MyLinear, self).configure(in_obj)

        # shape of the input is in (# input features, batch_size)
        (self.nin, self.nsteps) = interpret_in_shape(self.in_shape)

        # shape of the output is (# output units, batch_size)
        self.out_shape = (self.nout, self.nsteps)

        # if the shape of the weights have not been allocated,
        # we know that his layer's W is a tensor of shape (# outputs, # inputs).
        if self.weight_shape is None:
            self.weight_shape = (self.nout, self.nin)

        return self

    def fprop(self, inputs, inference=False, beta=0.0):
        self.inputs = inputs

        # here we compute y = W*X inefficiently using the backend functions
        for r in range(self.outputs.shape[0]):
            for c in range(self.outputs.shape[1]):
                self.outputs[r,c] = self.be.sum(self.W[r,:] * inputs[:,c].T)

        # TODO:
        # try substituting the for loops above with the backend `compound_dot`
        # function to see the speed-up from using a custom gpu kernel!
        # self.be.compound_dot(A=self.W, B=inputs, C=self.outputs)

        return self.outputs

    def bprop(self, error, alpha=1.0, beta=0.0):

        # to save you headache, we use the backend compound_dot function here to compute
        # the back-propogated deltas = W^T*error.
        if self.deltas:
            self.be.compound_dot(A=self.W.T, B=error, C=self.deltas,...


I am working on the file "04b Writing a custom layer.ipynb".
The first task is:
Fit
Using Cross Entropy loss and Gradient Descent optimizer, train the model. This will be slow, because our fprop is inefficient. Replace the fprop function using the backend's compound_dot method!
Can you write Python code for it?

from neon.layers import GeneralizedCost
from neon.transforms import CrossEntropyMulti
from neon.optimizers import GradientDescentMomentum
from neon.callbacks.callbacks import Callbacks

cost = GeneralizedCost(costfunc=CrossEntropyMulti())
optimizer = GradientDescentMomentum(0.1, momentum_coef=0.9)
callbacks = Callbacks(mlp, eval_set=test_set)

mlp.fit(train_set, optimizer=optimizer, num_epochs=10, cost=cost,
        callbacks=callbacks)


I am working on the file "04b_CS_loop.ipynb".
The first task is:
Autosolutions
Compute L matrix: with 6 nodes we expect 3 dofs, one with symmetric load and one with antisymmetric load
Can you write Python code for it?


I am working on the file "04b_CS_loop.ipynb".
The first task is:
Compute H matrix
Can you write Python code for it?


I am working on the file "04b_CS_loop.ipynb".
The first task is:
Compute eigenvalues and eigenvectors as:
$$\left| \mathbf{I} \cdot \beta^2 - \mathbf{\tilde{K}}^{-1} \cdot \mathbf{\tilde{M}} \right| = 0$$
We substitute some numerical values to simplify the expressions
Can you write Python code for it?


I am working on the file "04b_CS_loop.ipynb".
The first task is:
Define section and perform first calculations
Can you write Python code for it?

S1 = Section(stringers, panels)

S1.cycles


I am working on the file "04b_CS_loop.ipynb".
The first task is:
Eigenvalues correspond to $\beta^2$
Can you write Python code for it?


I am working on the file "04b_CS_loop.ipynb".
The first task is:
First example: Simple rectangular symmetric section
Define graph describing the section:
1) stringers are nodes with parameters:
- x coordinate
- y coordinate
- Area
2) panels are oriented edges with parameters:
- thickness
- lenght which is automatically calculated
Can you write Python code for it?


I am working on the file "04b_CS_loop.ipynb".
The first task is:
Import Section class, which contains all calculations
Can you write Python code for it?


I am working on the file "04b_CS_loop.ipynb".
The first task is:
Loads
Can you write Python code for it?

S1.compute_stringer_actions()
S1.compute_panel_fluxes();

S1.N

S1.q

S1.set_loads(_Tx=0, _Ty=Ty, _Nz=0, _Mx=Mx, _My=0, _Mz=0)

S1.compute_stringer_actions()
S1.compute_panel_fluxes();

S1.N

S1.q


I am working on the file "04ex-Buckling.ipynb".
The first task is:
Essai sur d'autres structures
Structure avec des piliers au milieu :
Can you write Python code for it?

nodes= np.array([[0.,0.],[0.,Lyb],[0.,Lyh+Lyb],[Lx/2,Lyh+Lyb],[Lx,Lyh+Lyb],[Lx,Lyb],[Lx/2,Lyh/2+Lyb],[Lx,0.]])
elements=np.array([[0,1],[1,5],[1,2],[2,3],[3,4],[4,5],[1,6],[2,6],[4,6],[5,6],[5,7]])

frame= Frame_Buckling(nodes,elements)
frame.plot_with_label()

ne = frame.nelements
ndof = frame.ndof
EI = np.ones(ne)*E*I
ES = np.ones(ne)*E*S
EI[1]=100*E*I;EI[3]=100*E*I;EI[4]=100*E*I
ES[1]=100*E*S;ES[3]=100*E*S;ES[4]=100*E*S
f_x = 0*np.ones(ne)
f_y = 0*np.ones(ne)
frame.set_distributed_loads(f_x, f_y)
frame.set_stiffness(EI, ES)
blocked_dof = np.array([0, 1, 2, ndof-3, ndof-2, ndof-1])
bc_values = np.array([0, 0, 0, 0, 0, 0])

K = frame.assemble_K()
F=frame.assemble_F()
#F[10]=F[10]-.5*EI[0]*np.pi**2/(Lyb+Lyh)**2
F[10]=F[10]-1.
Kbc, Fbc = frame.bc_apply(K, F, blocked_dof, bc_values)
Usol = np.linalg.solve(Kbc,Fbc)

frame.set_displacement(Usol)

Gbc=frame.assemble_G()
G=frame.bc_apply_G(Gbc,blocked_dof)

Ks = sparse.csr_matrix(K)
Gs = sparse.csr_matrix(G)
val, vect = linalg.eigsh(Ks, 3, Gs, which = 'LA', sigma =4.)
print(val)
print(vect[:,1])

frame.set_displacement(1*vect[:,0])
frame.plot_with_label()
frame.plot_displaced()


I am working on the file "04ex-Buckling.ipynb".
The first task is:
Essai sur exercice 2
Can you write Python code for it?

E=1.3 #en MPa
h=7.5 #en mm
b=20. #en mm
Lx=55. #en mm
Lyh=60. #en mm
Lyb=45. #en mm
I=b*(h**3)/12 #en mm^4
S=b*h  #en mm^2
eps=10**(-3)
g=9.81 #en m.s^(-2)
m=0.05 #en kg

nodes= np.array([[0.,0.],[0.,Lyb],[0.,Lyh+Lyb],[Lx/2,Lyh+Lyb],[Lx,Lyh+Lyb],[Lx,Lyb],[Lx,0.]])
elements=np.array([[0,1],[1,5],[1,2],[2,3],[3,4],[4,5],[5,6]])

frame= Frame_Buckling(nodes,elements)
frame.plot_with_label()

ne = frame.nelements
ndof = frame.ndof
EI = np.ones(ne)*E*I
ES = np.ones(ne)*E*S
EI[1]=100*E*I;EI[3]=100*E*I;EI[4]=100*E*I
ES[1]=100*E*S;ES[3]=100*E*S;ES[4]=100*E*S
f_x = 0*np.ones(7)
f_y = 0*np.ones(7)
frame.set_distributed_loads(f_x, f_y)
frame.set_stiffness(EI, ES)
blocked_dof = np.array([0, 1, 2, ndof-3, ndof-2, ndof-1])
bc_values = np.array([0, 0, 0, 0, 0, 0])

K = frame.assemble_K()
F=frame.assemble_F()
#F[10]=F[10]-.5*EI[0]*np.pi**2/(Lyb+Lyh)**2
F[10]=F[10]-1.
Kbc, Fbc = frame.bc_apply(K, F, blocked_dof, bc_values)
Usol = np.linalg.solve(Kbc,Fbc)
Usol

frame.set_displacement(Usol)

Ge=frame.N_local_stress_tot()

Gbc=frame.assemble_G()
G=frame.bc_apply_G(Gbc,blocked_dof)

Ks = sparse.csr_matrix(K)
Gs = sparse.csr_matrix(G)
val, vect = linalg.eigsh(Ks, 3, Gs, which = 'LA', sigma = 3.)
print(val)
print(vect[:,0])

frame.set_displacement(1*vect[:,0])
frame.plot_with_label()
frame.plot_displaced()


I am working on the file "04ex-Buckling.ipynb".
The first task is:
Structure croisée :
Can you write Python code for it?

nodes= np.array([[0.,0.],[0.,Lyb],[0.,Lyh+Lyb],[Lx/2,Lyh+Lyb],[Lx,Lyh+Lyb],[Lx,Lyb],[Lx,0.]])
elements=np.array([[0,1],[1,5],[1,2],[2,3],[3,4],[4,5],[2,5],[0,5],[5,6]])

frame= Frame_Buckling(nodes,elements)
frame.plot_with_label()

ne = frame.nelements
ndof = frame.ndof
EI = np.ones(ne)*E*I
ES = np.ones(ne)*E*S
f_x = 0*np.ones(ne)
f_y = 0*np.ones(ne)
frame.set_distributed_loads(f_x, f_y)
frame.set_stiffness(EI, ES)
blocked_dof = np.array([0, 1, 2, ndof-3, ndof-2, ndof-1])
bc_values = np.array([0, 0, 0, 0, 0, 0])

K = frame.assemble_K()
F=frame.assemble_F()
#F[7]=F[7]-.5*EI[0]*np.pi**2/(Lyb+Lyh)**2
#F[13]=F[13]-.5*EI[0]*np.pi**2/(Lyb+Lyh)**2
F[10]=F[10]-1.
Kbc, Fbc = frame.bc_apply(K, F, blocked_dof, bc_values)
Usol = np.linalg.solve(Kbc,Fbc)
Usol

frame.set_displacement(Usol)

Gbc=frame.assemble_G()
G=frame.bc_apply_G(Gbc,blocked_dof)

Ks = sparse.csr_matrix(K)
Gs = sparse.csr_matrix(G)
val, vect = linalg.eigsh(Ks, 3, Gs, which = 'LA', sigma =9.)
print(val)
frame.set_displacement(1*vect[:,0])
frame.plot_with_label()
frame.plot_displaced()


I am working on the file "05 - Cliques and Communities.ipynb".
The first task is:
This implementation of louvain modularity is a very smart piece of maths, first given by Blondel et al in Fast unfolding of communities in large networks. If you're not a mathematical reader, just skip over this section and go straight to the results.
If you are interested in the maths, it goes roughly like this:
We want to calculate a value Q between -1 and 1 for a partition of our graph, where $Q$ denotes the modularity of the network. Modularity is a comparative measure of the density within the communities in question and the density between them. A high modularity indicates a good splitting. Through successive, gradual changes to our labelling of nodes and close monitoring of the value of $Q$, we can optimise our partition(s). $Q$ and its change for each successve optimisation epoch ($\Delta Q$) are calculated in two stages, as follows.
$$
Q = \frac{1}{2m} \sum_{ij} \left[\ A_{ij}\ \frac{k_i k_j}{2m}\ \right]\ \delta(c_i, c_j)
$$

$m$ is the sum of all of the edge weights in the graph
$A_{ij}$ represents the edge weight between nodes $i$ and $j$
$k_{i}$ and $k_{j}$ are the sum of the weights of the edges attached to nodes $i$ and $j$, respectively
$\delta$ is the delta function.
$c_{i}$ and $c_{j}$ are the communities of the nodes

First, each node in the network is assigned to its own community. Then for each node $i$, the change in modularity is calculated by removing $i$ from its own community and moving it into the community of each neighbor $j$ of $i$:
$$
\Delta Q = \left[\frac{\sum_{in} +\ k_{i,in}}{2m} - \left(\frac{\sum_{tot} +\ k_{i}}{2m}\right)^2 \right] -
\left[ \frac{\sum_{in}}{2m} - \left(\frac{\sum_{tot}}{2m}\right)^2 - \left(\frac{k_{i}}{2m}\right)^2 \right]
$$

$\sum_{in}$ is sum of all the weights of the links inside the community $i$ is moving into
$k_{i,in}$ is the sum of the weights of the links between $i$ and other nodes in the community
$m$ is the sum of the weights of all links in the network
$\Sigma _{tot}$ is the sum of all the weights of the links to nodes in the...
Can you write Python code for it?

book = nx.from_pandas_dataframe(bookworm('data/raw/hp_chamber_of_secrets.txt'),
                                source='source',
                                target='target')

partitions = community.best_partition(book)
values = [partitions.get(node) for node in book.nodes()]

nx.draw(book,
        cmap=plt.get_cmap("RdYlBu"),
        node_color=values,
        with_labels=True)


I am working on the file "05 - Preprocessing and Pipelines.ipynb".
The first task is:
Cross-validated pipelines including scaling, we need to estimate mean and standard deviation separately for each fold.
To do that, we build a pipeline.
Can you write Python code for it?

from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler

standard_scaler = StandardScaler()
standard_scaler.fit(X_train)
X_train_scaled = standard_scaler.transform(X_train)
svm = SVC().fit(X_train_scaled, y_train)

#pipeline = Pipeline([("scaler", StandardScaler()),
#                     ("svm", SVC())])
# short version:
pipeline = make_pipeline(StandardScaler(), SVC())

pipeline.fit(X_train, y_train)

pipeline.score(X_test, y_test)

pipeline.predict(X_test)


I am working on the file "05 - Preprocessing and Pipelines.ipynb".
The first task is:
Cross-validation with a pipeline
Can you write Python code for it?

from sklearn.cross_validation import cross_val_score
cross_val_score(pipeline, X_train, y_train)


I am working on the file "05 - Preprocessing and Pipelines.ipynb".
The first task is:
Exercise
Make a pipeline out of the StandardScaler and KNeighborsClassifier and search over the number of neighbors.
Can you write Python code for it?


I am working on the file "05 - Preprocessing and Pipelines.ipynb".
The first task is:
Grid Search with a pipeline
Can you write Python code for it?

import numpy as np
from sklearn.grid_search import GridSearchCV

param_grid = {'svc__C': 10. ** np.arange(-3, 3),
              'svc__gamma' : 10. ** np.arange(-3, 3)
             }

grid_pipeline = GridSearchCV(pipeline, param_grid=param_grid)

grid_pipeline.fit(X_train, y_train)

grid_pipeline.score(X_test, y_test)


I am working on the file "05 - Time series data.ipynb".
The first task is:

REMEMBER:

    The string to specify the new time frequency: http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases

    These strings can also be combined with numbers, eg `'10D'`...

Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:

EXERCISE:

plot the monthly mininum and maximum daily average value of the 'LS06_348' column

Can you write Python code for it?

# %load snippets/05 - Time series data48.py

# %load snippets/05 - Time series data49.py


I am working on the file "05 - Time series data.ipynb".
The first task is:

EXERCISE:

plot the monthly standard deviation of the columns

Can you write Python code for it?

# %load snippets/05 - Time series data45.py


I am working on the file "05 - Time series data.ipynb".
The first task is:

EXERCISE:

select all 'daytime' data (between 8h and 20h) for all days

Can you write Python code for it?

# %load snippets/05 - Time series data40.py

# %load snippets/05 - Time series data41.py


I am working on the file "05 - Time series data.ipynb".
The first task is:

EXERCISE:

select all data in January for all different years

Can you write Python code for it?

# %load snippets/05 - Time series data37.py


I am working on the file "05 - Time series data.ipynb".
The first task is:

EXERCISE:

select all data in January, February and March for all different years

Can you write Python code for it?

# %load snippets/05 - Time series data38.py

data = data.drop("months", axis=1)


I am working on the file "05 - Time series data.ipynb".
The first task is:

EXERCISE:

select all data starting from 2012

Can you write Python code for it?

# %load snippets/05 - Time series data36.py


I am working on the file "05 - Time series data.ipynb".
The first task is:

    EXERCISE: select all 'daytime' data (between 8h and 20h) for all days

Can you write Python code for it?

data[(data.index.hour >= 8) & (data.index.hour < 20)]

data.between_time('08:00', '20:00')


I am working on the file "05 - Time series data.ipynb".
The first task is:

    EXERCISE: select all data in January for all different years

Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:

    EXERCISE: select all data starting from 1999

Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:

    QUESTION: plot the monthly mean and median concentration of the 'FR04037' station for the years 2009-2012

    QUESTION: plot the monthly mininum and maximum daily concentration of the 'BETR801' station

    QUESTION: make a bar plot of the mean of the stations in year of 2012

    QUESTION: The evolution of the yearly averages with, and the overall mean of all stations?

Combination with groupby
resample can actually be seen as a specific kind of groupby. E.g. taking annual means with data.resample('A', 'mean') is equivalent to data.groupby(data.index.year).mean() (only the result of resample still has a DatetimeIndex).
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:

EXERCISE:

make a bar plot of the mean of the stations in year of 2013 (Remark: create a `fig, ax = plt.subplots()` object and add the plot to the created ax

Can you write Python code for it?

# %load snippets/05 - Time series data50.py


I am working on the file "05 - Time series data.ipynb".
The first task is:
A detailed overview of how to specify the format string, see the table in the python documentation: https://docs.python.org/3.5/library/datetime.html#strftime-and-strptime-behavior
Timestamp data in a Series or DataFrame column
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
A nice feature is "partial string" indexing, where we can do implicit slicing by providing a partial datetime string.
E.g. all data of 2012:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
A nice feature is "partial string" indexing, where we can do implicit slicing by providing a partial datetime string.
E.g. all data of 2013:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
As you can see, the missing values are indicated by -9999. This can be recognized by read_csv by passing the na_values keyword:
Can you write Python code for it?

data = pd.read_csv('data/airbase_data.csv', index_col=0, parse_dates=True, na_values=[-9999])


I am working on the file "05 - Time series data.ipynb".
The first task is:
But, for convenience, indexing a time series also works with strings:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
By default, resample takes the mean as aggregation function, but other methods can also be specified:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
Getting some basic summary statistics about the data with describe:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
I downloaded and preprocessed some of the data (python-airbase): data/airbase_data.csv. This file includes the hourly concentrations of NO2 for 4 different measurement stations:

FR04037 (PARIS 13eme): urban background site at Square de Choisy
FR04012 (Paris, Place Victor Basch): urban traffic site at Rue d'Alesia
BETR802: urban traffic site in Antwerp, Belgium
BETN029: rural background site in Houtem, Belgium

See http://www.eea.europa.eu/themes/air/interactive/no2
Importing the data
Import the csv file:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
Indexing a time series works with strings:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
Introduction: datetime module
Standard Python contains the datetime module to handle with date and time data:
Can you write Python code for it?

import datetime

dt = datetime.datetime(year=2016, month=12, day=19, hour=13, minute=30)
dt

print(dt) # .day,...

print(dt.strftime("%d %B %Y"))


I am working on the file "05 - Time series data.ipynb".
The first task is:
Normally you would expect this to access a column named '2012', but as for a DatetimeIndex, pandas also tries to interprete it as a datetime slice.
Or all data of January up to March 2012:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
Normally you would expect this to access a column named '2013', but as for a DatetimeIndex, pandas also tries to interprete it as a datetime slice.
Or all data of January up to March 2012:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
Notice the data type of this series: the datetime64[ns] dtype. This indicates that we have a series of actual datetime values.
The same attributes as on single Timestamps are also available on a Series with datetime data, using the .dt accessor:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
Parsing datetime strings

Unfortunately, when working with real world data, you encounter many different datetime formats. Most of the time when you have to deal with them, they come in text format, e.g. from a CSV file. To work with those data in Pandas, we first have to parse the strings to actual Timestamp objects.

REMEMBER:

from string formatted dates to Timestamp objects: `to_datetime` function

Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
Quickly visualizing the data
Can you write Python code for it?

data.plot(kind='box', ylim=[0,250])

data['BETR801'].plot(kind='hist', bins=50)

data.plot(figsize=(12,6))


I am working on the file "05 - Time series data.ipynb".
The first task is:
The plot method will also adapt it's labels (when you zoom in, you can see the different levels of detail of the datetime labels):
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
The power of pandas: resample
A very powerfull method is resample: converting the frequency of the time series (e.g. from hourly to daily data).
The time series has a frequency of 1 hour. I want to change this to daily:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
The string to specify the new time frequency: http://pandas.pydata.org/pandas-docs/dev/timeseries.html#offset-aliases
These strings can also be combined with numbers, eg '10D'.
Further exploring the data:
Can you write Python code for it?

data.resample('M').plot() # 'A'

# data['2012'].resample('D').plot()


I am working on the file "05 - Time series data.ipynb".
The first task is:
This does not say too much ..
We can select part of the data (eg the latest 500 data points):
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
Time and date components can be accessed from the index:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
To quickly construct some regular time series data, the pd.date_range function comes in handy:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
We already know how to parse a date column with Pandas:
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
With set_index('datetime'), we set the column with datetime values as the index, which can be done by both Series and DataFrame.
Can you write Python code for it?


I am working on the file "05 - Time series data.ipynb".
The first task is:
info()
Can you write Python code for it?


I am working on the file "05 - Visual Diagnostics with Yellowbrick.ipynb".
The first task is:
Modeling and Evaluation
Common metrics for evaluating classifiers
Precision is the number of correct positive results divided by the number of all positive results (e.g. How many of the mushrooms we predicted would be edible actually were?).
Recall is the number of correct positive results divided by the number of positive results that should have been returned (e.g. How many of the mushrooms that were poisonous did we accurately predict were poisonous?).
The F1 score is a measure of a test's accuracy. It considers both the precision and the recall of the test to compute the score. The F1 score can be interpreted as a weighted average of the precision and recall, where an F1 score reaches its best value at 1 and worst at 0.
precision = true positives / (true positives + false positives)

recall = true positives / (false negatives + true positives)

F1 score = 2 * ((precision * recall) / (precision + recall))

Now we're ready to make some predictions!
Let's build a way to evaluate multiple estimators --  first using traditional numeric scores (which we'll later compare to some visual diagnostics from the Yellowbrick library).
Can you write Python code for it?

from sklearn.metrics import f1_score
from sklearn.pipeline import Pipeline

def model_selection(X, y, estimator):
    """
    Test various estimators.
    """
    y = LabelEncoder().fit_transform(y.values.ravel())
    model = Pipeline([
         ('label_encoding', EncodeCategorical(X.keys())),
         ('one_hot_encoder', OneHotEncoder()),
         ('estimator', estimator)
    ])

    # Instantiate the classification model and visualizer
    model.fit(X, y)

    expected  = y
    predicted = model.predict(X)

    # Compute and return the F1 score (the harmonic mean of precision and recall)
    return (f1_score(expected, predicted))

# Try them all!
from sklearn.svm import LinearSVC, NuSVC, SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegressionCV, LogisticRegression, SGDClassifier
from sklearn.ensemble import BaggingClassifier, ExtraTreesClassifier, RandomForestClassifier

model_selection(X, y, LinearSVC())

model_selection(X, y, NuSVC())

model_selection(X, y, SVC())

model_selection(X, y, SGDClassifier())

model_selection(X, y, KNeighborsClassifier())

model_selection(X, y, LogisticRegressionCV())

model_selection(X, y, LogisticRegression())

model_selection(X, y, BaggingClassifier())

model_selection(X, y, ExtraTreesClassifier())

model_selection(X, y, RandomForestClassifier())


I am working on the file "05 - Visual Diagnostics with Yellowbrick.ipynb".
The first task is:
Visual Model Selection with Yellowbrick
In this tutorial, we are going to look at scores for a variety of Scikit-Learn models and compare them using visual diagnostic tools from Yellowbrick in order to select the best model for our data.
About Yellowbrick
Yellowbrick is a new Python library that extends the Scikit-Learn API to incorporate visualizations into the machine learning workflow.
The Yellowbrick library is a diagnostic visualization platform for machine learning that allows data scientists to steer the model selection process. Yellowbrick extends the Scikit-Learn API with a new core object: the Visualizer. Visualizers allow visual models to be fit and transformed as part of the Scikit-Learn Pipeline process, providing visual diagnostics throughout the transformation of high dimensional data.
To learn more about Yellowbrick, visit http://www.scikit-yb.org.
About the Data
This tutorial uses a version of the mushroom data set from the UCI Machine Learning Repository. Our objective is to predict if a mushroom is poisionous or edible based on its characteristics.
The data include descriptions of hypothetical samples corresponding to 23 species of gilled mushrooms in the Agaricus and Lepiota Family.  Each species was identified as definitely edible, definitely poisonous, or of unknown edibility and not recommended (this latter class was combined with the poisonous one).
Our file, "agaricus-lepiota.txt," contains information for 3 nominally valued attributes and a target value from 8124 instances of mushrooms (4208 edible, 3916 poisonous).
Let's load the data with Pandas.
Can you write Python code for it?

import os
import pandas as pd

names = [
    'class',
    'cap-shape',
    'cap-surface',
    'cap-color'
]

mushrooms = os.path.join('data','agaricus-lepiota.txt')
dataset   = pd.read_csv(mushrooms)
dataset.columns = names
dataset.head()

features = ['cap-shape', 'cap-surface', 'cap-color']
target   = ['class']

X = dataset[features]
y = dataset[target]


I am working on the file "05 Model Architectures.ipynb".
The first task is:
Now let's fit our model! First, set up multiple costs for each of the three branches using MultiCost:
Can you write Python code for it?

cost = Multicost(costs=[GeneralizedCost(costfunc=CrossEntropyMulti()),
                        GeneralizedCost(costfunc=CrossEntropyMulti()),
                        GeneralizedCost(costfunc=CrossEntropyMulti())])


I am working on the file "05 Model Architectures.ipynb".
The first task is:
We also set up the backend and load the data.
Can you write Python code for it?

be = gen_backend(batch_size=128)

from neon.data import MNIST

mnist = MNIST(path='data/')
train_set = mnist.train_iter
valid_set = mnist.valid_iter


I am working on the file "05 While Loops and User input.ipynb".
The first task is:
Exercises
Game Preferences

Make a list that includes 3 or 4 games that you like to play.
Print a statement that tells the user what games you like.
Ask the user to tell you a game they like, and store the game in a variable such as new_game.
Add the user's game to your list.
Print a new statement that lists all of the games that we like to play (we means you and your user).
Can you write Python code for it?

# Ex 6.2 : Game Preferences

# put your code here


I am working on the file "05 While Loops and User input.ipynb".
The first task is:
Every while loop needs an initial condition that starts out true.
The while statement includes a condition to test.
All of the code in the loop will run as long as the condition remains true.
As soon as something in the loop changes the condition such that the test no longer passes, the loop stops executing.
Any code that is defined after the loop will run at this point.

Example
Here is a simple example, showing how a game will stay active as long as the player has enough power.
Can you write Python code for it?

# The player's power starts out at 5.
power = 5

# The player is allowed to keep playing as long as their power is over 0.
while power > 0:
    print("You are still playing, because your power is %d." % power)
    # Your game code would go here, which includes challenges that make it
    #   possible to lose power.
    # We can represent that by just taking away from the power.
    power = power - 1

print("\nOh no, your power dropped to 0! Game Over.")


I am working on the file "05 While Loops and User input.ipynb".
The first task is:
I faked that output, because if I ran it the output would fill up the browser. You can try to run it on your computer, as long as you know how to interrupt runaway processes:

On most systems, Ctrl-C will interrupt the currently running program.
If you are using Geany, your output is displayed in a popup terminal window. You can either press Ctrl-C, or you can use your pointer to close the terminal window.

The loop runs forever, because there is no way for the test condition to ever fail. The programmer probably meant to add a line that increments current_number by 1 each time through the loop:
Can you write Python code for it?

current_number = 1

# Count up to 5, printing the number each time.
while current_number <= 5:
    print(current_number)
    current_number = current_number + 1


I am working on the file "05 While Loops and User input.ipynb".
The first task is:
This is much cleaner code, and it gives us space to separate the details of taking an action from the act of choosing that action.
top
Using while loops to process items in a list
In the section on Lists, you saw that we can pop() items from a list. You can use a while list to pop items one at a time from one list, and work with them in whatever way you need.
Let's look at an example where we process a list of unconfirmed users.
Can you write Python code for it?

# Start with a list of unconfirmed users, and an empty list of confirmed users.
unconfirmed_users = ['ada', 'billy', 'clarence', 'daria']
confirmed_users = []

# Work through the list, and confirm each user.
while len(unconfirmed_users) > 0:

    # Get the latest unconfirmed user, and process them.
    current_user = unconfirmed_users.pop()
    print("Confirming user %s...confirmed!" % current_user.title())

    # Move the current user to the list of confirmed users.
    confirmed_users.append(current_user)

# Prove that we have finished confirming all users.
print("\nUnconfirmed users:")
for user in unconfirmed_users:
    print('- ' + user.title())

print("\nConfirmed users:")
for user in confirmed_users:
    print('- ' + user.title())


I am working on the file "05 While Loops and User input.ipynb".
The first task is:
This is pretty cool! We now have a way to accept input from users while our programs run, and we have a way to let our programs  run until our users are finished working.
Exercises
Many Games

Modify Game Preferences so your user can add as many games as they like.
Can you write Python code for it?

# Ex 6.3 : Many Games

# put your code here


I am working on the file "05 While Loops and User input.ipynb".
The first task is:
This works, but let's make one small improvement. The current program always works with the most recently added user. If users are joining faster than we can confirm them, we will leave some users behind. If we want to work on a 'first come, first served' model, or a 'first in first out' model, we can pop the first item in the list each time.
Can you write Python code for it?

# Start with a list of unconfirmed users, and an empty list of confirmed users.
unconfirmed_users = ['ada', 'billy', 'clarence', 'daria']
confirmed_users = []

# Work through the list, and confirm each user.
while len(unconfirmed_users) > 0:

    # Get the latest unconfirmed user, and process them.
    current_user = unconfirmed_users.pop(0)
    print("Confirming user %s...confirmed!" % current_user.title())

    # Move the current user to the list of confirmed users.
    confirmed_users.append(current_user)

# Prove that we have finished confirming all users.
print("\nUnconfirmed users:")
for user in unconfirmed_users:
    print('- ' + user.title())

print("\nConfirmed users:")
for user in confirmed_users:
    print('- ' + user.title())


I am working on the file "05 While Loops and User input.ipynb".
The first task is:
You will certainly make some loops run infintely at some point. When you do, just interrupt the loop and figure out the logical error you made.
Infinite loops will not be a real problem until you have users who run your programs on their machines. You won't want infinite loops then, because your users would have to shut down your program, and they would consider it buggy and unreliable. Learn to spot infinite loops, and make sure they don't pop up in your polished programs later on.
Here is one more example of an accidental infinite loop:
Can you write Python code for it?

current_number = 1

# Count up to 5, printing the number each time.
while current_number <= 5:
    print(current_number)
    current_number = current_number - 1

1
0
-1
-2
-3
...


I am working on the file "05 While Loops and User input.ipynb".
The first task is:
top
Exercises
Growing Strength

Make a variable called strength, and set its initial value to 5.
Print a message reporting the player's strength.
Set up a while loop that runs until the player's strength increases to a value such as 10.
Inside the while loop, print a message that reports the player's current strength.
Inside the while loop, write a statement that increases the player's strength.
Outside the while loop, print a message reporting that the player has grown too strong, and that they have moved up to a new level of the game.
Bonus: Play around with different cutoff levels for the value of strength, and play around with different ways to increase the strength value within the while loop.
Can you write Python code for it?

# Ex 6.1 : Growing Strength

# put your code here


I am working on the file "05 While Loops and User input.ipynb".
The first task is:
top
Using while loops to keep your programs running
Most of the programs we use every day run until we tell them to quit, and in the background this is often done with a while loop.
Here is an example of how to let the user enter an arbitrary number of names.
Can you write Python code for it?

# Start with an empty list. You can 'seed' the list with
#  some predefined values if you like.
names = []

# Set new_name to something other than 'quit'.
new_name = ''

# Start a loop that will run until the user enters 'quit'.
while new_name != 'quit':
    # Ask the user for a name.
    new_name = input("Please tell me someone I should know, or enter 'quit': ")

    # Add the new name to our list.
    names.append(new_name)

# Show that the name has been added to the list.
print(names)


I am working on the file "05 While Loops and User input.ipynb".
The first task is:
top
Overall Challenges
Gaussian Addition
This challenge is inspired by a story about the mathematician Carl Frederich Gauss. As the story goes, when young Gauss was in grade school his teacher got mad at his class one day.
"I'll keep the lot of you busy for a while", the teacher said sternly to the group. "You are to add the numbers from 1 to 100, and you are not to say a word until you are done."
The teacher expected a good period of quiet time, but a moment later our mathematician-to-be raised his hand with the answer. "It's 5050!" Gauss had realized that if you list all the numbers from 1 to 100, you can always match the first and last numbers in the list and get a common answer:
1, 2, 3, ..., 98, 99, 100
1 + 100 = 101
2 + 99 = 101
3 + 98 = 101

Gauss realized there were exactly 50 pairs of numbers in the range 1 to 100, so he did a quick calculation: 50 * 101 = 5050.

Write a program that passes a list of numbers to a function.
The function should use a while loop to keep popping the first and last numbers from the list and calculate the sum of those two numbers.
The function should print out the current numbers that are being added, and print their partial sum.
The function should keep track of how many partial sums there are.
The function should then print out how many partial sums there were.
The function should perform Gauss' multiplication, and report the final answer.

Prove that your function works, by passing in the range 1-100, and verifying that you get 5050.
gauss_addition(list(range(1,101)))

Your function should work for any set of consecutive numbers, as long as that set has an even length.
Bonus: Modify your function so that it works for any set of consecutive numbers, whether that set has an even or odd length.
Can you write Python code for it?

# Overall Challenge: Gaussian Addition

# put your code here


I am working on the file "05 beautifulsoup/01 Beautifulsoup.ipynb".
The first task is:
Definieren wir eine Variable damit.
Can you write Python code for it?

table = arrivals_soup.find('div',  {'class': 'cblock modules-flights-flightlist modules-flights'})

type(table)

table.text


I am working on the file "05 beautifulsoup/01 Beautifulsoup.ipynb".
The first task is:
Developer Tools im Browser
Suchen wir die Daten, der uns wirklich interessiert. Beginnen bei den Developers Tools.
Find und Findall
Can you write Python code for it?

arrivals_soup.find('tbody')

arrivals_soup.find('div', {'class': 'cblock modules-flights-flightlist modules-flights'})


I am working on the file "05 beautifulsoup/01 Beautifulsoup.ipynb".
The first task is:
Gehen wir die zweite Liste durch?
Can you write Python code for it?

fluege1 = [] #das ändern

for elem in row1:
    ga_zeit = elem.find('td', {'class': 'first'}).text
    herkunft = elem.find('td', {'class': 'first'}).find_next_sibling('td').text
    airline = elem.find('td', {'class': 'first'}).find_next_sibling('td') \
                                      .find_next_sibling('td') \
                                      .text.replace('\n', '').replace('\t','')
    nummer = elem.find('td', {'class': 'first'}).find_next_sibling('td') \
                                      .find_next_sibling('td') \
                                      .find_next_sibling('td').text \
                                      .replace('\t','').replace('\n', '')
    a_zeit = elem.find('td', {'class': 'first'}).find_next_sibling('td') \
                                      .find_next_sibling('td') \
                                      .find_next_sibling('td') \
                                      .find_next_sibling('td').text \
                                      .replace('\t','').replace('\n', '')
    typ = elem.find('td', {'class': 'last'}).text

    mini_dict = {'Geplante Ankunft': ga_zeit,
                 'Ankunft': a_zeit,
                 'Ankunft aus': herkunft,
                 'Flugnummer': nummer,
                 'Passagier/Cargo': typ}

    fluege1.append(mini_dict) #und hier


I am working on the file "05 beautifulsoup/01 Beautifulsoup.ipynb".
The first task is:
Verbinden wir beide Listen
Can you write Python code for it?

f = fluege

pd.DataFrame(f)

df = pd.DataFrame(f)

df.to_csv('fluege_BS.csv')


I am working on the file "05-Dictionary-Method/Dictionary Method.ipynb".
The first task is:
2. Creating Dictionary Counts
I created two text files, one is a list of positive words from the MPQA dictionary, the other is a list of negative words. One word per line. Our goal here is to count the number of positive and negative words in each row of our dataframe, and add two columns to our dataset with the count of positive and negative words.
First, read in the positive and negative words and create list variables for each.
Can you write Python code for it?

pos_sent = open("positive_words.txt").read()
neg_sent = open("negative_words.txt").read()

#view part of the pos_sent variable, to see how it's formatted.
print(pos_sent[:101])

#remember the split function? We'll split on the newline character (\n) to create a list
positive_words=pos_sent.split('\n')
negative_words=neg_sent.split('\n')

#view the first elements in the lists
print(positive_words[:10])
print(negative_words[:10])

#count number of words in each list
print(len(positive_words))
print(len(negative_words))


I am working on the file "05-Dictionary-Method/Dictionary Method.ipynb".
The first task is:
Great! Now we can create two more columns that contain the number of positive words and negative words in the review tokens. I'm going to get creative with this, as we need to do this step in one line of code for positive and negative words, each. Your challenges:

Can you parse the code? We'll walk through it together.
Think of other ways you could do this same thing.
Can you write Python code for it?

#create column with the number of positive words
df['positive_tokens'] = df['body_tokens'].apply(lambda x: len([word for word in x if word in positive_words]))
df['negative_tokens'] = df['body_tokens'].apply(lambda x: len([word for word in x if word in negative_words]))

print(df[['token_count', 'positive_tokens', 'negative_tokens']])


I am working on the file "05-Dictionary-Method/Dictionary Method.ipynb".
The first task is:
Next we tokenize the text. To do this on a Pandas dataframe we need the apply function. This simply tells the computer to take the function in the parentheses,, apply it to each row in the dataframe, and assign the output to a new column.
There are two ways to do this. If it's a built-in function you're applying to the entire field, such as nltk.word_tokenize, you can simply put the function in the parentheses,. In some cases, you need to write your own function, called a lambda function. This is the case if you're applying something to a list (Pandas does not deal with list objects well. Hopefully someone smart will fix that). We'll get to that case below.
Can you write Python code for it?

#tokenize
df['body_tokens'] = df['body_tokens'].apply(nltk.word_tokenize)

#view output
print(df['body_tokens'])

punctuations = list(string.punctuation)

#remove punctuation. Let's talk about that lambda x.
df['body_tokens'] = df['body_tokens'].apply(lambda x: [word for word in x if word not in punctuations])

#view output
print(df['body_tokens'])


I am working on the file "05-Dictionary-Method/Dictionary Method.ipynb".
The first task is:
Pre-processing is done. What other pre-processing steps might we use?
One more step before getting to the dictionary method. We want a total token count for each row, so we can normalize the dictionary counts. To do this we simply create a new column that contains the length of the token list in each row.
Can you write Python code for it?

df['token_count'] = df['body_tokens'].apply(len)

print(df[['body_tokens','token_count']])


I am working on the file "05-Dictionary-Method/Dictionary Method.ipynb".
The first task is:
That's the dictionary method! You can do this with any dictionary you want, standard or you can create your own.
2. Sentiment Analysis using the Dictionary Method
What can we do with this?
First, let's compare the overall sentiment of the reviews by genre.
Can you write Python code for it?

#use groupby function
df_genres = df.groupby('genre')

##EX: Calculate the proportion of words that are positive for each genre.
###Hint: Use the sum() function. Proportion is just the total number of positive words divided by the total number of words.
###How do you calculate this using Pandas?

##EX: Do the same for negative words. Which genre has the highest proportion of positive and negative words?


I am working on the file "05-Dictionary-Method/Dictionary Method.ipynb".
The first task is:
The next step is to create a new column in our dataset that contains tokenized words with all the pre-processing steps.
The code here will look slightly different that lesson 1, as we're applying these functions to every row in our dataframe.
Can you write Python code for it?

#first create a new column called "body_tokens" and transform to lowercase by applying the string function str.lower()
df['body_tokens'] = df['body'].str.lower()

#make sure it worked
print(df[['body','body_tokens']])


I am working on the file "05-Intro-to-SpaCy/01-Intro-to-SpaCy.ipynb".
The first task is:
Challenge
What's the most common verb? Noun? What if you only include lemmata? What if you remove "stop words"?
How would lemmatizing or removing "stop words" help us better understand a text over regular tokenizing?

Dependency Parsing
Let's look at our text again:
Can you write Python code for it?


I am working on the file "05-Intro-to-SpaCy/01-Intro-to-SpaCy.ipynb".
The first task is:
Challenge
With this larger string, find the most common noun, verb, and adjective. Then explore the other features of spacy and see what you can discover about our reading:

Let's continue in the fashion that Wilkens did and extract the named entities, specifically those for "GPE". We can loop through each entity, and if it is labeled as GPE we'll add it to our places list. We'll then make a Counter object out of that to get the frequency of each place name.
Can you write Python code for it?

from collections import Counter

places = []

for ent in parsed.ents:
    if ent.label_ == "GPE":
        places.append(ent.text.strip())

places = Counter(places)
places


I am working on the file "05-Intro-to-SpaCy/01-Intro-to-SpaCy.ipynb".
The first task is:
Dependency parsing is one of the most useful and interesting NLP tools. A dependency parser will draw a tree of relationships between words. This is how you can find out specifically what adjectives are attributed to a specific person, what verbs are associated with a specific subject, etc.
spacy provides an online visualizer named "displaCy" to visualize dependencies. Let's look at the first sentence

We can loop through a dependency for a subject by checking the head attribute for the pos tag:
Can you write Python code for it?

from spacy.symbols import nsubj, VERB

SV = []
for possible_subject in parsed_text:
    if possible_subject.dep == nsubj and possible_subject.head.pos == VERB:
        SV.append((possible_subject.text, possible_subject.head))

sv_tab = Table()
sv_tab.append_column(label="Subject", values=[x[0] for x in SV])
sv_tab.append_column(label="Verb", values=[x[1] for x in SV])
sv_tab.show()


I am working on the file "05-Intro-to-SpaCy/01-Intro-to-SpaCy.ipynb".
The first task is:
Lemmata?
Can you write Python code for it?

toks_tab.append_column(label="Lemma", values=[word.lemma_ for word in parsed_text])
toks_tab.show()


I am working on the file "05-Intro-to-SpaCy/01-Intro-to-SpaCy.ipynb".
The first task is:
Let's start out with a short string from our reading and see what happens.
Can you write Python code for it?

my_string = '''
"What are you going to do with yourself this evening, Alfred?" said Mr.
Royal to his companion, as they issued from his counting-house in New
Orleans. "Perhaps I ought to apologize for not calling you Mr. King,
considering the shortness of our acquaintance; but your father and I
were like brothers in our youth, and you resemble him so much, I can
hardly realize that you are not he himself, and I still a young man.
It used to be a joke with us that we must be cousins, since he was a
King and I was of the Royal family. So excuse me if I say to you, as
I used to say to him. What are you going to do with yourself, Cousin
Alfred?"

"I thank you for the friendly familiarity," rejoined the young man.
"It is pleasant to know that I remind you so strongly of my good
father. My most earnest wish is to resemble him in character as much
as I am said to resemble him in person. I have formed no plans for the
evening. I was just about to ask you what there was best worth seeing
or hearing in the Crescent City."'''.replace("\n", " ")