{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Financial Sentiment Analysis\n", "\n", "[Dataset source](https://www.kaggle.com/datasets/sbhatti/financial-sentiment-analysis)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!rm -f tracing_database.json\n", "%pip install great-ai > /dev/null" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "data = pd.read_csv('data.csv')\n", "\n", "pd.set_option(\"display.max_rows\", 30)\n", "pd.set_option(\"display.max_columns\", 30)\n", "data.head(30)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from great_ai import add_ground_truth, delete_ground_truth\n", "\n", "X = data['Sentence'].to_list()\n", "y = data['Sentiment'].to_list()\n", "\n", "add_ground_truth(X, y, train_split_ratio=0.85, test_split_ratio=0.15)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from great_ai import query_ground_truth\n", "\n", "train_split = query_ground_truth('train')\n", "test_split = query_ground_truth('test')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from great_ai.utilities import clean, simple_parallel_map\n", "import re\n", "from great_ai import Trace\n", "\n", "def normalize(text: str) -> str:\n", " cleaned = clean(text, convert_to_ascii=True).lower()\n", " return re.sub(r\"[^a-z]+\", \" \", cleaned)\n", "\n", "X_train = simple_parallel_map(normalize, [t.input for t in train_split])\n", "X_test = simple_parallel_map(normalize, [t.input for t in test_split])\n", "\n", "y_train = [t.output for t in train_split]\n", "y_test = [t.output for t in test_split]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.pipeline import Pipeline, make_pipeline\n", "from sklearn.linear_model import SGDClassifier\n", "from sklearn.feature_extraction.text import TfidfVectorizer\n", "\n", "\n", "def create_pipeline() -> Pipeline:\n", " return make_pipeline(\n", " TfidfVectorizer(min_df=5, max_df=0.3, ngram_range=(1, 3), sublinear_tf=True),\n", " SGDClassifier(max_iter=10000, tol=1e-4, penalty=\"elasticnet\")\n", " )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import RandomizedSearchCV\n", "import scipy.stats\n", "\n", "optimisation_pipeline = RandomizedSearchCV(\n", " create_pipeline(),\n", " {\n", " \"sgdclassifier__alpha\": scipy.stats.uniform(0.00005, 0.01),\n", " \"sgdclassifier__l1_ratio\": scipy.stats.uniform(0.5, 0.4),\n", " },\n", " cv=4,\n", " n_iter=150,\n", " verbose=1,\n", " scoring='f1_macro',\n", " n_jobs=-1\n", ")\n", "\n", "optimisation_pipeline.fit(X_train, y_train)\n", "results = pd.DataFrame(optimisation_pipeline.cv_results_)\n", "results.sort_values(\"rank_test_score\").head(20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = create_pipeline()\n", "model.set_params(\n", " **optimisation_pipeline.best_params_,\n", " sgdclassifier__max_iter=100000,\n", " sgdclassifier__tol=1e-5,\n", ")\n", "\n", "model.fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "from sklearn import metrics\n", "\n", "%matplotlib inline\n", "plt.rcParams[\"figure.figsize\"] = (10, 10)\n", "plt.rcParams[\"font.size\"] = 16\n", "\n", "y_predicted = model.predict(X_test)\n", "\n", "print(metrics.classification_report(y_test, y_predicted))\n", "metrics.ConfusionMatrixDisplay.from_predictions(\n", " y_true=y_test,\n", " y_pred=y_predicted,\n", " xticks_rotation=\"vertical\",\n", " normalize=\"pred\",\n", " values_format=\".2f\",\n", ")\n", "None" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "features = model.named_steps[\"tfidfvectorizer\"].get_feature_names_out()\n", "\n", "for i, name in enumerate(model.named_steps[\"sgdclassifier\"].classes_):\n", " weight = model.named_steps[\"sgdclassifier\"].coef_[i]\n", "\n", " print(f'There are {len([w for w in weight if w != 0])} features for the`{name}` class.')\n", "\n", " for w, f in sorted(zip(weight, features), reverse=True)[:15]:\n", " if w == 0:\n", " break\n", " print(f\" {f}: {w:.4f}\")\n", "\n", " print()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def predict(text: str):\n", " text = normalize(text)\n", " features = model.named_steps[\"tfidfvectorizer\"].transform([text])\n", " prediction = model.named_steps[\"sgdclassifier\"].predict(features)[0]\n", "\n", " explanation = [\n", " (feature_name, weight)\n", " for weight, feature_name in sorted(\n", " (\n", " (feature_weight * feature, feature_name)\n", " for feature_name, feature_weight, feature in zip(\n", " model.named_steps[\"tfidfvectorizer\"].get_feature_names_out(),\n", " model.named_steps[\"sgdclassifier\"].coef_[list(model.named_steps[\"sgdclassifier\"].classes_).index(prediction)],\n", " features.toarray()[0],\n", " )\n", " if feature * feature_weight != 0\n", " ),\n", " reverse=True,\n", " )\n", " ][:10]\n", "\n", " return prediction, explanation\n", "\n", "predict('''\n", " The last 12 months for Tesla shares have been fairly but positively volatile. \n", " The stock is up in the past year, as it was trading at just under $700 per share back in early August 2021. \n", " The share price spent much of late 2021 and early 2022 over the $1,000 mark. \n", " Prices dipped below $1,000—and stayed there—starting in late April.\n", "\n", " I have been bearish on Tesla lately, owing to its elevated share price and its growing competition in the electric vehicle field.\n", " The competition part isn't changing much. \n", " That's really only gotten worse thanks to most of the major automakers looking to get in on the market.\n", "\n", " However, Tesla's move to make its shares more reasonably priced should catch some attention. Thus, I'm moving to neutral on Tesla stock.\n", "''')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# todo: export the model" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.4 64-bit", "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.10.4" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" } } }, "nbformat": 4, "nbformat_minor": 2 }