Skip to content

Commit

Permalink
style: update black rules to allow lines up to 100 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
cmnemoi committed Nov 14, 2023
1 parent bb719af commit 6fb71ef
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 20 deletions.
6 changes: 1 addition & 5 deletions cmnemoi_learn/_abstract_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,4 @@ def _reshape_ndarray(self, ndarray: np.ndarray) -> np.ndarray:
Returns:
np.ndarray: Reshaped ndarray
"""
return (
ndarray.reshape((ndarray.shape[0], 1))
if len(ndarray.shape) == 1
else ndarray
)
return ndarray.reshape((ndarray.shape[0], 1)) if len(ndarray.shape) == 1 else ndarray
7 changes: 1 addition & 6 deletions cmnemoi_learn/classification/_abstract_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,5 @@ def score(self, X: np.ndarray, y: np.ndarray) -> float:
"""
y_pred = self.predict(X)
return float(
np.mean(
[
1 if yi_pred == yi_true else 0
for (yi_pred, yi_true) in zip(y_pred, y)
]
)
np.mean([1 if yi_pred == yi_true else 0 for (yi_pred, yi_true) in zip(y_pred, y)])
)
8 changes: 2 additions & 6 deletions cmnemoi_learn/classification/_logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ def predict(self, X: np.ndarray) -> np.ndarray:
"""
X = self._get_inputs_with_bias_column(X)
probabilities = self._logistic_function(X @ self.weights)
return np.array(
[1 if probability > self.threshold else 0 for probability in probabilities]
)
return np.array([1 if probability > self.threshold else 0 for probability in probabilities])

def _gradient_descent(self, n_iter: int, learning_rate: float = 0.5) -> np.ndarray:
"""Gradient descent algorithm.
Expand All @@ -76,9 +74,7 @@ def _gradient_descent(self, n_iter: int, learning_rate: float = 0.5) -> np.ndarr
weights_gradient = np.array([])
for _ in range(n_iter):
weights_gradient = (
(1 / n)
* self.X.T
@ (self._logistic_function(self.X @ weights) - self.y)
(1 / n) * self.X.T @ (self._logistic_function(self.X @ weights) - self.y)
)
weights = weights - learning_rate * weights_gradient

