From 51a5dcc8eedb91ab057928996bc04db7e0b91f6e Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Mon, 15 Jul 2024 12:07:47 +0200 Subject: [PATCH] Run black on all the notebooks - we use black's default settings - some cells are NOT kept in black's format to: - increase readability - or show Python's flexibility with regard to style --- .../00_content_arithmetic.ipynb | 4 +- .../05_content_functions.ipynb | 2 +- 01_scientific_stack/00_content_numpy.ipynb | 10 +-- 01_scientific_stack/02_content_pandas.ipynb | 68 ++++++------------- .../03_exercises_simple_queries.ipynb | 2 +- 03_classification/00_content.ipynb | 26 +++---- 6 files changed, 45 insertions(+), 67 deletions(-) diff --git a/00_python_in_a_nutshell/00_content_arithmetic.ipynb b/00_python_in_a_nutshell/00_content_arithmetic.ipynb index 4b71d10..d000eaf 100644 --- a/00_python_in_a_nutshell/00_content_arithmetic.ipynb +++ b/00_python_in_a_nutshell/00_content_arithmetic.ipynb @@ -123,7 +123,7 @@ } ], "source": [ - "2 ** 3" + "2**3" ] }, { @@ -143,7 +143,7 @@ } ], "source": [ - "2 * 2 ** 3" + "2 * 2**3" ] }, { diff --git a/00_python_in_a_nutshell/05_content_functions.ipynb b/00_python_in_a_nutshell/05_content_functions.ipynb index aed2f4c..d3b0ff1 100644 --- a/00_python_in_a_nutshell/05_content_functions.ipynb +++ b/00_python_in_a_nutshell/05_content_functions.ipynb @@ -385,7 +385,7 @@ ], "source": [ "for number in numbers:\n", - " square = number ** 2\n", + " square = number**2\n", " print(\"The square of\", number, \"is\", square)" ] }, diff --git a/01_scientific_stack/00_content_numpy.ipynb b/01_scientific_stack/00_content_numpy.ipynb index 03b6835..7ad92fe 100644 --- a/01_scientific_stack/00_content_numpy.ipynb +++ b/01_scientific_stack/00_content_numpy.ipynb @@ -282,10 +282,12 @@ } ], "source": [ - "m1 = np.array([\n", - " [1, 2, 3, 4, 5],\n", - " [6, 7, 8, 9, 10],\n", - "])\n", + "m1 = np.array(\n", + " [\n", + " [1, 2, 3, 4, 5],\n", + " [6, 7, 8, 9, 10],\n", + " ]\n", + ")\n", "\n", "m1" ] diff --git a/01_scientific_stack/02_content_pandas.ipynb b/01_scientific_stack/02_content_pandas.ipynb index 1c82549..2adc9dd 100644 --- a/01_scientific_stack/02_content_pandas.ipynb +++ b/01_scientific_stack/02_content_pandas.ipynb @@ -1852,10 +1852,7 @@ } ], "source": [ - "df.loc[\n", - " 200300:200800,\n", - " [\"o_street\", \"o_zip\", \"o_city\", \"o_latitude\", \"o_longitude\"]\n", - "]" + "df.loc[200300:200800, [\"o_street\", \"o_zip\", \"o_city\", \"o_latitude\", \"o_longitude\"]]" ] }, { @@ -1982,11 +1979,13 @@ "metadata": {}, "outputs": [], "source": [ - "df = df.astype({\n", - " \"pickup_at\": \"datetime64[ns]\",\n", - " \"delivery_at\": \"datetime64[ns]\",\n", - " \"cancelled\": bool,\n", - "})" + "df = df.astype(\n", + " {\n", + " \"pickup_at\": \"datetime64[ns]\",\n", + " \"delivery_at\": \"datetime64[ns]\",\n", + " \"cancelled\": bool,\n", + " }\n", + ")" ] }, { @@ -2686,7 +2685,7 @@ "source": [ "df.loc[\n", " max_a_table,\n", - " [\"customer_id\", \"d_street\", \"d_zip\", \"d_city\", \"d_latitude\", \"d_longitude\"]\n", + " [\"customer_id\", \"d_street\", \"d_zip\", \"d_city\", \"d_latitude\", \"d_longitude\"],\n", "].head()" ] }, @@ -2821,12 +2820,10 @@ " max_a_table\n", " &\n", " (\n", - " (df[\"d_latitude\"] > 44.85)\n", - " |\n", - " (df[\"d_longitude\"] < -0.59)\n", - " ) \n", + " (df[\"d_latitude\"] > 44.85) | (df[\"d_longitude\"] < -0.59)\n", + " )\n", " ),\n", - " [\"customer_id\", \"d_street\", \"d_zip\", \"d_city\", \"d_latitude\", \"d_longitude\"]\n", + " [\"customer_id\", \"d_street\", \"d_zip\", \"d_city\", \"d_latitude\", \"d_longitude\"],\n", "].head()" ] }, @@ -2933,12 +2930,8 @@ ], "source": [ "df.loc[\n", - " (\n", - " max_a_table\n", - " &\n", - " df[\"customer_id\"].isin([6037, 79900, 80095])\n", - " ),\n", - " [\"placed_at\", \"customer_id\", \"d_street\", \"d_zip\", \"d_city\", \"total\"]\n", + " (max_a_table & df[\"customer_id\"].isin([6037, 79900, 80095])),\n", + " [\"placed_at\", \"customer_id\", \"d_street\", \"d_zip\", \"d_city\", \"total\"],\n", "].head()" ] }, @@ -3067,12 +3060,8 @@ ], "source": [ "df.loc[\n", - " (\n", - " max_a_table\n", - " &\n", - " ~df[\"customer_id\"].isin([6037, 79900, 80095])\n", - " ),\n", - " [\"placed_at\", \"customer_id\", \"d_street\", \"d_zip\", \"d_city\", \"total\"]\n", + " (max_a_table & ~df[\"customer_id\"].isin([6037, 79900, 80095])),\n", + " [\"placed_at\", \"customer_id\", \"d_street\", \"d_zip\", \"d_city\", \"total\"],\n", "].head()" ] }, @@ -3166,10 +3155,7 @@ } ], "source": [ - "df.loc[\n", - " max_a_table,\n", - " \"customer_id\"\n", - "].unique()" + "df.loc[max_a_table, \"customer_id\"].unique()" ] }, { @@ -3289,10 +3275,7 @@ } ], "source": [ - "df.loc[\n", - " max_a_table,\n", - " \"total\"\n", - "].sum() / 100" + "df.loc[max_a_table, \"total\"].sum() / 100" ] }, { @@ -3352,10 +3335,7 @@ } ], "source": [ - "df.loc[\n", - " max_a_table,\n", - " \"total\"\n", - "].min() / 100" + "df.loc[max_a_table, \"total\"].min() / 100" ] }, { @@ -3375,10 +3355,7 @@ } ], "source": [ - "df.loc[\n", - " max_a_table,\n", - " \"total\"\n", - "].max() / 100" + "df.loc[max_a_table, \"total\"].max() / 100" ] }, { @@ -3438,10 +3415,7 @@ } ], "source": [ - "df.loc[\n", - " max_a_table,\n", - " \"total\"\n", - "].mean().round() / 100" + "df.loc[max_a_table, \"total\"].mean().round() / 100" ] } ], diff --git a/01_scientific_stack/03_exercises_simple_queries.ipynb b/01_scientific_stack/03_exercises_simple_queries.ipynb index 8043dce..e2e3259 100644 --- a/01_scientific_stack/03_exercises_simple_queries.ipynb +++ b/01_scientific_stack/03_exercises_simple_queries.ipynb @@ -56,7 +56,7 @@ " \"orders.csv\",\n", " index_col=\"order_id\",\n", " dtype={\"cancelled\": bool},\n", - " parse_dates=[\"placed_at\", \"pickup_at\", \"delivery_at\"]\n", + " parse_dates=[\"placed_at\", \"pickup_at\", \"delivery_at\"],\n", ")" ] }, diff --git a/03_classification/00_content.ipynb b/03_classification/00_content.ipynb index 090aace..5c3d4c4 100644 --- a/03_classification/00_content.ipynb +++ b/03_classification/00_content.ipynb @@ -518,17 +518,17 @@ ], "source": [ "feature_index = 2\n", - "colors = ['blue', 'red', 'green']\n", + "colors = [\"blue\", \"red\", \"green\"]\n", "\n", "for label, color in zip(range(len(iris.target_names)), colors):\n", " plt.hist(\n", - " iris.data[iris.target==label, feature_index], \n", + " iris.data[iris.target == label, feature_index],\n", " label=iris.target_names[label],\n", " color=color,\n", " )\n", "\n", "plt.xlabel(iris.feature_names[feature_index])\n", - "plt.legend(loc='upper right')\n", + "plt.legend(loc=\"upper right\")\n", "plt.show()" ] }, @@ -559,19 +559,19 @@ "first_feature_index = 1\n", "second_feature_index = 0\n", "\n", - "colors = ['blue', 'red', 'green']\n", + "colors = [\"blue\", \"red\", \"green\"]\n", "\n", "for label, color in zip(range(len(iris.target_names)), colors):\n", " plt.scatter(\n", - " iris.data[iris.target==label, first_feature_index], \n", - " iris.data[iris.target==label, second_feature_index],\n", + " iris.data[iris.target == label, first_feature_index],\n", + " iris.data[iris.target == label, second_feature_index],\n", " label=iris.target_names[label],\n", " c=color,\n", " )\n", "\n", "plt.xlabel(iris.feature_names[first_feature_index])\n", "plt.ylabel(iris.feature_names[second_feature_index])\n", - "plt.legend(loc='upper left')\n", + "plt.legend(loc=\"upper left\")\n", "plt.show()" ] }, @@ -772,7 +772,9 @@ } ], "source": [ - "X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, test_size=0.3, random_state=42, stratify=y)\n", + "X_train, X_test, y_train, y_test = train_test_split(\n", + " X, y, train_size=0.7, test_size=0.3, random_state=42, stratify=y\n", + ")\n", "\n", "y_test" ] @@ -1040,12 +1042,12 @@ " X_test[incorrect_idx, first_feature_index],\n", " X_test[incorrect_idx, second_feature_index],\n", " color=\"darkred\",\n", - " label='misclassified',\n", + " label=\"misclassified\",\n", ")\n", "\n", - "plt.xlabel('sepal width [cm]')\n", - "plt.ylabel('petal length [cm]')\n", - "plt.legend(loc='best')\n", + "plt.xlabel(\"sepal width [cm]\")\n", + "plt.ylabel(\"petal length [cm]\")\n", + "plt.legend(loc=\"best\")\n", "plt.title(\"Iris Classification results\")\n", "plt.show()" ]