Trains an LSTM neural network to classify the sentiment of financial sentences as negative, neutral, or positive. Everything lives in NLP_finance.py.
- Loads
finance_train.csvandfinance_test.csv, each a set of financial sentences with a sentiment label. - Cleans the text: lowercases it, strips punctuation and symbols, and removes English stopwords using NLTK.
- Tokenizes and pads the sentences with Keras utilities so they can feed into a neural network.
- Builds an Embedding plus LSTM model in Keras, trains it with early stopping, and evaluates accuracy and loss on a held out test set.
- Plots training versus validation loss and accuracy over epochs, and a confusion matrix for the final model's predictions.
- Python 3.9+
- numpy
- pandas
- seaborn
- matplotlib
- nltk
- beautifulsoup4
- scikit-learn
- gdown
- tensorflow (includes keras)
Install with:
pip install numpy pandas seaborn matplotlib nltk beautifulsoup4 scikit-learn gdown tensorflowThe script also calls nltk.download() for punkt, stopwords, and wordnet on first run, so an internet connection is needed the first time.
finance_train.csv and finance_test.csv are not included in this repo. The comments at the top of the script point to where they were originally pulled from (an Inspirit AI project data bucket); grab them from there, or use your own train and test CSVs with Sentence and Label columns, and place them in the same folder as the script.
The final model in the script gets to about 69.7% test accuracy and starts overfitting after the second or third epoch (validation loss climbs while training loss keeps dropping), which the inline notes call out. The script also trains a model twice: once directly near the middle of the file, and again through run_model() near the end with slightly different hyperparameters. The second run is the one whose results (confusion matrix included) are treated as final.