Expand Down
339 changes: 339 additions & 0 deletions notebooks/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,339 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'matplotlib'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[1], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mnumpy\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mnp\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mmatplotlib\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mpyplot\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mplt\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mdatasets\u001b[39;00m \u001b[39mimport\u001b[39;00m make_blobs, make_circles\n\u001b[1;32m 4\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mmetrics\u001b[39;00m \u001b[39mimport\u001b[39;00m accuracy_score, log_loss\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'matplotlib'"
]
}
],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.datasets import make_blobs, make_circles\n",
"from sklearn.metrics import accuracy_score, log_loss\n",
"from tqdm import tqdm"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fonctions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n",
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'"
]
}
],
"source": [
"def initialisation(dimensions):\n",
" \n",
" parametres = {}\n",
" C = len(dimensions)\n",
"\n",
" np.random.seed(1)\n",
"\n",
" for c in range(1, C):\n",
" parametres['W' + str(c)] = np.random.randn(dimensions[c], dimensions[c - 1])\n",
" parametres['b' + str(c)] = np.random.randn(dimensions[c], 1)\n",
"\n",
" return parametres"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n",
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'"
]
}
],
"source": [
"def forward_propagation(X, parametres):\n",
" \n",
" activations = {'A0': X}\n",
"\n",
" C = len(parametres) // 2\n",
"\n",
" for c in range(1, C + 1):\n",
"\n",
" Z = parametres['W' + str(c)].dot(activations['A' + str(c - 1)]) + parametres['b' + str(c)]\n",
" activations['A' + str(c)] = 1 / (1 + np.exp(-Z))\n",
"\n",
" return activations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n",
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'"
]
}
],
"source": [
"def back_propagation(y, parametres, activations):\n",
"\n",
" m = y.shape[1]\n",
" C = len(parametres) // 2\n",
"\n",
" dZ = activations['A' + str(C)] - y\n",
" gradients = {}\n",
"\n",
" for c in reversed(range(1, C + 1)):\n",
" gradients['dW' + str(c)] = 1/m * np.dot(dZ, activations['A' + str(c - 1)].T)\n",
" gradients['db' + str(c)] = 1/m * np.sum(dZ, axis=1, keepdims=True)\n",
" if c > 1:\n",
" dZ = np.dot(parametres['W' + str(c)].T, dZ) * activations['A' + str(c - 1)] * (1 - activations['A' + str(c - 1)])\n",
"\n",
" return gradients"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n",
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'"
]
}
],
"source": [
"def update(gradients, parametres, learning_rate):\n",
"\n",
" C = len(parametres) // 2\n",
"\n",
" for c in range(1, C + 1):\n",
" parametres['W' + str(c)] = parametres['W' + str(c)] - learning_rate * gradients['dW' + str(c)]\n",
" parametres['b' + str(c)] = parametres['b' + str(c)] - learning_rate * gradients['db' + str(c)]\n",
"\n",
" return parametres"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n",
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'"
]
}
],
"source": [
"def predict(X, parametres):\n",
" activations = forward_propagation(X, parametres)\n",
" C = len(parametres) // 2\n",
" Af = activations['A' + str(C)]\n",
" return Af >= 0.5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n",
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'"
]
}
],
"source": [
"def deep_neural_network(X, y, hidden_layers = (16, 16, 16), learning_rate = 0.001, n_iter = 3000):\n",
" \n",
" # initialisation parametres\n",
" dimensions = list(hidden_layers)\n",
" dimensions.insert(0, X.shape[0])\n",
" dimensions.append(y.shape[0])\n",
" np.random.seed(1)\n",
" parametres = initialisation(dimensions)\n",
"\n",
" # tableau numpy contenant les futures accuracy et log_loss\n",
" training_history = np.zeros((int(n_iter), 2))\n",
"\n",
" C = len(parametres) // 2\n",
"\n",
" # gradient descent\n",
" for i in tqdm(range(n_iter)):\n",
"\n",
" activations = forward_propagation(X, parametres)\n",
" gradients = back_propagation(y, parametres, activations)\n",
" parametres = update(gradients, parametres, learning_rate)\n",
" Af = activations['A' + str(C)]\n",
"\n",
" # calcul du log_loss et de l'accuracy\n",
" training_history[i, 0] = (log_loss(y.flatten(), Af.flatten()))\n",
" y_pred = predict(X, parametres)\n",
" training_history[i, 1] = (accuracy_score(y.flatten(), y_pred.flatten()))\n",
"\n",
" # Plot courbe d'apprentissage\n",
" plt.figure(figsize=(12, 4))\n",
" plt.subplot(1, 2, 1)\n",
" plt.plot(training_history[:, 0], label='train loss')\n",
" plt.legend()\n",
" plt.subplot(1, 2, 2)\n",
" plt.plot(training_history[:, 1], label='train acc')\n",
" plt.legend()\n",
" plt.show()\n",
"\n",
" return training_history"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n",
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'"
]
}
],
"source": [
"X, y = make_circles(n_samples=100, noise=0.1, factor=0.3, random_state=0)\n",
"X = X.T\n",
"y = y.reshape((1, y.shape[0]))\n",
"\n",
"print('dimensions de X:', X.shape)\n",
"print('dimensions de y:', y.shape)\n",
"\n",
"plt.scatter(X[0, :], X[1, :], c=y, cmap='summer')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n",
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'"
]
}
],
"source": [
"deep_neural_network(X, y, hidden_layers = (16, 16, 16), learning_rate = 0.1, n_iter = 3000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n",
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'"
]
}
],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "038c04557dfd72b4d6039cb7951b93ffe7520921b6515cb88d8784deedfaf89f"
},
"kernelspec": {
"display_name": "Python 3.7.9 ('base')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pytest = "^7.4.0"
pytest-cov = "^4.1.0"
scikit-learn = "^1.3.0"

[tool.black]
line-length = 100

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Loading

0 comments on commit 6fb71ef

Please sign in to comment.