This project is a simple implementation of a financial sentiment analysis model using Python. It classifies financial news headlines into one of three categories: positive, negative, or neutral.
This project was built as a hands-on introduction to Natural Language Processing (NLP) for quantitative finance applications.
- Text Preprocessing: Cleans text data by removing stop words, lemmatizing, and removing non-alphabetic characters.
- Feature Extraction: Uses
TfidfVectorizerfrom Scikit-learn to convert text into numerical features. - Machine Learning Model: Employs a
Multinomial Naive Bayesclassifier to predict sentiment. - Evaluation: The model's performance is evaluated using an accuracy score, classification report, and a confusion matrix.
This project uses the "Financial PhraseBank" dataset, which can be found on Kaggle. It contains sentences from financial news that have been manually labeled with a sentiment.
The model was trained on 80% of the data and tested on the remaining 20%.
- Accuracy: ~67.6%
- Key Findings:
- The model is highly effective at identifying neutral statements (94% recall).
- It performs reasonably well on positive statements (53% recall).
- The model struggles significantly with identifying negative statements (3% recall), often misclassifying them as neutral.
This indicates that while the model can identify clear positive or neutral language, it has difficulty with the nuance of negative financial news.
-
Clone the repository:
git clone https://github.com/YOUR_USERNAME/YOUR_REPOSITORY_NAME.git cd YOUR_REPOSITORY_NAME -
Install the dependencies:
pip install -r requirements.txt -
Download NLTK data: Run the following in a Python interpreter:
import nltk nltk.download('punkt') nltk.download('stopwords') nltk.download('wordnet') -
Download the dataset:
- Download
data.csvfrom the Kaggle link above. - Place the
data.csvfile in the root of the project folder.
- Download
-
Run the script:
python sentiment_analyzer.py
- Use a more advanced model: Implement models like Logistic Regression, SVM, or even deep learning models like an LSTM or a transformer (e.g., FinBERT).
- Address class imbalance: The model's poor performance on the negative class could be due to having fewer negative examples. Techniques like oversampling (e.g., SMOTE) could help.
- Improve text preprocessing: Experiment with different techniques, such as bigrams or trigrams.