{ "cells": [ { "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": {}, "inputWidgets": {}, "nuid": "a6a69339-37e8-4cb6-863a-858e6df8872f", "showTitle": false, "title": "" }, "id": "o5Iixw4vHWG9" }, "source": [ "# Objective\n", "\n", "To illustrate a supervised learning workflow for classification tasks." ] }, { "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": {}, "inputWidgets": {}, "nuid": "f3a9806a-355a-41fe-9b2a-0bda43aedd4e", "showTitle": false, "title": "" }, "id": "niLZjnkCHWG_" }, "source": [ "\n", "# Setup" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "69c6950d-190a-4c7d-a5a2-a42164f4833b", "showTitle": false, "title": "" }, "id": "SEBLSdL-HWHA" }, "outputs": [], "source": [ "import sklearn\n", "import joblib\n", "\n", "from sklearn.datasets import fetch_openml\n", "\n", "from sklearn.preprocessing import StandardScaler, OneHotEncoder\n", "from sklearn.compose import make_column_transformer\n", "\n", "from sklearn.pipeline import make_pipeline\n", "\n", "from sklearn.model_selection import train_test_split, RandomizedSearchCV\n", "\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.metrics import accuracy_score, classification_report" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "d006e342-db1e-42d7-95e1-7cfed889cb59", "showTitle": false, "title": "" }, "id": "kzG9kVi4HWHB" }, "outputs": [], "source": [ "sklearn.set_config(display='diagram')" ] }, { "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": {}, "inputWidgets": {}, "nuid": "28fdb1c5-213d-4bfc-bf78-c14c60f0f365", "showTitle": false, "title": "" }, "id": "bQrbzi5RHWHC" }, "source": [ "# Data" ] }, { "cell_type": "markdown", "source": [ "For this session consider the case of predicting machinery failure based on the quality of the machinery and its wear and tear. Such predictions often require an expert intervention on the shopfloor and result in high dependencies on the expert." ], "metadata": { "id": "knJjGZz-QVbr" } }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "de73d795-30fe-4891-8a22-2356863aaef3", "showTitle": false, "title": "" }, "id": "h5ZOCZjlHWHC" }, "outputs": [], "source": [ "dataset = fetch_openml(data_id=42890, as_frame=True, parser=\"auto\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "11e65af7-7b11-40fb-acf1-8c253e269ac6", "showTitle": false, "title": "" }, "id": "gZ9nYAyJHWHD" }, "outputs": [], "source": [ "data_df = dataset.data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "20762caf-3731-4797-9fd2-8400061aae8a", "showTitle": false, "title": "" }, "id": "Y_tEmVPFHWHD" }, "outputs": [], "source": [ "target = 'Machine failure'\n", "numeric_features = [\n", " 'Air temperature [K]',\n", " 'Process temperature [K]',\n", " 'Rotational speed [rpm]',\n", " 'Torque [Nm]',\n", " 'Tool wear [min]'\n", "]\n", "categorical_features = ['Type']" ] }, { "cell_type": "markdown", "source": [ "## Attribute Information:\n", "\n", "The dataset consists of 10 000 data points stored as rows with 14 features in columns\n", "\n", "- UID: unique identifier ranging from 1 to 10000\n", "- product ID: consisting of a letter L, M, or H for low (50% of all products), medium (30%) and high (20%) as product quality variants and a variant-specific serial number\n", "- air temperature [K]: generated using a random walk process later normalized to a standard deviation of 2 K around 300 K\n", "- process temperature [K]: generated using a random walk process normalized to a standard deviation of 1 K, added to the air temperature plus 10 K.\n", "- rotational speed [rpm]: calculated from a power of 2860 W, overlaid with a normally distributed noise\n", "- torque [Nm]: torque values are normally distributed around 40 Nm with a f = 10 Nm and no negative values.\n", "- tool wear [min]: The quality variants H/M/L add 5/3/2 minutes of tool wear to the used tool in the process.\n", "- 'machine failure' label that indicates, whether the machine has failed in this particular datapoint for any of the following failure modes are true.\n", "\n", "The machine failure consists of five independent failure modes\n", "tool wear failure (TWF): the tool will be replaced of fail at a randomly selected tool wear time between 200-240 mins (120 times in our dataset). At this point in time, the tool is replaced 69 times, and fails 51 times (randomly assigned).\n", "\n", "- heat dissipation failure (HDF): heat dissipation causes a process failure, if the difference between air- and process temperature is below 8.6 K and the tools rotational speed is below 1380 rpm. This is the case for 115 data points.\n", "- power failure (PWF): the product of torque and rotational speed (in rad/s) equals the power required for the process. If this power is below 3500 W or above 9000 W, the process fails, which is the case 95 times in our dataset.\n", "- overstrain failure (OSF): if the product of tool wear and torque exceeds 11,000 minNm for the L product variant (12,000 M, 13,000 H), the process fails due to overstrain. This is true for 98 datapoints.\n", "- random failures (RNF): each process has a chance of 0,1 % to fail regardless of its process parameters. This is the case for only 5 datapoints, less than could be expected for 10,000 datapoints in our dataset.\n", "\n", "If at least one of the above failure modes is true, the process fails and the 'machine failure' label is set to 1." ], "metadata": { "id": "tTN4lzHJQaXR" } }, { "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": {}, "inputWidgets": {}, "nuid": "c1f6d4b1-8956-4c8c-99c6-f616b6ffbbb7", "showTitle": false, "title": "" }, "id": "iT25LmvoHWHD" }, "source": [ "# EDA" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": {}, "inputWidgets": {}, "nuid": "13f93578-a8e7-4cd1-b599-af01cde98947", "showTitle": false, "title": "" }, "id": "pRTCdlYTHWHD", "outputId": "ae108d43-1a7c-46ea-ecf3-bb1952554fd2", "colab": { "base_uri": "https://localhost:8080/", "height": 300 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " Air temperature [K] Process temperature [K] Rotational speed [rpm] \\\n", "count 10000.000000 10000.000000 10000.000000 \n", "mean 300.004930 310.005560 1538.776100 \n", "std 2.000259 1.483734 179.284096 \n", "min 295.300000 305.700000 1168.000000 \n", "25% 298.300000 308.800000 1423.000000 \n", "50% 300.100000 310.100000 1503.000000 \n", "75% 301.500000 311.100000 1612.000000 \n", "max 304.500000 313.800000 2886.000000 \n", "\n", " Torque [Nm] Tool wear [min] \n", "count 10000.000000 10000.000000 \n", "mean 39.986910 107.951000 \n", "std 9.968934 63.654147 \n", "min 3.800000 0.000000 \n", "25% 33.200000 53.000000 \n", "50% 40.100000 108.000000 \n", "75% 46.800000 162.000000 \n", "max 76.600000 253.000000 " ], "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Air temperature [K]Process temperature [K]Rotational speed [rpm]Torque [Nm]Tool wear [min]
count10000.00000010000.00000010000.00000010000.00000010000.000000
mean300.004930310.0055601538.77610039.986910107.951000
std2.0002591.483734179.2840969.96893463.654147
min295.300000305.7000001168.0000003.8000000.000000
25%298.300000308.8000001423.00000033.20000053.000000
50%300.100000310.1000001503.00000040.100000108.000000
75%301.500000311.1000001612.00000046.800000162.000000
max304.500000313.8000002886.00000076.600000253.000000
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "summary": "{\n \"name\": \"data_df[numeric_features]\",\n \"rows\": 8,\n \"fields\": [\n {\n \"column\": \"Air temperature [K]\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3446.1125878918356,\n \"min\": 2.000258682915751,\n \"max\": 10000.0,\n \"num_unique_values\": 8,\n \"samples\": [\n 300.00493,\n 300.1,\n 10000.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Process temperature [K]\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3443.232448462189,\n \"min\": 1.4837342191657208,\n \"max\": 10000.0,\n \"num_unique_values\": 8,\n \"samples\": [\n 310.00556,\n 310.1,\n 10000.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Rotational speed [rpm]\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3103.301433905864,\n \"min\": 179.2840959134266,\n \"max\": 10000.0,\n \"num_unique_values\": 8,\n \"samples\": [\n 1538.7761,\n 1503.0,\n 10000.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Torque [Nm]\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3522.9553380464763,\n \"min\": 3.8,\n \"max\": 10000.0,\n \"num_unique_values\": 8,\n \"samples\": [\n 39.986909999999995,\n 40.1,\n 10000.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Tool wear [min]\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3498.6028684989537,\n \"min\": 0.0,\n \"max\": 10000.0,\n \"num_unique_values\": 8,\n \"samples\": [\n 107.951,\n 108.0,\n 10000.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" } }, "metadata": {}, "execution_count": 6 } ], "source": [ "data_df[numeric_features].describe()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": {}, "inputWidgets": {}, "nuid": "888a838c-f16c-408c-bcd6-5aa0f13d943c", "showTitle": false, "title": "" }, "id": "phM5W-m2HWHE", "outputId": "db3783cb-e734-4880-b3bc-0b49ed69bb20", "colab": { "base_uri": "https://localhost:8080/", "height": 175 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " Type\n", "count 10000\n", "unique 3\n", "top L\n", "freq 6000" ], "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Type
count10000
unique3
topL
freq6000
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "summary": "{\n \"name\": \"data_df[categorical_features]\",\n \"rows\": 4,\n \"fields\": [\n {\n \"column\": \"Type\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4,\n \"samples\": [\n 3,\n \"6000\",\n \"10000\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" } }, "metadata": {}, "execution_count": 7 } ], "source": [ "data_df[categorical_features].describe()" ] }, { "cell_type": "code", "source": [ "data_df[target].value_counts()" ], "metadata": { "id": "dVRml2osH8Av", "outputId": "e5b7ecf2-d241-4ed1-f66b-db953dff1f1d", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0 9661\n", "1 339\n", "Name: Machine failure, dtype: int64" ] }, "metadata": {}, "execution_count": 8 } ] }, { "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": {}, "inputWidgets": {}, "nuid": "550dfffd-2a1f-44c5-8d31-23a452896bef", "showTitle": false, "title": "" }, "id": "Cl7T7_jFHWHE" }, "source": [ "# Model Estimation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "0c99d9b0-4771-487b-86fd-9d452118b7ab", "showTitle": false, "title": "" }, "id": "YKuRNqVuHWHE" }, "outputs": [], "source": [ "X = data_df[numeric_features + categorical_features]\n", "y = data_df[target]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "1e90b308-b374-4f65-81b8-598a0bab1d1d", "showTitle": false, "title": "" }, "id": "tbgt_5ZCHWHF" }, "outputs": [], "source": [ "Xtrain, Xtest, ytrain, ytest = train_test_split(\n", " X, y,\n", " test_size=0.2,\n", " random_state=42\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "bffcbcac-0047-4332-b206-9fadb5c78956", "showTitle": false, "title": "" }, "id": "GYnMo95hHWHF" }, "outputs": [], "source": [ "preprocessor = make_column_transformer(\n", " (StandardScaler(), numeric_features),\n", " (OneHotEncoder(handle_unknown='ignore'), categorical_features)\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "76a64681-e124-4eef-ada8-75a8511dc783", "showTitle": false, "title": "" }, "id": "D83BGGp3HWHF" }, "outputs": [], "source": [ "model_logistic_regression = LogisticRegression(n_jobs=-1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "7e566699-0812-46e2-a48b-cf8a4f83c7b6", "showTitle": false, "title": "" }, "id": "3HkzYXsPHWHF" }, "outputs": [], "source": [ "model_pipeline = make_pipeline(\n", " preprocessor,\n", " model_logistic_regression\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "245034db-84c4-406f-b808-dde8860b9084", "showTitle": false, "title": "" }, "id": "E_ZmCrDnHWHG", "outputId": "39bd9532-0181-4509-d069-6b158c3e7875", "colab": { "base_uri": "https://localhost:8080/", "height": 192 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "Pipeline(steps=[('columntransformer',\n", " ColumnTransformer(transformers=[('standardscaler',\n", " StandardScaler(),\n", " ['Air temperature [K]',\n", " 'Process temperature [K]',\n", " 'Rotational speed [rpm]',\n", " 'Torque [Nm]',\n", " 'Tool wear [min]']),\n", " ('onehotencoder',\n", " OneHotEncoder(handle_unknown='ignore'),\n", " ['Type'])])),\n", " ('logisticregression', LogisticRegression(n_jobs=-1))])" ], "text/html": [ "
Pipeline(steps=[('columntransformer',\n",
              "                 ColumnTransformer(transformers=[('standardscaler',\n",
              "                                                  StandardScaler(),\n",
              "                                                  ['Air temperature [K]',\n",
              "                                                   'Process temperature [K]',\n",
              "                                                   'Rotational speed [rpm]',\n",
              "                                                   'Torque [Nm]',\n",
              "                                                   'Tool wear [min]']),\n",
              "                                                 ('onehotencoder',\n",
              "                                                  OneHotEncoder(handle_unknown='ignore'),\n",
              "                                                  ['Type'])])),\n",
              "                ('logisticregression', LogisticRegression(n_jobs=-1))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ] }, "metadata": {}, "execution_count": 14 } ], "source": [ "model_pipeline.fit(Xtrain, ytrain)" ] }, { "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": {}, "inputWidgets": {}, "nuid": "b69edec2-1b51-47d1-89a4-18ed7d7814f7", "showTitle": false, "title": "" }, "id": "vKh6Gg6BHWHG" }, "source": [ "# Model Evaluation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "087eeb21-d77e-4ca1-9f75-414586c18b7d", "showTitle": false, "title": "" }, "id": "yquQSa6KHWHH", "outputId": "4bad9720-4659-42a3-c38f-a10a3f245f10", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([0, 0, 0, ..., 0, 0, 0])" ] }, "metadata": {}, "execution_count": 15 } ], "source": [ "model_pipeline.predict(Xtest)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "841dbbf4-00fe-43bb-86b7-13e184d3759d", "showTitle": false, "title": "" }, "id": "KdXMlBmGHWHH", "outputId": "e35a4a06-b334-4294-b54a-444e536872e4", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0.9735" ] }, "metadata": {}, "execution_count": 16 } ], "source": [ "accuracy_score(ytest, model_pipeline.predict(Xtest))" ] }, { "cell_type": "code", "source": [ "print(classification_report(ytest, model_pipeline.predict(Xtest)))" ], "metadata": { "id": "ZTwKVOtmHr-M", "outputId": "1b93e443-73ff-4d18-f651-0e34abd5c353", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " precision recall f1-score support\n", "\n", " 0 0.98 1.00 0.99 1939\n", " 1 0.67 0.26 0.38 61\n", "\n", " accuracy 0.97 2000\n", " macro avg 0.82 0.63 0.68 2000\n", "weighted avg 0.97 0.97 0.97 2000\n", "\n" ] } ] }, { "cell_type": "markdown", "source": [ "# Hyperparameter Tuning" ], "metadata": { "id": "UGqiCLRnRx8i" } }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "bffcbcac-0047-4332-b206-9fadb5c78956", "showTitle": false, "title": "" }, "id": "D8BdWyOUR46x" }, "outputs": [], "source": [ "preprocessor = make_column_transformer(\n", " (StandardScaler(), numeric_features),\n", " (OneHotEncoder(handle_unknown='ignore'), categorical_features)\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "76a64681-e124-4eef-ada8-75a8511dc783", "showTitle": false, "title": "" }, "id": "creqMx9fR46y" }, "outputs": [], "source": [ "model_logistic_regression = LogisticRegression(n_jobs=-1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { "byteLimit": 2048000, "rowLimit": 10000 }, "inputWidgets": {}, "nuid": "7e566699-0812-46e2-a48b-cf8a4f83c7b6", "showTitle": false, "title": "" }, "id": "bwV3VmWrR46y" }, "outputs": [], "source": [ "model_pipeline = make_pipeline(\n", " preprocessor,\n", " model_logistic_regression\n", ")" ] }, { "cell_type": "code", "source": [ "model_pipeline.named_steps" ], "metadata": { "id": "fSNxALuGR6iT", "outputId": "1c942461-8281-4e55-ec28-e6f34840a300", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'columntransformer': ColumnTransformer(transformers=[('standardscaler', StandardScaler(),\n", " ['Air temperature [K]',\n", " 'Process temperature [K]',\n", " 'Rotational speed [rpm]', 'Torque [Nm]',\n", " 'Tool wear [min]']),\n", " ('onehotencoder',\n", " OneHotEncoder(handle_unknown='ignore'),\n", " ['Type'])]),\n", " 'logisticregression': LogisticRegression(n_jobs=-1)}" ] }, "metadata": {}, "execution_count": 21 } ] }, { "cell_type": "code", "source": [ "param_distribution = {\n", " \"logisticregression__C\": [0.001, 0.01, 0.1, 0.5, 1, 5, 10]\n", "}" ], "metadata": { "id": "GlOJwOE2SA4m" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "rand_search_cv = RandomizedSearchCV(\n", " model_pipeline,\n", " param_distribution,\n", " n_iter=3,\n", " cv=3,\n", " random_state=42\n", ")" ], "metadata": { "id": "e-6kTwZkSfxT" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "rand_search_cv.fit(Xtrain, ytrain)" ], "metadata": { "id": "ycqTA2xKSjU7", "outputId": "67e1ecfc-f048-4251-a702-aa74fd70d065", "colab": { "base_uri": "https://localhost:8080/", "height": 218 } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "RandomizedSearchCV(cv=3,\n", " estimator=Pipeline(steps=[('columntransformer',\n", " ColumnTransformer(transformers=[('standardscaler',\n", " StandardScaler(),\n", " ['Air '\n", " 'temperature '\n", " '[K]',\n", " 'Process '\n", " 'temperature '\n", " '[K]',\n", " 'Rotational '\n", " 'speed '\n", " '[rpm]',\n", " 'Torque '\n", " '[Nm]',\n", " 'Tool '\n", " 'wear '\n", " '[min]']),\n", " ('onehotencoder',\n", " OneHotEncoder(handle_unknown='ignore'),\n", " ['Type'])])),\n", " ('logisticregression',\n", " LogisticRegression(n_jobs=-1))]),\n", " n_iter=3,\n", " param_distributions={'logisticregression__C': [0.001, 0.01,\n", " 0.1, 0.5, 1,\n", " 5, 10]},\n", " random_state=42)" ], "text/html": [ "
RandomizedSearchCV(cv=3,\n",
              "                   estimator=Pipeline(steps=[('columntransformer',\n",
              "                                              ColumnTransformer(transformers=[('standardscaler',\n",
              "                                                                               StandardScaler(),\n",
              "                                                                               ['Air '\n",
              "                                                                                'temperature '\n",
              "                                                                                '[K]',\n",
              "                                                                                'Process '\n",
              "                                                                                'temperature '\n",
              "                                                                                '[K]',\n",
              "                                                                                'Rotational '\n",
              "                                                                                'speed '\n",
              "                                                                                '[rpm]',\n",
              "                                                                                'Torque '\n",
              "                                                                                '[Nm]',\n",
              "                                                                                'Tool '\n",
              "                                                                                'wear '\n",
              "                                                                                '[min]']),\n",
              "                                                                              ('onehotencoder',\n",
              "                                                                               OneHotEncoder(handle_unknown='ignore'),\n",
              "                                                                               ['Type'])])),\n",
              "                                             ('logisticregression',\n",
              "                                              LogisticRegression(n_jobs=-1))]),\n",
              "                   n_iter=3,\n",
              "                   param_distributions={'logisticregression__C': [0.001, 0.01,\n",
              "                                                                  0.1, 0.5, 1,\n",
              "                                                                  5, 10]},\n",
              "                   random_state=42)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ] }, "metadata": {}, "execution_count": 24 } ] }, { "cell_type": "code", "source": [ "rand_search_cv.best_estimator_" ], "metadata": { "id": "J4E01MTHSxkN", "outputId": "bab81e57-2c37-4cca-cf56-14f53a8369f6", "colab": { "base_uri": "https://localhost:8080/", "height": 192 } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "Pipeline(steps=[('columntransformer',\n", " ColumnTransformer(transformers=[('standardscaler',\n", " StandardScaler(),\n", " ['Air temperature [K]',\n", " 'Process temperature [K]',\n", " 'Rotational speed [rpm]',\n", " 'Torque [Nm]',\n", " 'Tool wear [min]']),\n", " ('onehotencoder',\n", " OneHotEncoder(handle_unknown='ignore'),\n", " ['Type'])])),\n", " ('logisticregression', LogisticRegression(C=5, n_jobs=-1))])" ], "text/html": [ "
Pipeline(steps=[('columntransformer',\n",
              "                 ColumnTransformer(transformers=[('standardscaler',\n",
              "                                                  StandardScaler(),\n",
              "                                                  ['Air temperature [K]',\n",
              "                                                   'Process temperature [K]',\n",
              "                                                   'Rotational speed [rpm]',\n",
              "                                                   'Torque [Nm]',\n",
              "                                                   'Tool wear [min]']),\n",
              "                                                 ('onehotencoder',\n",
              "                                                  OneHotEncoder(handle_unknown='ignore'),\n",
              "                                                  ['Type'])])),\n",
              "                ('logisticregression', LogisticRegression(C=5, n_jobs=-1))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ] }, "metadata": {}, "execution_count": 25 } ] }, { "cell_type": "code", "source": [ "rand_search_cv.best_score_" ], "metadata": { "id": "PL_j1mxxS8hm", "outputId": "80c312d3-965b-479e-f66f-69ec03a49183", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0.9691250146619894" ] }, "metadata": {}, "execution_count": 26 } ] }, { "cell_type": "code", "source": [ "print(classification_report(ytest, rand_search_cv.best_estimator_.predict(Xtest)))" ], "metadata": { "id": "n06s0FlJUKmN", "outputId": "a797f84d-ea72-46b9-b83e-cb9f5deef346", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " precision recall f1-score support\n", "\n", " 0 0.98 0.99 0.99 1939\n", " 1 0.63 0.28 0.39 61\n", "\n", " accuracy 0.97 2000\n", " macro avg 0.80 0.64 0.69 2000\n", "weighted avg 0.97 0.97 0.97 2000\n", "\n" ] } ] }, { "cell_type": "markdown", "source": [ "# Serialization" ], "metadata": { "id": "lqkUvTHzO3UH" } }, { "cell_type": "code", "source": [ "!pip show scikit-learn" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "q1VtbaO3O1E4", "outputId": "58a62101-9aff-4a99-a022-90791aa1ea3c" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Name: scikit-learn\n", "Version: 1.2.2\n", "Summary: A set of python modules for machine learning and data mining\n", "Home-page: http://scikit-learn.org\n", "Author: \n", "Author-email: \n", "License: new BSD\n", "Location: /usr/local/lib/python3.10/dist-packages\n", "Requires: joblib, numpy, scipy, threadpoolctl\n", "Required-by: bigframes, fastai, imbalanced-learn, librosa, mlxtend, qudida, sklearn-pandas, yellowbrick\n" ] } ] }, { "cell_type": "code", "source": [ "%%writefile requirements.txt\n", "scikit-learn==1.2.2" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2O3H_GLiO6KG", "outputId": "226b92eb-6773-4343-ad0c-d824d9b7da8d" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Writing requirements.txt\n" ] } ] }, { "cell_type": "code", "source": [ "%%writefile train.py\n", "\n", "import joblib\n", "\n", "from sklearn.datasets import fetch_openml\n", "\n", "from sklearn.preprocessing import StandardScaler, OneHotEncoder\n", "from sklearn.compose import make_column_transformer\n", "\n", "from sklearn.pipeline import make_pipeline\n", "\n", "from sklearn.model_selection import train_test_split, RandomizedSearchCV\n", "\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.metrics import accuracy_score, classification_report\n", "\n", "dataset = fetch_openml(data_id=42890, as_frame=True, parser=\"auto\")\n", "\n", "data_df = dataset.data\n", "\n", "target = 'Machine failure'\n", "numeric_features = [\n", " 'Air temperature [K]',\n", " 'Process temperature [K]',\n", " 'Rotational speed [rpm]',\n", " 'Torque [Nm]',\n", " 'Tool wear [min]'\n", "]\n", "categorical_features = ['Type']\n", "\n", "print(\"Creating data subsets\")\n", "\n", "X = data_df[numeric_features + categorical_features]\n", "y = data_df[target]\n", "\n", "Xtrain, Xtest, ytrain, ytest = train_test_split(\n", " X, y,\n", " test_size=0.2,\n", " random_state=42\n", ")\n", "\n", "preprocessor = make_column_transformer(\n", " (StandardScaler(), numeric_features),\n", " (OneHotEncoder(handle_unknown='ignore'), categorical_features)\n", ")\n", "\n", "model_logistic_regression = LogisticRegression(n_jobs=-1)\n", "\n", "print(\"Estimating Best Model Pipeline\")\n", "\n", "model_pipeline = make_pipeline(\n", " preprocessor,\n", " model_logistic_regression\n", ")\n", "\n", "param_distribution = {\n", " \"logisticregression__C\": [0.001, 0.01, 0.1, 0.5, 1, 5, 10]\n", "}\n", "\n", "rand_search_cv = RandomizedSearchCV(\n", " model_pipeline,\n", " param_distribution,\n", " n_iter=3,\n", " cv=3,\n", " random_state=42\n", ")\n", "\n", "rand_search_cv.fit(Xtrain, ytrain)\n", "\n", "print(\"Logging Metrics\")\n", "print(f\"Accuracy: {rand_search_cv.best_score_}\")\n", "\n", "print(\"Serializing Model\")\n", "\n", "saved_model_path = \"model.joblib\"\n", "\n", "joblib.dump(rand_search_cv.best_estimator_, saved_model_path)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "gAMAcWTYO8_s", "outputId": "fa0903c7-c770-4d2b-c75f-b408f561ca5f" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Writing train.py\n" ] } ] }, { "cell_type": "code", "source": [ "!python train.py" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "T-JTqunHPckf", "outputId": "1aff9f9a-cb25-47cc-8da7-59edde4afc95" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Creating data subsets\n", "Estimating Best Model Pipeline\n", "Logging Metrics\n", "Accuracy: 0.9691250146619894\n", "Serializing Model\n" ] } ] }, { "cell_type": "markdown", "source": [ "# Test Predictions" ], "metadata": { "id": "lFC4HfHjMR2G" } }, { "cell_type": "code", "source": [ "saved_model = joblib.load(\"model.joblib\")" ], "metadata": { "id": "uNR7Dxj2MTM9" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "saved_model" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 192 }, "id": "LoPE6PZUM7Uh", "outputId": "a05d48a1-2a8b-460b-cacc-38efc4c6928e" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "Pipeline(steps=[('columntransformer',\n", " ColumnTransformer(transformers=[('standardscaler',\n", " StandardScaler(),\n", " ['Air temperature [K]',\n", " 'Process temperature [K]',\n", " 'Rotational speed [rpm]',\n", " 'Torque [Nm]',\n", " 'Tool wear [min]']),\n", " ('onehotencoder',\n", " OneHotEncoder(handle_unknown='ignore'),\n", " ['Type'])])),\n", " ('logisticregression', LogisticRegression(C=5, n_jobs=-1))])" ], "text/html": [ "
Pipeline(steps=[('columntransformer',\n",
              "                 ColumnTransformer(transformers=[('standardscaler',\n",
              "                                                  StandardScaler(),\n",
              "                                                  ['Air temperature [K]',\n",
              "                                                   'Process temperature [K]',\n",
              "                                                   'Rotational speed [rpm]',\n",
              "                                                   'Torque [Nm]',\n",
              "                                                   'Tool wear [min]']),\n",
              "                                                 ('onehotencoder',\n",
              "                                                  OneHotEncoder(handle_unknown='ignore'),\n",
              "                                                  ['Type'])])),\n",
              "                ('logisticregression', LogisticRegression(C=5, n_jobs=-1))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ] }, "metadata": {}, "execution_count": 33 } ] }, { "cell_type": "code", "source": [ "saved_model.predict(Xtest)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "MzpxBjtSM9f-", "outputId": "7795684b-11fa-4a5a-d1ca-5c4774742277" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([0, 0, 0, ..., 0, 0, 0])" ] }, "metadata": {}, "execution_count": 34 } ] } ], "metadata": { "application/vnd.databricks.v1+notebook": { "dashboards": [], "language": "python", "notebookMetadata": { "pythonIndentUnit": 4 }, "notebookName": "machine_failure_prediction", "widgets": {} }, "colab": { "provenance": [] }, "language_info": { "name": "python" }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "nbformat": 4, "nbformat_minor": 0 }