{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multiclass Classification for Transactions\n", "\n", "For this notebook we will be looking to classify a public dataset of transactions into a number of categories that we have predefined. These approaches should be replicable to any multiclass classificaiton use case where we are trying to fit transactional data into predefined categories, and by the end of running through this you should have a few approaches for dealing with both labelled and unlabelled datasets.\n", "\n", "The different approaches we'll be taking in this notebook are:\n", "- **Zero-shot Classification:** First we'll do zero shot classification to put transactions in one of five named buckets using only a prompt for guidance\n", "- **Classification with Embeddings:** Following this we'll create embeddings on a labelled dataset, and then use a traditional classification model to test their effectiveness at identifying our categories\n", "- **Fine-tuned Classification:** Lastly we'll produce a fine-tuned model trained on our labelled dataset to see how this compares to the zero-shot and few-shot classification approaches" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload " ] }, { "cell_type": "code", "execution_count": 311, "metadata": {}, "outputs": [], "source": [ "import openai\n", "import pandas as pd\n", "import numpy as np\n", "import json\n", "import os\n", "\n", "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", "COMPLETIONS_MODEL = \"text-davinci-002\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load dataset\n", "\n", "We're using a public transaction dataset of transactions over £25k for the Library of Scotland. The dataset has three features that we'll be using:\n", "- Supplier: The name of the supplier\n", "- Description: A text description of the transaction\n", "- Value: The value of the transaction in GBP\n", "\n", "**Source**:\n", "\n", "https://data.nls.uk/data/organisational-data/transactions-over-25k/" ] }, { "cell_type": "code", "execution_count": 312, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "359" ] }, "execution_count": 312, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transactions = pd.read_csv('./data/25000_spend_dataset_current.csv', encoding= 'unicode_escape')\n", "len(transactions)" ] }, { "cell_type": "code", "execution_count": 313, "metadata": {}, "outputs": [ { "data": { "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", "
DateSupplierDescriptionTransaction value (£)
021/04/2016M & J Ballantyne LtdGeorge IV Bridge Work35098.0
126/04/2016Private SaleLiterary & Archival Items30000.0
230/04/2016City Of Edinburgh CouncilNon Domestic Rates40800.0
309/05/2016Computacenter UkKelvin Hall72835.0
409/05/2016John Graham Construction LtdCausewayside Refurbishment64361.0
\n", "
" ], "text/plain": [ " Date Supplier Description \\\n", "0 21/04/2016 M & J Ballantyne Ltd George IV Bridge Work \n", "1 26/04/2016 Private Sale Literary & Archival Items \n", "2 30/04/2016 City Of Edinburgh Council Non Domestic Rates \n", "3 09/05/2016 Computacenter Uk Kelvin Hall \n", "4 09/05/2016 John Graham Construction Ltd Causewayside Refurbishment \n", "\n", " Transaction value (£) \n", "0 35098.0 \n", "1 30000.0 \n", "2 40800.0 \n", "3 72835.0 \n", "4 64361.0 " ] }, "execution_count": 313, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transactions.head()" ] }, { "cell_type": "code", "execution_count": 335, "metadata": {}, "outputs": [], "source": [ "def request_completion(prompt):\n", " \n", " completion_response = openai.Completion.create(\n", " prompt=prompt,\n", " temperature=0,\n", " max_tokens=5,\n", " top_p=1,\n", " frequency_penalty=0,\n", " presence_penalty=0,\n", " model=COMPLETIONS_MODEL\n", " )\n", " \n", " return completion_response\n", "\n", "def classify_transaction(transaction,prompt):\n", " \n", " prompt = prompt.replace('SUPPLIER_NAME',transaction['Supplier'])\n", " prompt = prompt.replace('DESCRIPTION_TEXT',transaction['Description'])\n", " prompt = prompt.replace('TRANSACTION_VALUE',str(transaction['Transaction value (£)']))\n", " \n", " classification = request_completion(prompt)['choices'][0]['text'].replace('\\n','')\n", " \n", " return classification\n", "\n", "# This function takes your training and validation outputs from the prepare_data function of the Finetuning API, and\n", "# confirms that each have the same number of classes.\n", "# If they do not have the same number of classes the fine-tune will fail and return an error\n", "\n", "def check_finetune_classes(train_file,valid_file):\n", "\n", " train_classes = set()\n", " valid_classes = set()\n", " with open(train_file, 'r') as json_file:\n", " json_list = list(json_file)\n", " print(len(json_list))\n", "\n", " for json_str in json_list:\n", " result = json.loads(json_str)\n", " train_classes.add(result['completion'])\n", " #print(f\"result: {result['completion']}\")\n", " #print(isinstance(result, dict))\n", "\n", " with open(valid_file, 'r') as json_file:\n", " json_list = list(json_file)\n", " print(len(json_list))\n", "\n", " for json_str in json_list:\n", " result = json.loads(json_str)\n", " valid_classes.add(result['completion'])\n", " #print(f\"result: {result['completion']}\")\n", " #print(isinstance(result, dict))\n", " \n", " if len(train_classes) == len(valid_classes):\n", " print('All good')\n", " \n", " else:\n", " print('Classes do not match, please prepare data again')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Zero-shot Classification\n", "\n", "We'll first assess the performance of the base models at classifying these transactions using a simple prompt. We'll provide the model with 5 categories and a catch-all of \"Could not classify\" for ones that it cannot place." ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [], "source": [ "zero_shot_prompt = '''You are a data expert working for the National Library of Scotland. \n", "You are analysing all transactions over £25,000 in value and classifying them into one of five categories.\n", "The five categories are Building Improvement, Literature & Archive, Utility Bills, Professional Services and Software/IT.\n", "If you can't tell what it is, say Could not classify\n", " \n", "Transaction:\n", " \n", "Supplier: SUPPLIER_NAME\n", "Description: DESCRIPTION_TEXT\n", "Value: TRANSACTION_VALUE\n", " \n", "The classification is:'''" ] }, { "cell_type": "code", "execution_count": 315, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Building Improvement\n" ] } ], "source": [ "# Get a test transaction\n", "transaction = transactions.iloc[0]\n", "\n", "# Interpolate the values into the prompt\n", "prompt = zero_shot_prompt.replace('SUPPLIER_NAME',transaction['Supplier'])\n", "prompt = prompt.replace('DESCRIPTION_TEXT',transaction['Description'])\n", "prompt = prompt.replace('TRANSACTION_VALUE',str(transaction['Transaction value (£)']))\n", "\n", "# Use our completion function to return a prediction\n", "completion_response = request_completion(prompt)\n", "print(completion_response['choices'][0]['text'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our first attempt is correct, M & J Ballantyne Ltd are a house builder and the work they performed is indeed Building Improvement.\n", "\n", "Lets expand the sample size to 25 and see how it performs, again with just a simple prompt to guide it" ] }, { "cell_type": "code", "execution_count": 291, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " \n" ] } ], "source": [ "test_transactions = transactions.iloc[:25]\n", "test_transactions['Classification'] = test_transactions.apply(lambda x: classify_transaction(x,zero_shot_prompt),axis=1)" ] }, { "cell_type": "code", "execution_count": 292, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " Building Improvement 14\n", " Could not classify 5\n", " Literature & Archive 3\n", " Software/IT 2\n", " Utility Bills 1\n", "Name: Classification, dtype: int64" ] }, "execution_count": 292, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_transactions['Classification'].value_counts()" ] }, { "cell_type": "code", "execution_count": 293, "metadata": {}, "outputs": [ { "data": { "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", " \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", " \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", "
DateSupplierDescriptionTransaction value (£)Classification
021/04/2016M & J Ballantyne LtdGeorge IV Bridge Work35098.0Building Improvement
126/04/2016Private SaleLiterary & Archival Items30000.0Literature & Archive
230/04/2016City Of Edinburgh CouncilNon Domestic Rates40800.0Utility Bills
309/05/2016Computacenter UkKelvin Hall72835.0Software/IT
409/05/2016John Graham Construction LtdCausewayside Refurbishment64361.0Building Improvement
509/05/2016A McGillivrayCausewayside Refurbishment53690.0Building Improvement
616/05/2016John Graham Construction LtdCausewayside Refurbishment365344.0Building Improvement
723/05/2016Computacenter UkKelvin Hall26506.0Software/IT
823/05/2016ECG Facilities ServiceFacilities Management Charge32777.0Building Improvement
923/05/2016ECG Facilities ServiceFacilities Management Charge32777.0Building Improvement
1030/05/2016ALDLALDL Charges32317.0Could not classify
1110/06/2016Wavetek LtdKelvin Hall87589.0Could not classify
1210/06/2016John Graham Construction LtdCausewayside Refurbishment381803.0Building Improvement
1328/06/2016ECG Facilities ServiceFacilities Management Charge32832.0Building Improvement
1430/06/2016Glasgow City CouncilKelvin Hall1700000.0Building Improvement
1511/07/2016Wavetek LtdKelvin Hall65692.0Could not classify
1611/07/2016John Graham Construction LtdCausewayside Refurbishment139845.0Building Improvement
1715/07/2016Sotheby'SLiterary & Archival Items28500.0Literature & Archive
1818/07/2016ChristiesLiterary & Archival Items33800.0Literature & Archive
1925/07/2016A McGillivrayCausewayside Refurbishment30113.0Building Improvement
2031/07/2016ALDLALDL Charges32317.0Could not classify
2108/08/2016ECG Facilities ServiceFacilities Management Charge32795.0Building Improvement
2215/08/2016Creative Video Productions LtdKelvin Hall26866.0Could not classify
2315/08/2016John Graham Construction LtdCausewayside Refurbishment196807.0Building Improvement
2424/08/2016ECG Facilities ServiceFacilities Management Charge32795.0Building Improvement
\n", "
" ], "text/plain": [ " Date Supplier Description \\\n", "0 21/04/2016 M & J Ballantyne Ltd George IV Bridge Work \n", "1 26/04/2016 Private Sale Literary & Archival Items \n", "2 30/04/2016 City Of Edinburgh Council Non Domestic Rates \n", "3 09/05/2016 Computacenter Uk Kelvin Hall \n", "4 09/05/2016 John Graham Construction Ltd Causewayside Refurbishment \n", "5 09/05/2016 A McGillivray Causewayside Refurbishment \n", "6 16/05/2016 John Graham Construction Ltd Causewayside Refurbishment \n", "7 23/05/2016 Computacenter Uk Kelvin Hall \n", "8 23/05/2016 ECG Facilities Service Facilities Management Charge \n", "9 23/05/2016 ECG Facilities Service Facilities Management Charge \n", "10 30/05/2016 ALDL ALDL Charges \n", "11 10/06/2016 Wavetek Ltd Kelvin Hall \n", "12 10/06/2016 John Graham Construction Ltd Causewayside Refurbishment \n", "13 28/06/2016 ECG Facilities Service Facilities Management Charge \n", "14 30/06/2016 Glasgow City Council Kelvin Hall \n", "15 11/07/2016 Wavetek Ltd Kelvin Hall \n", "16 11/07/2016 John Graham Construction Ltd Causewayside Refurbishment \n", "17 15/07/2016 Sotheby'S Literary & Archival Items \n", "18 18/07/2016 Christies Literary & Archival Items \n", "19 25/07/2016 A McGillivray Causewayside Refurbishment \n", "20 31/07/2016 ALDL ALDL Charges \n", "21 08/08/2016 ECG Facilities Service Facilities Management Charge \n", "22 15/08/2016 Creative Video Productions Ltd Kelvin Hall \n", "23 15/08/2016 John Graham Construction Ltd Causewayside Refurbishment \n", "24 24/08/2016 ECG Facilities Service Facilities Management Charge \n", "\n", " Transaction value (£) Classification \n", "0 35098.0 Building Improvement \n", "1 30000.0 Literature & Archive \n", "2 40800.0 Utility Bills \n", "3 72835.0 Software/IT \n", "4 64361.0 Building Improvement \n", "5 53690.0 Building Improvement \n", "6 365344.0 Building Improvement \n", "7 26506.0 Software/IT \n", "8 32777.0 Building Improvement \n", "9 32777.0 Building Improvement \n", "10 32317.0 Could not classify \n", "11 87589.0 Could not classify \n", "12 381803.0 Building Improvement \n", "13 32832.0 Building Improvement \n", "14 1700000.0 Building Improvement \n", "15 65692.0 Could not classify \n", "16 139845.0 Building Improvement \n", "17 28500.0 Literature & Archive \n", "18 33800.0 Literature & Archive \n", "19 30113.0 Building Improvement \n", "20 32317.0 Could not classify \n", "21 32795.0 Building Improvement \n", "22 26866.0 Could not classify \n", "23 196807.0 Building Improvement \n", "24 32795.0 Building Improvement " ] }, "execution_count": 293, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_transactions.head(25)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initial results are pretty good even with no labelled examples! The ones that it could not classify were tougher cases with few clues as to their topic, but maybe if we clean up the labelled dataset to give more examples we can get better performance." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Classification with Embeddings\n", "\n", "Lets create embeddings from the small set that we've classified so far - we've made a set of labelled examples by running the zero-shot classifier on 101 transactions from our dataset and manually correcting the 15 **Could not classify** results that we got\n", "\n", "### Create embeddings\n", "\n", "This initial section reuses the approach from the [Obtain_dataset Notebook](Obtain_dataset.ipynb) to create embeddings from a combined field concatenating all of our features" ] }, { "cell_type": "code", "execution_count": 317, "metadata": {}, "outputs": [ { "data": { "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", "
DateSupplierDescriptionTransaction value (£)Classification
015/08/2016Creative Video Productions LtdKelvin Hall26866Other
129/05/2017John Graham Construction LtdCausewayside Refurbishment74806Building Improvement
229/05/2017Morris & Spottiswood LtdGeorge IV Bridge Work56448Building Improvement
331/05/2017John Graham Construction LtdCausewayside Refurbishment164691Building Improvement
424/07/2017John Graham Construction LtdCausewayside Refurbishment27926Building Improvement
\n", "
" ], "text/plain": [ " Date Supplier Description \\\n", "0 15/08/2016 Creative Video Productions Ltd Kelvin Hall \n", "1 29/05/2017 John Graham Construction Ltd Causewayside Refurbishment \n", "2 29/05/2017 Morris & Spottiswood Ltd George IV Bridge Work \n", "3 31/05/2017 John Graham Construction Ltd Causewayside Refurbishment \n", "4 24/07/2017 John Graham Construction Ltd Causewayside Refurbishment \n", "\n", " Transaction value (£) Classification \n", "0 26866 Other \n", "1 74806 Building Improvement \n", "2 56448 Building Improvement \n", "3 164691 Building Improvement \n", "4 27926 Building Improvement " ] }, "execution_count": 317, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('./data/labelled_transactions.csv')\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 318, "metadata": {}, "outputs": [ { "data": { "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", "
DateSupplierDescriptionTransaction value (£)Classificationcombined
015/08/2016Creative Video Productions LtdKelvin Hall26866OtherSupplier: Creative Video Productions Ltd; Desc...
129/05/2017John Graham Construction LtdCausewayside Refurbishment74806Building ImprovementSupplier: John Graham Construction Ltd; Descri...
\n", "
" ], "text/plain": [ " Date Supplier Description \\\n", "0 15/08/2016 Creative Video Productions Ltd Kelvin Hall \n", "1 29/05/2017 John Graham Construction Ltd Causewayside Refurbishment \n", "\n", " Transaction value (£) Classification \\\n", "0 26866 Other \n", "1 74806 Building Improvement \n", "\n", " combined \n", "0 Supplier: Creative Video Productions Ltd; Desc... \n", "1 Supplier: John Graham Construction Ltd; Descri... " ] }, "execution_count": 318, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['combined'] = \"Supplier: \" + df['Supplier'].str.strip() + \"; Description: \" + df['Description'].str.strip() + \"; Value: \" + str(df['Transaction value (£)']).strip()\n", "df.head(2)" ] }, { "cell_type": "code", "execution_count": 319, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "101" ] }, "execution_count": 319, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from transformers import GPT2TokenizerFast\n", "tokenizer = GPT2TokenizerFast.from_pretrained(\"gpt2\")\n", "\n", "df['n_tokens'] = df.combined.apply(lambda x: len(tokenizer.encode(x)))\n", "len(df)" ] }, { "cell_type": "code", "execution_count": 320, "metadata": {}, "outputs": [], "source": [ "embedding_path = './data/transactions_with_embeddings_100.csv'" ] }, { "cell_type": "code", "execution_count": 321, "metadata": {}, "outputs": [], "source": [ "from openai.embeddings_utils import get_embedding\n", "\n", "df['babbage_similarity'] = df.combined.apply(lambda x: get_embedding(x, engine='text-similarity-babbage-001'))\n", "df['babbage_search'] = df.combined.apply(lambda x: get_embedding(x, engine='text-search-babbage-doc-001'))\n", "df.to_csv(embedding_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use embeddings for classification\n", "\n", "Now that we have our embeddings, let see if classifying these into the categories we've named gives us any more success.\n", "\n", "For this we'll use a template from the [Classification_using_embeddings](Classification_using_embeddings.ipynb) notebook" ] }, { "cell_type": "code", "execution_count": 309, "metadata": {}, "outputs": [ { "data": { "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", " \n", " \n", " \n", " \n", "
Unnamed: 0DateSupplierDescriptionTransaction value (£)Classificationcombinedn_tokensbabbage_similaritybabbage_search
0015/08/2016Creative Video Productions LtdKelvin Hall26866OtherSupplier: Creative Video Productions Ltd; Desc...136[-0.009802100248634815, 0.022551486268639565, ...[-0.00232666521333158, 0.019198870286345482, 0...
1129/05/2017John Graham Construction LtdCausewayside Refurbishment74806Building ImprovementSupplier: John Graham Construction Ltd; Descri...140[-0.009065819904208183, 0.012094118632376194, ...[0.005169447045773268, 0.00473341578617692, -0...
2229/05/2017Morris & Spottiswood LtdGeorge IV Bridge Work56448Building ImprovementSupplier: Morris & Spottiswood Ltd; Descriptio...141[-0.009000026620924473, 0.02405017428100109, -...[0.0028343256562948227, 0.021166473627090454, ...
3331/05/2017John Graham Construction LtdCausewayside Refurbishment164691Building ImprovementSupplier: John Graham Construction Ltd; Descri...140[-0.009065819904208183, 0.012094118632376194, ...[0.005169447045773268, 0.00473341578617692, -0...
4424/07/2017John Graham Construction LtdCausewayside Refurbishment27926Building ImprovementSupplier: John Graham Construction Ltd; Descri...140[-0.009065819904208183, 0.012094118632376194, ...[0.005169447045773268, 0.00473341578617692, -0...
\n", "
" ], "text/plain": [ " Unnamed: 0 Date Supplier \\\n", "0 0 15/08/2016 Creative Video Productions Ltd \n", "1 1 29/05/2017 John Graham Construction Ltd \n", "2 2 29/05/2017 Morris & Spottiswood Ltd \n", "3 3 31/05/2017 John Graham Construction Ltd \n", "4 4 24/07/2017 John Graham Construction Ltd \n", "\n", " Description Transaction value (£) Classification \\\n", "0 Kelvin Hall 26866 Other \n", "1 Causewayside Refurbishment 74806 Building Improvement \n", "2 George IV Bridge Work 56448 Building Improvement \n", "3 Causewayside Refurbishment 164691 Building Improvement \n", "4 Causewayside Refurbishment 27926 Building Improvement \n", "\n", " combined n_tokens \\\n", "0 Supplier: Creative Video Productions Ltd; Desc... 136 \n", "1 Supplier: John Graham Construction Ltd; Descri... 140 \n", "2 Supplier: Morris & Spottiswood Ltd; Descriptio... 141 \n", "3 Supplier: John Graham Construction Ltd; Descri... 140 \n", "4 Supplier: John Graham Construction Ltd; Descri... 140 \n", "\n", " babbage_similarity \\\n", "0 [-0.009802100248634815, 0.022551486268639565, ... \n", "1 [-0.009065819904208183, 0.012094118632376194, ... \n", "2 [-0.009000026620924473, 0.02405017428100109, -... \n", "3 [-0.009065819904208183, 0.012094118632376194, ... \n", "4 [-0.009065819904208183, 0.012094118632376194, ... \n", "\n", " babbage_search \n", "0 [-0.00232666521333158, 0.019198870286345482, 0... \n", "1 [0.005169447045773268, 0.00473341578617692, -0... \n", "2 [0.0028343256562948227, 0.021166473627090454, ... \n", "3 [0.005169447045773268, 0.00473341578617692, -0... \n", "4 [0.005169447045773268, 0.00473341578617692, -0... " ] }, "execution_count": 309, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.ensemble import RandomForestClassifier\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import classification_report, accuracy_score\n", "\n", "fs_df = pd.read_csv(embedding_path)\n", "fs_df[\"babbage_similarity\"] = fs_df.babbage_similarity.apply(eval).apply(np.array)\n", "fs_df.head()" ] }, { "cell_type": "code", "execution_count": 310, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " precision recall f1-score support\n", "\n", "Building Improvement 0.92 1.00 0.96 11\n", "Literature & Archive 1.00 1.00 1.00 3\n", " Other 0.00 0.00 0.00 1\n", " Software/IT 1.00 1.00 1.00 1\n", " Utility Bills 1.00 1.00 1.00 5\n", "\n", " accuracy 0.95 21\n", " macro avg 0.78 0.80 0.79 21\n", " weighted avg 0.91 0.95 0.93 21\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/metrics/_classification.py:1318: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n", " _warn_prf(average, modifier, msg_start, len(result))\n", "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/metrics/_classification.py:1318: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n", " _warn_prf(average, modifier, msg_start, len(result))\n", "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/metrics/_classification.py:1318: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n", " _warn_prf(average, modifier, msg_start, len(result))\n" ] } ], "source": [ "X_train, X_test, y_train, y_test = train_test_split(\n", " list(fs_df.babbage_similarity.values), fs_df.Classification, test_size=0.2, random_state=42\n", ")\n", "\n", "clf = RandomForestClassifier(n_estimators=100)\n", "clf.fit(X_train, y_train)\n", "preds = clf.predict(X_test)\n", "probas = clf.predict_proba(X_test)\n", "\n", "report = classification_report(y_test, preds)\n", "print(report)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Performance for this model is pretty strong, so creating embeddings and using even a simpler classifier looks like an effective approach as well, with the zero-shot classifier helping us do the initial classification of the unlabelled dataset.\n", "\n", "Lets take it one step further and see if a fine-tuned model trained on this same labelled datasets gives us comparable results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fine-tuned Transaction Classification\n", "\n", "For this use case we're going to try to improve on the few-shot classification from above by training a fine-tuned model on the same labelled set of 101 transactions and applying this fine-tuned model on group of unseen transactions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Building Fine-tuned Classifier\n", "\n", "We'll need to do some data prep first to get our data ready. This will take the following steps:\n", "- First we'll list out our classes and replace them with numeric identifiers. Making the model predict a single token rather than multiple consecutive ones like 'Building Improvement' should give us better results\n", "- We also need to add a common prefix and suffix to each example to aid the model in making predictions - in our case our text is already started with 'Supplier' and we'll add a suffix of '\\n\\n###\\n\\n'\n", "- Lastly we'll aid a leading whitespace onto each of our target classes for classification, again to aid the model" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "101" ] }, "execution_count": 210, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ft_prep_df = fs_df.copy()\n", "len(ft_prep_df)" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "data": { "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", " \n", " \n", " \n", " \n", "
Unnamed: 0DateSupplierDescriptionTransaction value (£)Classificationcombinedn_tokensbabbage_similaritybabbage_search
0015/08/2016Creative Video Productions LtdKelvin Hall26866OtherSupplier: Creative Video Productions Ltd; Desc...12[-0.009630300104618073, 0.009887108579277992, ...[-0.008217384107410908, 0.025170527398586273, ...
1129/05/2017John Graham Construction LtdCausewayside Refurbishment74806Building ImprovementSupplier: John Graham Construction Ltd; Descri...16[-0.006144719664007425, -0.0018709596479311585...[-0.007424891460686922, 0.008475713431835175, ...
2229/05/2017Morris & Spottiswood LtdGeorge IV Bridge Work56448Building ImprovementSupplier: Morris & Spottiswood Ltd; Descriptio...17[-0.005225738976150751, 0.015156379900872707, ...[-0.007611643522977829, 0.030322374776005745, ...
3331/05/2017John Graham Construction LtdCausewayside Refurbishment164691Building ImprovementSupplier: John Graham Construction Ltd; Descri...16[-0.006144719664007425, -0.0018709596479311585...[-0.007424891460686922, 0.008475713431835175, ...
4424/07/2017John Graham Construction LtdCausewayside Refurbishment27926Building ImprovementSupplier: John Graham Construction Ltd; Descri...16[-0.006144719664007425, -0.0018709596479311585...[-0.007424891460686922, 0.008475713431835175, ...
\n", "
" ], "text/plain": [ " Unnamed: 0 Date Supplier \\\n", "0 0 15/08/2016 Creative Video Productions Ltd \n", "1 1 29/05/2017 John Graham Construction Ltd \n", "2 2 29/05/2017 Morris & Spottiswood Ltd \n", "3 3 31/05/2017 John Graham Construction Ltd \n", "4 4 24/07/2017 John Graham Construction Ltd \n", "\n", " Description Transaction value (£) Classification \\\n", "0 Kelvin Hall 26866 Other \n", "1 Causewayside Refurbishment 74806 Building Improvement \n", "2 George IV Bridge Work 56448 Building Improvement \n", "3 Causewayside Refurbishment 164691 Building Improvement \n", "4 Causewayside Refurbishment 27926 Building Improvement \n", "\n", " combined n_tokens \\\n", "0 Supplier: Creative Video Productions Ltd; Desc... 12 \n", "1 Supplier: John Graham Construction Ltd; Descri... 16 \n", "2 Supplier: Morris & Spottiswood Ltd; Descriptio... 17 \n", "3 Supplier: John Graham Construction Ltd; Descri... 16 \n", "4 Supplier: John Graham Construction Ltd; Descri... 16 \n", "\n", " babbage_similarity \\\n", "0 [-0.009630300104618073, 0.009887108579277992, ... \n", "1 [-0.006144719664007425, -0.0018709596479311585... \n", "2 [-0.005225738976150751, 0.015156379900872707, ... \n", "3 [-0.006144719664007425, -0.0018709596479311585... \n", "4 [-0.006144719664007425, -0.0018709596479311585... \n", "\n", " babbage_search \n", "0 [-0.008217384107410908, 0.025170527398586273, ... \n", "1 [-0.007424891460686922, 0.008475713431835175, ... \n", "2 [-0.007611643522977829, 0.030322374776005745, ... \n", "3 [-0.007424891460686922, 0.008475713431835175, ... \n", "4 [-0.007424891460686922, 0.008475713431835175, ... " ] }, "execution_count": 211, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ft_prep_df.head()" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "( class_id class\n", " 0 0 Literature & Archive\n", " 1 1 Utility Bills\n", " 2 2 Building Improvement\n", " 3 3 Software/IT\n", " 4 4 Other,\n", " 5)" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classes = list(set(ft_prep_df['Classification']))\n", "class_df = pd.DataFrame(classes).reset_index()\n", "class_df.columns = ['class_id','class']\n", "class_df , len(class_df)" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "data": { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0DateSupplierDescriptionTransaction value (£)Classificationcombinedn_tokensbabbage_similaritybabbage_searchclass_idprompt
0015/08/2016Creative Video Productions LtdKelvin Hall26866OtherSupplier: Creative Video Productions Ltd; Desc...12[-0.009630300104618073, 0.009887108579277992, ...[-0.008217384107410908, 0.025170527398586273, ...4Supplier: Creative Video Productions Ltd; Desc...
15131/03/2017NLS FoundationGrant Payment177500OtherSupplier: NLS Foundation; Description: Grant P...11[-0.022305507212877274, 0.008543581701815128, ...[-0.020519884303212166, 0.01993306167423725, -...4Supplier: NLS Foundation; Description: Grant P...
27026/06/2017British LibraryLegal Deposit Services50056OtherSupplier: British Library; Description: Legal ...11[-0.01019938476383686, 0.015277703292667866, -...[-0.01843327097594738, 0.03343546763062477, -0...4Supplier: British Library; Description: Legal ...
37124/07/2017ALDLLegal Deposit Services27067OtherSupplier: ALDL; Description: Legal Deposit Ser...11[-0.008471488021314144, 0.004098685923963785, ...[-0.012966590002179146, 0.01299362163990736, 0...4Supplier: ALDL; Description: Legal Deposit Ser...
410024/07/2017AM PhillipVehicle Purchase26604OtherSupplier: AM Phillip; Description: Vehicle Pur...10[-0.003459023078903556, 0.004626389592885971, ...[-0.0010945454705506563, 0.008626140654087067,...4Supplier: AM Phillip; Description: Vehicle Pur...
\n", "
" ], "text/plain": [ " Unnamed: 0 Date Supplier \\\n", "0 0 15/08/2016 Creative Video Productions Ltd \n", "1 51 31/03/2017 NLS Foundation \n", "2 70 26/06/2017 British Library \n", "3 71 24/07/2017 ALDL \n", "4 100 24/07/2017 AM Phillip \n", "\n", " Description Transaction value (£) Classification \\\n", "0 Kelvin Hall 26866 Other \n", "1 Grant Payment 177500 Other \n", "2 Legal Deposit Services 50056 Other \n", "3 Legal Deposit Services 27067 Other \n", "4 Vehicle Purchase 26604 Other \n", "\n", " combined n_tokens \\\n", "0 Supplier: Creative Video Productions Ltd; Desc... 12 \n", "1 Supplier: NLS Foundation; Description: Grant P... 11 \n", "2 Supplier: British Library; Description: Legal ... 11 \n", "3 Supplier: ALDL; Description: Legal Deposit Ser... 11 \n", "4 Supplier: AM Phillip; Description: Vehicle Pur... 10 \n", "\n", " babbage_similarity \\\n", "0 [-0.009630300104618073, 0.009887108579277992, ... \n", "1 [-0.022305507212877274, 0.008543581701815128, ... \n", "2 [-0.01019938476383686, 0.015277703292667866, -... \n", "3 [-0.008471488021314144, 0.004098685923963785, ... \n", "4 [-0.003459023078903556, 0.004626389592885971, ... \n", "\n", " babbage_search class_id \\\n", "0 [-0.008217384107410908, 0.025170527398586273, ... 4 \n", "1 [-0.020519884303212166, 0.01993306167423725, -... 4 \n", "2 [-0.01843327097594738, 0.03343546763062477, -0... 4 \n", "3 [-0.012966590002179146, 0.01299362163990736, 0... 4 \n", "4 [-0.0010945454705506563, 0.008626140654087067,... 4 \n", "\n", " prompt \n", "0 Supplier: Creative Video Productions Ltd; Desc... \n", "1 Supplier: NLS Foundation; Description: Grant P... \n", "2 Supplier: British Library; Description: Legal ... \n", "3 Supplier: ALDL; Description: Legal Deposit Ser... \n", "4 Supplier: AM Phillip; Description: Vehicle Pur... " ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ft_df_with_class = ft_prep_df.merge(class_df,left_on='Classification',right_on='class',how='inner')\n", "\n", "# Adding a leading whitespace onto each completion to help the model\n", "ft_df_with_class['class_id'] = ft_df_with_class.apply(lambda x: ' ' + str(x['class_id']),axis=1)\n", "ft_df_with_class = ft_df_with_class.drop('class', axis=1)\n", "\n", "# Adding a common separator onto the end of each prompt so the model knows when a prompt is terminating\n", "ft_df_with_class['prompt'] = ft_df_with_class.apply(lambda x: x['combined'] + '\\n\\n###\\n\\n',axis=1)\n", "ft_df_with_class.head()" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "data": { "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", "
promptcompletion
ordering
0Supplier: Sothebys; Description: Literary & Ar...0
1Supplier: Sotheby'S; Description: Literary & A...0
2Supplier: City Of Edinburgh Council; Descripti...1
2Supplier: John Graham Construction Ltd; Descri...2
3Supplier: John Graham Construction Ltd; Descri...2
\n", "
" ], "text/plain": [ " prompt completion\n", "ordering \n", "0 Supplier: Sothebys; Description: Literary & Ar... 0\n", "1 Supplier: Sotheby'S; Description: Literary & A... 0\n", "2 Supplier: City Of Edinburgh Council; Descripti... 1\n", "2 Supplier: John Graham Construction Ltd; Descri... 2\n", "3 Supplier: John Graham Construction Ltd; Descri... 2" ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This step is unnecessary if you have a number of observations in each class\n", "# In our case we don't, so we shuffle the data to give us a better chance of getting equal classes in our train and validation sets\n", "# Our fine-tuned model will error if we have less classes in the validation set, so this is a necessary step\n", "\n", "import random \n", "\n", "labels = [x for x in ft_df_with_class['class_id']]\n", "text = [x for x in ft_df_with_class['prompt']]\n", "ft_df = pd.DataFrame(zip(text, labels), columns = ['prompt','class_id']) #[:300]\n", "ft_df.columns = ['prompt','completion']\n", "ft_df['ordering'] = ft_df.apply(lambda x: random.randint(0,len(ft_df)), axis = 1)\n", "ft_df.set_index('ordering',inplace=True)\n", "ft_df_sorted = ft_df.sort_index(ascending=True)\n", "ft_df_sorted.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This step is to remove any existing files if we've already produced training/validation sets for this classifier\n", "#!rm transactions_grouped*\n", "\n", "# We output our shuffled dataframe to a .jsonl file and run the prepare_data function to get us our input files\n", "ft_df_sorted.to_json(\"transactions_grouped.jsonl\", orient='records', lines=True)\n", "!openai tools fine_tunes.prepare_data -f transactions_grouped.jsonl -q" ] }, { "cell_type": "code", "execution_count": 322, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "31\n", "8\n", "All good\n" ] } ], "source": [ "# This functions checks that your classes all appear in both prepared files\n", "# If they don't, the fine-tuned model creation will fail\n", "check_classes('transactions_grouped_prepared_train.jsonl','transactions_grouped_prepared_valid.jsonl')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This step creates your model\n", "!openai api fine_tunes.create -t \"transactions_grouped_prepared_train.jsonl\" -v \"transactions_grouped_prepared_valid.jsonl\" --compute_classification_metrics --classification_n_classes 5 -m curie" ] }, { "cell_type": "code", "execution_count": 323, "metadata": {}, "outputs": [], "source": [ "# Congrats, you've got a fine-tuned model!\n", "# Copy/paste the name provided into the variable below and we'll take it for a spin\n", "fine_tuned_model = 'curie:ft-personal-2022-10-20-10-42-56'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Applying Fine-tuned Classifier\n", "\n", "Now we'll apply our classifier to see how it performs. We only had 31 unique observations in our training set and 8 in our validation set, so lets see how the performance is" ] }, { "cell_type": "code", "execution_count": 324, "metadata": {}, "outputs": [ { "data": { "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", "
promptcompletion
0Supplier: Wavetek Ltd; Description: Kelvin Hal...2
1Supplier: ECG Facilities Service; Description:...1
2Supplier: M & J Ballantyne Ltd; Description: G...2
3Supplier: Private Sale; Description: Literary ...0
4Supplier: Ex Libris; Description: IT equipment...3
\n", "
" ], "text/plain": [ " prompt completion\n", "0 Supplier: Wavetek Ltd; Description: Kelvin Hal... 2\n", "1 Supplier: ECG Facilities Service; Description:... 1\n", "2 Supplier: M & J Ballantyne Ltd; Description: G... 2\n", "3 Supplier: Private Sale; Description: Literary ... 0\n", "4 Supplier: Ex Libris; Description: IT equipment... 3" ] }, "execution_count": 324, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_set = pd.read_json('transactions_grouped_prepared_valid.jsonl', lines=True)\n", "test_set.head()" ] }, { "cell_type": "code", "execution_count": 325, "metadata": {}, "outputs": [], "source": [ "test_set['predicted_class'] = test_set.apply(lambda x: openai.Completion.create(model=fine_tuned_model, prompt=x['prompt'], max_tokens=1, temperature=0, logprobs=5),axis=1)\n", "test_set['pred'] = test_set.apply(lambda x : x['predicted_class']['choices'][0]['text'],axis=1)" ] }, { "cell_type": "code", "execution_count": 326, "metadata": {}, "outputs": [], "source": [ "test_set['result'] = test_set.apply(lambda x: str(x['pred']).strip() == str(x['completion']).strip(), axis = 1)" ] }, { "cell_type": "code", "execution_count": 327, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True 4\n", "False 4\n", "Name: result, dtype: int64" ] }, "execution_count": 327, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_set['result'].value_counts()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Performance is not great - unfortunately this is expected. With only a few examples of each class, the above approach with embeddings and a traditional classifier worked better.\n", "\n", "A fine-tuned model works best with a great number of labelled observations. If we had a few hundred or thousand we may get better results, but lets do one last test on a holdout set to confirm that it doesn't generalise well to a new set of observations" ] }, { "cell_type": "code", "execution_count": 330, "metadata": {}, "outputs": [ { "data": { "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", "
DateSupplierDescriptionTransaction value (£)
10123/10/2017City Building LLPCausewayside Refurbishment53147.0
10230/10/2017ECG Facilities ServiceFacilities Management Charge35758.0
10330/10/2017ECG Facilities ServiceFacilities Management Charge35758.0
10406/11/2017John Graham Construction LtdCausewayside Refurbishment134208.0
10506/11/2017ALDLLegal Deposit Services27067.0
\n", "
" ], "text/plain": [ " Date Supplier Description \\\n", "101 23/10/2017 City Building LLP Causewayside Refurbishment \n", "102 30/10/2017 ECG Facilities Service Facilities Management Charge \n", "103 30/10/2017 ECG Facilities Service Facilities Management Charge \n", "104 06/11/2017 John Graham Construction Ltd Causewayside Refurbishment \n", "105 06/11/2017 ALDL Legal Deposit Services \n", "\n", " Transaction value (£) \n", "101 53147.0 \n", "102 35758.0 \n", "103 35758.0 \n", "104 134208.0 \n", "105 27067.0 " ] }, "execution_count": 330, "metadata": {}, "output_type": "execute_result" } ], "source": [ "holdout_df = transactions.copy().iloc[101:]\n", "holdout_df.head()" ] }, { "cell_type": "code", "execution_count": 332, "metadata": {}, "outputs": [], "source": [ "holdout_df['combined'] = \"Supplier: \" + holdout_df['Supplier'].str.strip() + \"; Description: \" + holdout_df['Description'].str.strip() + '\\n\\n###\\n\\n' # + \"; Value: \" + str(df['Transaction value (£)']).strip()\n", "holdout_df['prediction_result'] = holdout_df.apply(lambda x: openai.Completion.create(model=fine_tuned_model, prompt=x['combined'], max_tokens=1, temperature=0, logprobs=5),axis=1)\n", "holdout_df['pred'] = holdout_df.apply(lambda x : x['prediction_result']['choices'][0]['text'],axis=1)" ] }, { "cell_type": "code", "execution_count": 333, "metadata": {}, "outputs": [ { "data": { "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", " \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", "
DateSupplierDescriptionTransaction value (£)combinedprediction_resultpred
10123/10/2017City Building LLPCausewayside Refurbishment53147.0Supplier: City Building LLP; Description: Caus...{'id': 'cmpl-63YDadbYLo8xKsGY2vReOFCMgTOvG', '...2
10230/10/2017ECG Facilities ServiceFacilities Management Charge35758.0Supplier: ECG Facilities Service; Description:...{'id': 'cmpl-63YDbNK1D7UikDc3xi5ATihg5kQEt', '...2
10330/10/2017ECG Facilities ServiceFacilities Management Charge35758.0Supplier: ECG Facilities Service; Description:...{'id': 'cmpl-63YDbwfiHjkjMWsfTKNt6naeqPzOe', '...2
10406/11/2017John Graham Construction LtdCausewayside Refurbishment134208.0Supplier: John Graham Construction Ltd; Descri...{'id': 'cmpl-63YDbWAndtsRqPTi2ZHZtPodZvOwr', '...2
10506/11/2017ALDLLegal Deposit Services27067.0Supplier: ALDL; Description: Legal Deposit Ser...{'id': 'cmpl-63YDbDu7WM3svYWsRAMdDUKtSFDBu', '...2
10627/11/2017Maggs Bros LtdLiterary & Archival Items26500.0Supplier: Maggs Bros Ltd; Description: Literar...{'id': 'cmpl-63YDbxNNI8ZH5CJJNxQ0IF9Zf925C', '...0
10730/11/2017Glasgow City CouncilKelvin Hall42345.0Supplier: Glasgow City Council; Description: K...{'id': 'cmpl-63YDb8R1FWu4bjwM2xE775rouwneV', '...2
10811/12/2017ECG Facilities ServiceFacilities Management Charge35758.0Supplier: ECG Facilities Service; Description:...{'id': 'cmpl-63YDcAPsp37WhbPs9kwfUX0kBk7Hv', '...2
10911/12/2017John Graham Construction LtdCausewayside Refurbishment159275.0Supplier: John Graham Construction Ltd; Descri...{'id': 'cmpl-63YDcML2welrC3wF0nuKgcNmVu1oQ', '...2
11008/01/2018ECG Facilities ServiceFacilities Management Charge35758.0Supplier: ECG Facilities Service; Description:...{'id': 'cmpl-63YDc95SSdOHnIliFB2cjMEEm7Z2u', '...2
\n", "
" ], "text/plain": [ " Date Supplier Description \\\n", "101 23/10/2017 City Building LLP Causewayside Refurbishment \n", "102 30/10/2017 ECG Facilities Service Facilities Management Charge \n", "103 30/10/2017 ECG Facilities Service Facilities Management Charge \n", "104 06/11/2017 John Graham Construction Ltd Causewayside Refurbishment \n", "105 06/11/2017 ALDL Legal Deposit Services \n", "106 27/11/2017 Maggs Bros Ltd Literary & Archival Items \n", "107 30/11/2017 Glasgow City Council Kelvin Hall \n", "108 11/12/2017 ECG Facilities Service Facilities Management Charge \n", "109 11/12/2017 John Graham Construction Ltd Causewayside Refurbishment \n", "110 08/01/2018 ECG Facilities Service Facilities Management Charge \n", "\n", " Transaction value (£) combined \\\n", "101 53147.0 Supplier: City Building LLP; Description: Caus... \n", "102 35758.0 Supplier: ECG Facilities Service; Description:... \n", "103 35758.0 Supplier: ECG Facilities Service; Description:... \n", "104 134208.0 Supplier: John Graham Construction Ltd; Descri... \n", "105 27067.0 Supplier: ALDL; Description: Legal Deposit Ser... \n", "106 26500.0 Supplier: Maggs Bros Ltd; Description: Literar... \n", "107 42345.0 Supplier: Glasgow City Council; Description: K... \n", "108 35758.0 Supplier: ECG Facilities Service; Description:... \n", "109 159275.0 Supplier: John Graham Construction Ltd; Descri... \n", "110 35758.0 Supplier: ECG Facilities Service; Description:... \n", "\n", " prediction_result pred \n", "101 {'id': 'cmpl-63YDadbYLo8xKsGY2vReOFCMgTOvG', '... 2 \n", "102 {'id': 'cmpl-63YDbNK1D7UikDc3xi5ATihg5kQEt', '... 2 \n", "103 {'id': 'cmpl-63YDbwfiHjkjMWsfTKNt6naeqPzOe', '... 2 \n", "104 {'id': 'cmpl-63YDbWAndtsRqPTi2ZHZtPodZvOwr', '... 2 \n", "105 {'id': 'cmpl-63YDbDu7WM3svYWsRAMdDUKtSFDBu', '... 2 \n", "106 {'id': 'cmpl-63YDbxNNI8ZH5CJJNxQ0IF9Zf925C', '... 0 \n", "107 {'id': 'cmpl-63YDb8R1FWu4bjwM2xE775rouwneV', '... 2 \n", "108 {'id': 'cmpl-63YDcAPsp37WhbPs9kwfUX0kBk7Hv', '... 2 \n", "109 {'id': 'cmpl-63YDcML2welrC3wF0nuKgcNmVu1oQ', '... 2 \n", "110 {'id': 'cmpl-63YDc95SSdOHnIliFB2cjMEEm7Z2u', '... 2 " ] }, "execution_count": 333, "metadata": {}, "output_type": "execute_result" } ], "source": [ "holdout_df.head(10)" ] }, { "cell_type": "code", "execution_count": 334, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " 2 231\n", " 0 27\n", "Name: pred, dtype: int64" ] }, "execution_count": 334, "metadata": {}, "output_type": "execute_result" } ], "source": [ "holdout_df['pred'].value_counts()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Well those results were similarly underwhelming - so we've learned that with a dataset with a small number of labelled observations, either zero-shot classification or traditional classification with embeddings return better results than a fine-tuned model.\n", "\n", "A fine-tuned model is still a great tool, but is more effective when you have a larger number of labelled examples for each class that you're looking to classify" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }