mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-02-02 19:31:33 +01:00
Refactor code for improved readability and consistency across multiple Jupyter notebooks
- Added missing commas in various print statements and function calls for better syntax. - Reformatted code to enhance clarity, including breaking long lines and aligning parameters. - Updated function signatures to use float type for sigma parameters instead of int for better precision. - Cleaned up comments and documentation strings for clarity and consistency. - Ensured consistent formatting in plotting functions and data handling.
This commit is contained in:
@@ -338,7 +338,7 @@
|
||||
" return np.max(\n",
|
||||
" np.power(np.abs(sol_appr - sol_exact), 2)[\n",
|
||||
" np.isfinite(np.power(np.abs(sol_appr - sol_exact), 2))\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"from sklearn.datasets import make_classification\n",
|
||||
"from sklearn.metrics import accuracy_score\n",
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
@@ -47,12 +48,18 @@
|
||||
"\n",
|
||||
"for _ in range(10):\n",
|
||||
" X, y = make_classification(\n",
|
||||
" n_samples=1000, n_features=4, n_classes=3, n_clusters_per_class=1\n",
|
||||
" n_samples=1000,\n",
|
||||
" n_features=4,\n",
|
||||
" n_classes=3,\n",
|
||||
" n_clusters_per_class=1,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
|
||||
" model = MLPClassifier(\n",
|
||||
" hidden_layer_sizes=(5, 7), activation=\"relu\", max_iter=10000, solver=\"adam\"\n",
|
||||
" hidden_layer_sizes=(5, 7),\n",
|
||||
" activation=\"relu\",\n",
|
||||
" max_iter=10000,\n",
|
||||
" solver=\"adam\",\n",
|
||||
" )\n",
|
||||
" model.fit(X_train, y_train)\n",
|
||||
"\n",
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"import scipy.stats as stats"
|
||||
"from scipy import stats"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -46,15 +46,12 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def S(t, S0, mu, sigma, W):\n",
|
||||
" \"\"\"\n",
|
||||
" Solution exacte de l'EDS de Black-Scholes\n",
|
||||
" \"\"\"\n",
|
||||
" \"\"\"Solution exacte de l'EDS de Black-Scholes\"\"\"\n",
|
||||
" return S0 * np.exp((mu - 0.5 * sigma**2) * t + sigma * W)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def euler_maruyama(mu, sigma, T, N, X0=0.0):\n",
|
||||
" \"\"\"\n",
|
||||
" Simulation d'une EDS de Black-Scholes par la méthode d'Euler-Maruyama\n",
|
||||
" \"\"\"Simulation d'une EDS de Black-Scholes par la méthode d'Euler-Maruyama\n",
|
||||
"\n",
|
||||
" Paramètres :\n",
|
||||
" mu (float) : drift\n",
|
||||
@@ -84,8 +81,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"def plot_brownien(t, X, B=None):\n",
|
||||
" \"\"\"\n",
|
||||
" Plot la simulation d'Euler-Maruyama\n",
|
||||
" \"\"\"Plot la simulation d'Euler-Maruyama\n",
|
||||
"\n",
|
||||
" Paramètres :\n",
|
||||
" t (array-like) : tableau des temps\n",
|
||||
@@ -169,8 +165,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"def plot_convergence(S0, mu, sigma, T):\n",
|
||||
" \"\"\"\n",
|
||||
" Plot la convergence du schéma d'Euler-Maruyama\n",
|
||||
" \"\"\"Plot la convergence du schéma d'Euler-Maruyama\n",
|
||||
"\n",
|
||||
" Paramètres :\n",
|
||||
" S0 (int) : valeur initiale\n",
|
||||
@@ -291,7 +286,7 @@
|
||||
"print(\n",
|
||||
" \"La barrière a été franchie\"\n",
|
||||
" if is_barrier_breached(X, B)\n",
|
||||
" else \"La barrière n'a pas été franchie\"\n",
|
||||
" else \"La barrière n'a pas été franchie\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -335,8 +330,7 @@
|
||||
" \"\"\"\n",
|
||||
" if not is_barrier_breached(X, B):\n",
|
||||
" return max(X[-1] - K, 0)\n",
|
||||
" else:\n",
|
||||
" return 0\n",
|
||||
" return 0\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def call_BS(x):\n",
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
" for h in h_list:\n",
|
||||
" t = np.arange(a, b, h)\n",
|
||||
" y = np.array(\n",
|
||||
" [3 / 4 * h * f(t[i] + h / 3) + h / 4 * f(t[i] + h) for i in range(len(t))]\n",
|
||||
" [3 / 4 * h * f(t[i] + h / 3) + h / 4 * f(t[i] + h) for i in range(len(t))],\n",
|
||||
" )\n",
|
||||
" I_approx = np.sum(y)\n",
|
||||
" I.append(I_approx)\n",
|
||||
@@ -326,7 +326,7 @@
|
||||
" 1 + np.power(x, 2) * y - (z + 1) * x,\n",
|
||||
" x * z - np.power(x, 2) * y,\n",
|
||||
" -x * z + 1.45,\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -682,7 +682,7 @@
|
||||
" [\n",
|
||||
" (F(x + delta * e(i, d)) - F(x - delta * e(i, d))) / (2 * delta)\n",
|
||||
" for i in range(d)\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -384,7 +384,7 @@
|
||||
"optimal_point_newton, iterations_newton = newton_method(initial_guess_newton)\n",
|
||||
"print(f\"Optimal point (Newton): {optimal_point_newton}\")\n",
|
||||
"print(\n",
|
||||
" f\"Objective function value at optimal point (Newton): {objective_function(optimal_point_newton)}\"\n",
|
||||
" f\"Objective function value at optimal point (Newton): {objective_function(optimal_point_newton)}\",\n",
|
||||
")\n",
|
||||
"print(f\"Number of iterations (Newton): {iterations_newton}\")\n",
|
||||
"\n",
|
||||
@@ -395,7 +395,7 @@
|
||||
"optimal_point_dichotomy, iterations_dichotomy = dichotomy_method(aL, aR)\n",
|
||||
"print(f\"Optimal point (Dichotomy): {optimal_point_dichotomy}\")\n",
|
||||
"print(\n",
|
||||
" f\"Objective function value at optimal point (Dichotomy): {objective_function(optimal_point_dichotomy)}\"\n",
|
||||
" f\"Objective function value at optimal point (Dichotomy): {objective_function(optimal_point_dichotomy)}\",\n",
|
||||
")\n",
|
||||
"print(f\"Number of iterations (Dichotomy): {iterations_dichotomy}\")"
|
||||
]
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
"def generate_thetas(n):\n",
|
||||
" random_steps = np.random.random(n)\n",
|
||||
" return np.concatenate(\n",
|
||||
" ([0], np.cumsum(random_steps / np.sum(random_steps) * (2 * np.pi)))\n",
|
||||
" ([0], np.cumsum(random_steps / np.sum(random_steps) * (2 * np.pi))),\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -27,9 +27,10 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import yfinance as yf\n",
|
||||
"\n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import yfinance as yf"
|
||||
"import pandas as pd"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -406,7 +407,7 @@
|
||||
"print(f\"Standard deviation sd_T: {sd_T}\")\n",
|
||||
"print(f\"Allocation pi_T: {pi_T}\")\n",
|
||||
"print(\n",
|
||||
" f\"We can verify that the allocation is possible as the sum of the allocations for the different indices is {sum(pi_T)}, that is very close to 1\"\n",
|
||||
" f\"We can verify that the allocation is possible as the sum of the allocations for the different indices is {sum(pi_T)}, that is very close to 1\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -452,9 +453,9 @@
|
||||
"for i in range(len(std)):\n",
|
||||
" print(f\"The annualized volatilities of the index {Tickers[i]} is {std[i]}\")\n",
|
||||
" print(\n",
|
||||
" f\"The annualized expected returns of the index {Tickers[i]} is {mean[Tickers[i]]}\"\n",
|
||||
" f\"The annualized expected returns of the index {Tickers[i]} is {mean[Tickers[i]]}\",\n",
|
||||
" )\n",
|
||||
" print(\"\")\n",
|
||||
" print()\n",
|
||||
"\n",
|
||||
"print(f\"The annualized volatility of the Tangent Portfolio is {sd_T * np.sqrt(252)}\")\n",
|
||||
"print(f\"The annualized expected return of the Tangent Portfolio is {m_T * 252}\")"
|
||||
@@ -494,7 +495,7 @@
|
||||
"\n",
|
||||
"for i in range(4):\n",
|
||||
" print(\n",
|
||||
" f\"the sharpe ratio of the index {Tickers[i]} is {(mean[Tickers[i]] - r) / std[i]}\"\n",
|
||||
" f\"the sharpe ratio of the index {Tickers[i]} is {(mean[Tickers[i]] - r) / std[i]}\",\n",
|
||||
" )"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -13,9 +13,10 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import yfinance as yf\n",
|
||||
"\n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import yfinance as yf"
|
||||
"import pandas as pd"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -530,7 +531,7 @@
|
||||
"\n",
|
||||
"# Self financing portfolio\n",
|
||||
"m_w = np.sqrt(\n",
|
||||
" (mean - b / a * vec1).T.dot(inv_sigma).dot(mean - b / a * vec1)\n",
|
||||
" (mean - b / a * vec1).T.dot(inv_sigma).dot(mean - b / a * vec1),\n",
|
||||
") # Expected return\n",
|
||||
"\n",
|
||||
"# Tangent portfolio\n",
|
||||
@@ -580,7 +581,7 @@
|
||||
"range_sup = np.max(mean) + 1\n",
|
||||
"y = np.linspace(range_inf, range_sup, 50)\n",
|
||||
"x_1 = np.array(\n",
|
||||
" [np.sqrt(((y - m_a) / m_w) ** 2 + sd_a**2)]\n",
|
||||
" [np.sqrt(((y - m_a) / m_w) ** 2 + sd_a**2)],\n",
|
||||
") # Sigma values for the frontier\n",
|
||||
"x_2 = np.array([(y - r) / (m_T - r) * sd_T]) # Sigma values for the Capital Market Line\n",
|
||||
"\n",
|
||||
|
||||
@@ -1114,7 +1114,12 @@
|
||||
"R = multivariate_normal([0, 0], np.eye(2))\n",
|
||||
"\n",
|
||||
"surf = ax.plot_surface(\n",
|
||||
" X, Y, R.pdf(np.dstack((X, Y))), cmap=\"coolwarm\", linewidth=0, antialiased=False\n",
|
||||
" X,\n",
|
||||
" Y,\n",
|
||||
" R.pdf(np.dstack((X, Y))),\n",
|
||||
" cmap=\"coolwarm\",\n",
|
||||
" linewidth=0,\n",
|
||||
" antialiased=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"fig.colorbar(surf, shrink=0.5, aspect=5)\n",
|
||||
|
||||
@@ -238,7 +238,10 @@
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(\n",
|
||||
" X, y, test_size=0.33, random_state=42\n",
|
||||
" X,\n",
|
||||
" y,\n",
|
||||
" test_size=0.33,\n",
|
||||
" random_state=42,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -702,10 +705,10 @@
|
||||
"predictions2 = [knn_class_2(X_train, y_train, data, 3) for data in X_test]\n",
|
||||
"\n",
|
||||
"print(\n",
|
||||
" f\"The accuracy rate of our classifier is {np.sum(predictions == y_test) / len(predictions) * 100}%\"\n",
|
||||
" f\"The accuracy rate of our classifier is {np.sum(predictions == y_test) / len(predictions) * 100}%\",\n",
|
||||
")\n",
|
||||
"print(\n",
|
||||
" f\"The accuracy rate of our classifier is {np.sum(predictions2 == y_test) / len(predictions2) * 100}%\"\n",
|
||||
" f\"The accuracy rate of our classifier is {np.sum(predictions2 == y_test) / len(predictions2) * 100}%\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -1278,6 +1281,7 @@
|
||||
"\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"from sklearn.neighbors import KNeighborsClassifier"
|
||||
]
|
||||
},
|
||||
@@ -2094,7 +2098,10 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"X_train, X_test, y_train, y_test = train_test_split(\n",
|
||||
" X, y, test_size=0.33, random_state=42\n",
|
||||
" X,\n",
|
||||
" y,\n",
|
||||
" test_size=0.33,\n",
|
||||
" random_state=42,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"\n",
|
||||
"from sklearn import datasets\n",
|
||||
"\n",
|
||||
"iris = datasets.load_iris(as_frame=True)"
|
||||
@@ -402,7 +403,10 @@
|
||||
"from sklearn.neighbors import KNeighborsClassifier\n",
|
||||
"\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(\n",
|
||||
" X, y, test_size=0.33, random_state=42\n",
|
||||
" X,\n",
|
||||
" y,\n",
|
||||
" test_size=0.33,\n",
|
||||
" random_state=42,\n",
|
||||
")\n",
|
||||
"knn_clf = KNeighborsClassifier(n_neighbors=5)\n",
|
||||
"knn_clf.fit(X_train, y_train)\n",
|
||||
@@ -583,7 +587,11 @@
|
||||
],
|
||||
"source": [
|
||||
"X_train_strat, X_test_strat, y_train_strat, y_test_strat = train_test_split(\n",
|
||||
" X, y, test_size=0.33, random_state=42, stratify=y\n",
|
||||
" X,\n",
|
||||
" y,\n",
|
||||
" test_size=0.33,\n",
|
||||
" random_state=42,\n",
|
||||
" stratify=y,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"y_test_strat.value_counts()"
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"from sklearn.linear_model import LogisticRegression"
|
||||
]
|
||||
},
|
||||
@@ -192,7 +193,10 @@
|
||||
"plt.figure(figsize=(8, 8))\n",
|
||||
"\n",
|
||||
"plt.scatter(\n",
|
||||
" X[:, 0], X[:, 1], alpha=0.3, cmap=mcolors.ListedColormap([\"steelblue\", \"tomato\"])\n",
|
||||
" X[:, 0],\n",
|
||||
" X[:, 1],\n",
|
||||
" alpha=0.3,\n",
|
||||
" cmap=mcolors.ListedColormap([\"steelblue\", \"tomato\"]),\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -708,20 +712,22 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def mini_batch_SGD(X, y, learning_rate, batch_size, epochs):\n",
|
||||
" \"\"\"\n",
|
||||
" Mini-batch stochastic gradient descent for logistic regression.\n",
|
||||
" \"\"\"Mini-batch stochastic gradient descent for logistic regression.\n",
|
||||
"\n",
|
||||
" Parameters:\n",
|
||||
" Parameters\n",
|
||||
" ----------\n",
|
||||
" X (numpy.ndarray): Input data of shape (n, d), where n is the number of samples and d is the number of features.\n",
|
||||
" y (numpy.ndarray): Labels of shape (n,), where n is the sample size.\n",
|
||||
" learning_rate (float): Learning rate for gradient descent.\n",
|
||||
" batch_size (int): Size of each mini-batch.\n",
|
||||
" epochs (int): Number of epochs to train.\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" Returns\n",
|
||||
" -------\n",
|
||||
" w (numpy.ndarray): Final weight vector of shape (d,).\n",
|
||||
" b (float): Final bias term.\n",
|
||||
" costs_SGD (list): Cost function values at each step.\n",
|
||||
"\n",
|
||||
" \"\"\"\n",
|
||||
" # Initialization\n",
|
||||
" n, d = X.shape\n",
|
||||
@@ -40783,7 +40789,11 @@
|
||||
],
|
||||
"source": [
|
||||
"w_SGD, b_SGD, cost_SGD = mini_batch_SGD(\n",
|
||||
" X, y, learning_rate=5e-5, batch_size=50, epochs=1000\n",
|
||||
" X,\n",
|
||||
" y,\n",
|
||||
" learning_rate=5e-5,\n",
|
||||
" batch_size=50,\n",
|
||||
" epochs=1000,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -40950,7 +40960,11 @@
|
||||
],
|
||||
"source": [
|
||||
"w_SGD, b_SGD, costs_SGD = mini_batch_SGD(\n",
|
||||
" X, y, learning_rate=5e-4, batch_size=1000, epochs=30\n",
|
||||
" X,\n",
|
||||
" y,\n",
|
||||
" learning_rate=5e-4,\n",
|
||||
" batch_size=1000,\n",
|
||||
" epochs=30,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"The parameters computed by stochastic gradient descent are: \", w_SGD, b_SGD)"
|
||||
@@ -41041,7 +41055,10 @@
|
||||
"\n",
|
||||
"X, y = iris.data, iris.target\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(\n",
|
||||
" X, y, test_size=0.33, random_state=5\n",
|
||||
" X,\n",
|
||||
" y,\n",
|
||||
" test_size=0.33,\n",
|
||||
" random_state=5,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"log_reg = LogisticRegression(fit_intercept=True)\n",
|
||||
@@ -41194,7 +41211,7 @@
|
||||
"model.add(tf.keras.layers.Input(shape=[28, 28])) # we specify the input shape\n",
|
||||
"model.add(tf.keras.layers.Flatten()) # we flatten the data\n",
|
||||
"model.add(\n",
|
||||
" tf.keras.layers.Dense(10, activation=\"softmax\")\n",
|
||||
" tf.keras.layers.Dense(10, activation=\"softmax\"),\n",
|
||||
") # 10 labels (figures from 0 to 9)\n",
|
||||
"# activation=\"softmax\" as it is a multiclass problem"
|
||||
]
|
||||
@@ -41237,7 +41254,9 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model.compile(\n",
|
||||
" loss=\"sparse_categorical_crossentropy\", optimizer=\"sgd\", metrics=[\"accuracy\"]\n",
|
||||
" loss=\"sparse_categorical_crossentropy\",\n",
|
||||
" optimizer=\"sgd\",\n",
|
||||
" metrics=[\"accuracy\"],\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -898,9 +898,9 @@
|
||||
"ex = pd.DataFrame(\n",
|
||||
" {\n",
|
||||
" \"nom\": [\"Alice\", \"Nicolas\", \"Jean\"],\n",
|
||||
" \"age\": [19, np.NaN, np.NaN],\n",
|
||||
" \"exam\": [15, 14, np.NaN],\n",
|
||||
" }\n",
|
||||
" \"age\": [19, np.nan, np.nan],\n",
|
||||
" \"exam\": [15, 14, np.nan],\n",
|
||||
" },\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"data : \\n\", ex)\n",
|
||||
@@ -2299,7 +2299,8 @@
|
||||
"\n",
|
||||
"linReg = LinearRegression()\n",
|
||||
"linReg.fit(\n",
|
||||
" Xtrain, Ytrain\n",
|
||||
" Xtrain,\n",
|
||||
" Ytrain,\n",
|
||||
") # no need to scale for OLS if you just want to predict (unless the solver works best with scaled data)\n",
|
||||
"# the predictions should not be different with or without standardization (could differ only owing to numerical problems)\n",
|
||||
"hatY_LinReg = linReg.predict(Xtest)\n",
|
||||
|
||||
@@ -1246,14 +1246,15 @@
|
||||
"# 2. Displaying the vectors :\n",
|
||||
"\n",
|
||||
"print(\n",
|
||||
" \"2. The vectors corresponding to the sms are : \\n\", X.toarray()\n",
|
||||
" \"2. The vectors corresponding to the sms are : \\n\",\n",
|
||||
" X.toarray(),\n",
|
||||
") # X.toarray because\n",
|
||||
"# X is a \"sparse\" matrix.\n",
|
||||
"\n",
|
||||
"# 3. For a new data x_0=\"iphone gratuit\",\n",
|
||||
"# you must also transform x_0 into a numerical vector before predicting.\n",
|
||||
"\n",
|
||||
"vec_x_0 = vec.transform([\"iphone gratuit\"]).toarray() #\n",
|
||||
"vec_x_0 = vec.transform([\"iphone gratuit\"]).toarray()\n",
|
||||
"print(\"3. The numerical vector corresponding to (x_0=iphone gratuit) is \\n\", vec_x_0)"
|
||||
]
|
||||
},
|
||||
@@ -1410,7 +1411,10 @@
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(\n",
|
||||
" X, y, test_size=0.30, random_state=50\n",
|
||||
" X,\n",
|
||||
" y,\n",
|
||||
" test_size=0.30,\n",
|
||||
" random_state=50,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"size of the training set: \", X_train.shape[0])\n",
|
||||
@@ -1986,7 +1990,7 @@
|
||||
" \"Iphone 15 is now free\",\n",
|
||||
" \"I want coffee\",\n",
|
||||
" \"I want to buy a new iphone\",\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"pred_my_sms = sms_bayes.predict(my_sms)\n",
|
||||
@@ -2055,7 +2059,10 @@
|
||||
"X_copy = (X.copy() >= 127).astype(int)\n",
|
||||
"\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(\n",
|
||||
" X_copy, y, test_size=0.25, random_state=42\n",
|
||||
" X_copy,\n",
|
||||
" y,\n",
|
||||
" test_size=0.25,\n",
|
||||
" random_state=42,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"ber_bayes = BernoulliNB()\n",
|
||||
|
||||
@@ -92,6 +92,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"import tensorflow as tf\n",
|
||||
"\n",
|
||||
"tf.keras.utils.set_random_seed(42)\n",
|
||||
@@ -346,7 +347,7 @@
|
||||
" tf.keras.layers.Dense(300, activation=\"relu\", kernel_initializer=\"he_normal\"),\n",
|
||||
" tf.keras.layers.Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n",
|
||||
" tf.keras.layers.Dense(10, activation=\"softmax\"),\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -691,7 +692,9 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model.compile(\n",
|
||||
" loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n",
|
||||
" loss=\"sparse_categorical_crossentropy\",\n",
|
||||
" optimizer=\"adam\",\n",
|
||||
" metrics=[\"accuracy\"],\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -1101,11 +1104,13 @@
|
||||
" tf.keras.layers.Dense(300, activation=\"relu\", kernel_initializer=\"he_normal\"),\n",
|
||||
" tf.keras.layers.Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n",
|
||||
" tf.keras.layers.Dense(10, activation=\"softmax\"),\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"model_10.compile(\n",
|
||||
" loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n",
|
||||
" loss=\"sparse_categorical_crossentropy\",\n",
|
||||
" optimizer=\"adam\",\n",
|
||||
" metrics=[\"accuracy\"],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"model_10.fit(X_train01, y_train, epochs=10, validation_data=(X_val01, y_val))"
|
||||
@@ -1270,7 +1275,8 @@
|
||||
],
|
||||
"source": [
|
||||
"early_stopping_cb = tf.keras.callbacks.EarlyStopping(\n",
|
||||
" patience=5, restore_best_weights=True\n",
|
||||
" patience=5,\n",
|
||||
" restore_best_weights=True,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"model = tf.keras.Sequential(\n",
|
||||
@@ -1280,11 +1286,13 @@
|
||||
" tf.keras.layers.Dense(300, activation=\"relu\", kernel_initializer=\"he_normal\"),\n",
|
||||
" tf.keras.layers.Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n",
|
||||
" tf.keras.layers.Dense(10, activation=\"softmax\"),\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"model.compile(\n",
|
||||
" loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n",
|
||||
" loss=\"sparse_categorical_crossentropy\",\n",
|
||||
" optimizer=\"adam\",\n",
|
||||
" metrics=[\"accuracy\"],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"history2 = model.fit(\n",
|
||||
@@ -1598,10 +1606,12 @@
|
||||
" tf.keras.layers.Input(shape=[28, 28]),\n",
|
||||
" tf.keras.layers.Flatten(),\n",
|
||||
" tf.keras.layers.Dense(10, activation=\"softmax\"),\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"reg_log.compile(\n",
|
||||
" loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n",
|
||||
" loss=\"sparse_categorical_crossentropy\",\n",
|
||||
" optimizer=\"adam\",\n",
|
||||
" metrics=[\"accuracy\"],\n",
|
||||
")\n",
|
||||
"reg_log.fit(X_train01, y_train, epochs=90, validation_data=(X_val01, y_val))"
|
||||
]
|
||||
@@ -1709,10 +1719,12 @@
|
||||
" tf.keras.layers.Dense(300, activation=\"relu\", kernel_initializer=\"he_normal\"),\n",
|
||||
" tf.keras.layers.Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n",
|
||||
" tf.keras.layers.Dense(10, activation=\"softmax\"),\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"model_ter.compile(\n",
|
||||
" loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n",
|
||||
" loss=\"sparse_categorical_crossentropy\",\n",
|
||||
" optimizer=\"adam\",\n",
|
||||
" metrics=[\"accuracy\"],\n",
|
||||
")\n",
|
||||
"model_ter.fit(X_train, y_train, epochs=30, validation_data=(X_val, y_val))"
|
||||
]
|
||||
@@ -1820,10 +1832,12 @@
|
||||
" tf.keras.layers.Dense(300, activation=\"relu\", kernel_initializer=\"he_normal\"),\n",
|
||||
" tf.keras.layers.Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n",
|
||||
" tf.keras.layers.Dense(10, activation=\"softmax\"),\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"model_5.compile(\n",
|
||||
" loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n",
|
||||
" loss=\"sparse_categorical_crossentropy\",\n",
|
||||
" optimizer=\"adam\",\n",
|
||||
" metrics=[\"accuracy\"],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"X_train_far_too_small, X_val_far_too_small = X_train / 25500.0, X_val / 25500.0\n",
|
||||
@@ -1938,16 +1952,22 @@
|
||||
" tf.keras.layers.Input(shape=[28, 28]),\n",
|
||||
" tf.keras.layers.Flatten(),\n",
|
||||
" tf.keras.layers.Dense(\n",
|
||||
" 300, activation=\"sigmoid\", kernel_initializer=\"he_normal\"\n",
|
||||
" 300,\n",
|
||||
" activation=\"sigmoid\",\n",
|
||||
" kernel_initializer=\"he_normal\",\n",
|
||||
" ),\n",
|
||||
" tf.keras.layers.Dense(\n",
|
||||
" 100, activation=\"sigmoid\", kernel_initializer=\"he_normal\"\n",
|
||||
" 100,\n",
|
||||
" activation=\"sigmoid\",\n",
|
||||
" kernel_initializer=\"he_normal\",\n",
|
||||
" ),\n",
|
||||
" tf.keras.layers.Dense(10, activation=\"softmax\"),\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"model_sig_norm.compile(\n",
|
||||
" loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n",
|
||||
" loss=\"sparse_categorical_crossentropy\",\n",
|
||||
" optimizer=\"adam\",\n",
|
||||
" metrics=[\"accuracy\"],\n",
|
||||
")\n",
|
||||
"model_sig_norm.fit(X_train01, y_train, epochs=30, validation_data=(X_val, y_val))"
|
||||
]
|
||||
@@ -2043,16 +2063,22 @@
|
||||
" tf.keras.layers.Input(shape=[28, 28]),\n",
|
||||
" tf.keras.layers.Flatten(),\n",
|
||||
" tf.keras.layers.Dense(\n",
|
||||
" 300, activation=\"sigmoid\", kernel_initializer=\"he_normal\"\n",
|
||||
" 300,\n",
|
||||
" activation=\"sigmoid\",\n",
|
||||
" kernel_initializer=\"he_normal\",\n",
|
||||
" ),\n",
|
||||
" tf.keras.layers.Dense(\n",
|
||||
" 100, activation=\"sigmoid\", kernel_initializer=\"he_normal\"\n",
|
||||
" 100,\n",
|
||||
" activation=\"sigmoid\",\n",
|
||||
" kernel_initializer=\"he_normal\",\n",
|
||||
" ),\n",
|
||||
" tf.keras.layers.Dense(10, activation=\"softmax\"),\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"model_sig_un_norm.compile(\n",
|
||||
" loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n",
|
||||
" loss=\"sparse_categorical_crossentropy\",\n",
|
||||
" optimizer=\"adam\",\n",
|
||||
" metrics=[\"accuracy\"],\n",
|
||||
")\n",
|
||||
"model_sig_un_norm.fit(X_train, y_train, epochs=30, validation_data=(X_val, y_val))"
|
||||
]
|
||||
@@ -2220,17 +2246,19 @@
|
||||
" tf.keras.layers.Dense(300, activation=\"relu\"),\n",
|
||||
" tf.keras.layers.Dense(100, activation=\"relu\"),\n",
|
||||
" tf.keras.layers.Dense(10, activation=\"softmax\"),\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"model_high_variance.layers[1].set_weights(\n",
|
||||
" [200 * np.random.randn(28 * 28, 300) / 100, np.zeros(300)]\n",
|
||||
" [200 * np.random.randn(28 * 28, 300) / 100, np.zeros(300)],\n",
|
||||
")\n",
|
||||
"model_high_variance.layers[2].set_weights(\n",
|
||||
" [200 * np.random.randn(300, 100) / 100, np.zeros(100)]\n",
|
||||
" [200 * np.random.randn(300, 100) / 100, np.zeros(100)],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"model_high_variance.compile(\n",
|
||||
" loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n",
|
||||
" loss=\"sparse_categorical_crossentropy\",\n",
|
||||
" optimizer=\"adam\",\n",
|
||||
" metrics=[\"accuracy\"],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"model_high_variance.fit(X_train01, y_train, epochs=60, validation_data=(X_val01, y_val))"
|
||||
|
||||
@@ -543,7 +543,12 @@
|
||||
"plt.plot(X[:, 0], X[:, 1], \".b\", alpha=0.2)\n",
|
||||
"for center in kmeans1.cluster_centers_:\n",
|
||||
" plt.plot(\n",
|
||||
" center[0], center[1], \".\", color=\"red\", markersize=10, label=\"Cluster center\"\n",
|
||||
" center[0],\n",
|
||||
" center[1],\n",
|
||||
" \".\",\n",
|
||||
" color=\"red\",\n",
|
||||
" markersize=10,\n",
|
||||
" label=\"Cluster center\",\n",
|
||||
" )\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
@@ -623,7 +628,12 @@
|
||||
"\n",
|
||||
"for center in kmeans1.cluster_centers_:\n",
|
||||
" plt.plot(\n",
|
||||
" center[0], center[1], \".\", color=\"red\", markersize=10, label=\"Cluster center\"\n",
|
||||
" center[0],\n",
|
||||
" center[1],\n",
|
||||
" \".\",\n",
|
||||
" color=\"red\",\n",
|
||||
" markersize=10,\n",
|
||||
" label=\"Cluster center\",\n",
|
||||
" )\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
@@ -1529,9 +1539,10 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"from scipy.stats import mode\n",
|
||||
"\n",
|
||||
"import tensorflow as tf\n",
|
||||
"\n",
|
||||
"mnist = tf.keras.datasets.mnist\n",
|
||||
"(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
|
||||
"\n",
|
||||
@@ -1543,7 +1554,7 @@
|
||||
"\n",
|
||||
"def map_clusters_to_labels(clusters, true_labels):\n",
|
||||
" return np.array(\n",
|
||||
" [mode(true_labels[clusters == i], keepdims=True).mode[0] for i in range(10)]\n",
|
||||
" [mode(true_labels[clusters == i], keepdims=True).mode[0] for i in range(10)],\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"\n",
|
||||
"import tensorflow as tf"
|
||||
]
|
||||
},
|
||||
@@ -187,10 +188,12 @@
|
||||
" kernel_regularizer=tf.keras.regularizers.l2(0.01),\n",
|
||||
" ),\n",
|
||||
" tf.keras.layers.Dense(\n",
|
||||
" 8, activation=\"relu\", kernel_regularizer=tf.keras.regularizers.l2(0.01)\n",
|
||||
" 8,\n",
|
||||
" activation=\"relu\",\n",
|
||||
" kernel_regularizer=tf.keras.regularizers.l2(0.01),\n",
|
||||
" ),\n",
|
||||
" tf.keras.layers.Dense(1, activation=\"sigmoid\"),\n",
|
||||
" ]\n",
|
||||
" ],\n",
|
||||
" )\n",
|
||||
" model.compile(optimizer=\"adam\", loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\n",
|
||||
" return model"
|
||||
@@ -296,7 +299,10 @@
|
||||
"histories = []\n",
|
||||
"\n",
|
||||
"early_stopping = EarlyStopping(\n",
|
||||
" monitor=\"val_loss\", patience=10, restore_best_weights=True, verbose=1\n",
|
||||
" monitor=\"val_loss\",\n",
|
||||
" patience=10,\n",
|
||||
" restore_best_weights=True,\n",
|
||||
" verbose=1,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"for fold, (train_idx, val_idx) in enumerate(skf.split(X, y), 1):\n",
|
||||
@@ -314,7 +320,9 @@
|
||||
"\n",
|
||||
" # EarlyStopping\n",
|
||||
" callback = tf.keras.callbacks.EarlyStopping(\n",
|
||||
" monitor=\"val_loss\", patience=10, restore_best_weights=True\n",
|
||||
" monitor=\"val_loss\",\n",
|
||||
" patience=10,\n",
|
||||
" restore_best_weights=True,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # Entraînement\n",
|
||||
@@ -433,13 +441,18 @@
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"import tensorflow as tf\n",
|
||||
"from sklearn.metrics import classification_report, f1_score\n",
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"from sklearn.preprocessing import StandardScaler\n",
|
||||
"\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(\n",
|
||||
" X, y, test_size=0.2, random_state=42, stratify=y\n",
|
||||
" X,\n",
|
||||
" y,\n",
|
||||
" test_size=0.2,\n",
|
||||
" random_state=42,\n",
|
||||
" stratify=y,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"scaler = StandardScaler()\n",
|
||||
@@ -451,7 +464,9 @@
|
||||
"model.compile(optimizer=\"adam\", loss=\"binary_crossentropy\")\n",
|
||||
"\n",
|
||||
"callback = tf.keras.callbacks.EarlyStopping(\n",
|
||||
" monitor=\"val_loss\", patience=10, restore_best_weights=True\n",
|
||||
" monitor=\"val_loss\",\n",
|
||||
" patience=10,\n",
|
||||
" restore_best_weights=True,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"history = model.fit(\n",
|
||||
|
||||
Reference in New Issue
Block a user