|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Adding keyword labels to O&M data\n", |
| 9 | + "This notebook demonstrates the use of the `pvops.classify.get_attributes_from_keywords` module for adding asset labels based off O&M notes." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import pandas as pd\n", |
| 19 | + "from sklearn.metrics import accuracy_score\n", |
| 20 | + "\n", |
| 21 | + "from pvops.text import utils, preprocess\n", |
| 22 | + "from pvops.text.classify import get_attributes_from_keywords\n", |
| 23 | + "from pvops.text.visualize import visualize_classification_confusion_matrix" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "attachments": {}, |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "# Step 0: Get sample data, remap assets" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": null, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "# pull in sample data and remap assets for ease of comparison\n", |
| 41 | + "\n", |
| 42 | + "om_df = pd.read_csv('example_data/example_ML_ticket_data.csv')\n", |
| 43 | + "col_dict = {\n", |
| 44 | + " \"data\" : \"CompletionDesc\",\n", |
| 45 | + " \"eventstart\" : \"Date_EventStart\",\n", |
| 46 | + " \"save_data_column\" : \"processed_data\",\n", |
| 47 | + " \"save_date_column\" : \"processed_date\",\n", |
| 48 | + " \"attribute_col\" : \"Asset\",\n", |
| 49 | + " \"predicted_col\" : \"Keyword_Asset\",\n", |
| 50 | + " \"remapping_col_from\": \"in\",\n", |
| 51 | + " \"remapping_col_to\": \"out_\"\n", |
| 52 | + "}\n", |
| 53 | + "\n", |
| 54 | + "# remap assets\n", |
| 55 | + "remapping_df = pd.read_csv('example_data/remappings_asset.csv')\n", |
| 56 | + "remapping_df['out_'] = remapping_df['out_'].replace({'met station': 'met',\n", |
| 57 | + " 'energy storage': 'battery',\n", |
| 58 | + " 'energy meter': 'meter'})\n", |
| 59 | + "om_df = utils.remap_attributes(om_df, remapping_df, col_dict, allow_missing_mappings=True)\n", |
| 60 | + "om_df.head()" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "attachments": {}, |
| 65 | + "cell_type": "markdown", |
| 66 | + "metadata": {}, |
| 67 | + "source": [ |
| 68 | + "# Step 1: Text preprocessing" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": null, |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [], |
| 76 | + "source": [ |
| 77 | + "# preprocessing steps\n", |
| 78 | + "om_df[col_dict['attribute_col']] = om_df.apply(lambda row: row[col_dict['attribute_col']].lower(), axis=1)\n", |
| 79 | + "om_df = preprocess.preprocessor(om_df, lst_stopwords=[], col_dict=col_dict, print_info=False, extract_dates_only=False)\n", |
| 80 | + "\n", |
| 81 | + "DATA_COL = col_dict['data']\n", |
| 82 | + "om_df[DATA_COL] = om_df['processed_data']\n", |
| 83 | + "\n", |
| 84 | + "# replace terms\n", |
| 85 | + "equipment_df = pd.read_csv('~/pvOps/examples/example_data/mappings_equipment.csv')\n", |
| 86 | + "pv_terms_df = pd.read_csv('~/pvOps/examples/example_data/mappings_pv_terms.csv')\n", |
| 87 | + "pv_reference_df = pd.concat([equipment_df, pv_terms_df])\n", |
| 88 | + "om_df = utils.remap_words_in_text(om_df=om_df, remapping_df=pv_reference_df, remapping_col_dict=col_dict)\n", |
| 89 | + "\n", |
| 90 | + "om_df.head()" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "attachments": {}, |
| 95 | + "cell_type": "markdown", |
| 96 | + "metadata": {}, |
| 97 | + "source": [ |
| 98 | + "# Step 2: Search for keywords to use as labels" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "metadata": {}, |
| 105 | + "outputs": [], |
| 106 | + "source": [ |
| 107 | + "# add asset labels from keyword reference dict\n", |
| 108 | + "om_df = get_attributes_from_keywords(om_df=om_df,\n", |
| 109 | + " col_dict=col_dict,\n", |
| 110 | + " reference_df=equipment_df)\n", |
| 111 | + "om_df.head()" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "attachments": {}, |
| 116 | + "cell_type": "markdown", |
| 117 | + "metadata": {}, |
| 118 | + "source": [ |
| 119 | + "# Step 3: Metrics" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": null, |
| 125 | + "metadata": {}, |
| 126 | + "outputs": [], |
| 127 | + "source": [ |
| 128 | + "# get accuracy measures and count metrics\n", |
| 129 | + "PREDICT_COL = col_dict['predicted_col']\n", |
| 130 | + "LABEL_COL = col_dict['attribute_col']\n", |
| 131 | + "\n", |
| 132 | + "# entries with some keyword over interest, over all entries\n", |
| 133 | + "label_count = om_df[PREDICT_COL].count() / len(om_df)\n", |
| 134 | + "\n", |
| 135 | + "# replace 'Other' values with 'Unknown'\n", |
| 136 | + "om_df[LABEL_COL] = om_df[LABEL_COL].replace('other', 'unknown')\n", |
| 137 | + "# replace NaN values to use accuracy score\n", |
| 138 | + "om_df[[LABEL_COL, PREDICT_COL]] = om_df[[LABEL_COL, PREDICT_COL]].fillna('unknown')\n", |
| 139 | + "acc_score = accuracy_score(y_true=om_df[LABEL_COL], y_pred=om_df[PREDICT_COL])\n", |
| 140 | + "\n", |
| 141 | + "msg = f'{label_count:.2%} of entries had a keyword of interest, with {acc_score:.2%} accuracy.'\n", |
| 142 | + "print(msg)" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "attachments": {}, |
| 147 | + "cell_type": "markdown", |
| 148 | + "metadata": {}, |
| 149 | + "source": [ |
| 150 | + "# Step 4: Visualization" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": null, |
| 156 | + "metadata": {}, |
| 157 | + "outputs": [], |
| 158 | + "source": [ |
| 159 | + "# plot confusion matrix\n", |
| 160 | + "title = 'Confusion Matrix of Actual and Predicted Asset Labels'\n", |
| 161 | + "visualize_classification_confusion_matrix(om_df, col_dict, title)" |
| 162 | + ] |
| 163 | + } |
| 164 | + ], |
| 165 | + "metadata": { |
| 166 | + "kernelspec": { |
| 167 | + "display_name": "Python 3", |
| 168 | + "language": "python", |
| 169 | + "name": "python3" |
| 170 | + }, |
| 171 | + "language_info": { |
| 172 | + "codemirror_mode": { |
| 173 | + "name": "ipython", |
| 174 | + "version": 3 |
| 175 | + }, |
| 176 | + "file_extension": ".py", |
| 177 | + "mimetype": "text/x-python", |
| 178 | + "name": "python", |
| 179 | + "nbconvert_exporter": "python", |
| 180 | + "pygments_lexer": "ipython3", |
| 181 | + "version": "3.7.5" |
| 182 | + }, |
| 183 | + "orig_nbformat": 4 |
| 184 | + }, |
| 185 | + "nbformat": 4, |
| 186 | + "nbformat_minor": 2 |
| 187 | +} |
0 commit comments