{"id":15176679,"url":"https://github.com/thepredictivedev/financial-news-classifier-using-conv1d-nn","last_synced_at":"2026-02-27T07:34:50.515Z","repository":{"id":254648609,"uuid":"847134771","full_name":"ThePredictiveDev/Financial-News-Classifier-Using-Conv1D-NN","owner":"ThePredictiveDev","description":"Used a Conv1D Neural Network to Create a Financial News Classifier with 98% accuracy","archived":false,"fork":false,"pushed_at":"2024-08-25T00:56:22.000Z","size":16954,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-03T14:24:15.275Z","etag":null,"topics":["conv1d-neural-networks","data-science","deep-learning","financial-news-classifier","machine-learning","natural-language-processing","news-analysis","python","sentiment-analysis","stock-market-prediction","tensorflow","text-classification"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ThePredictiveDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-25T00:26:21.000Z","updated_at":"2024-08-25T01:40:36.000Z","dependencies_parsed_at":"2024-08-25T03:42:09.631Z","dependency_job_id":null,"html_url":"https://github.com/ThePredictiveDev/Financial-News-Classifier-Using-Conv1D-NN","commit_stats":null,"previous_names":["thepredictivedev/financial-news-classifier-using-conv1d-nn"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThePredictiveDev%2FFinancial-News-Classifier-Using-Conv1D-NN","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThePredictiveDev%2FFinancial-News-Classifier-Using-Conv1D-NN/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThePredictiveDev%2FFinancial-News-Classifier-Using-Conv1D-NN/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThePredictiveDev%2FFinancial-News-Classifier-Using-Conv1D-NN/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ThePredictiveDev","download_url":"https://codeload.github.com/ThePredictiveDev/Financial-News-Classifier-Using-Conv1D-NN/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240221330,"owners_count":19767442,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conv1d-neural-networks","data-science","deep-learning","financial-news-classifier","machine-learning","natural-language-processing","news-analysis","python","sentiment-analysis","stock-market-prediction","tensorflow","text-classification"],"created_at":"2024-09-27T13:40:18.742Z","updated_at":"2025-11-13T07:03:06.445Z","avatar_url":"https://github.com/ThePredictiveDev.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Financial News Classifier Using Conv1D with a Trainable Embedding Layer\n\nThis project demonstrates how to build a financial news sentiment classifier using a Convolutional Neural Network (CNN) with a trainable embedding layer. The model classifies financial news into three categories: positive, neutral, and negative. The model achieved 98% accuracy\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Data Preprocessing](#data-preprocessing)\n- [Model Creation, Compilation, and Training](#model-creation-compilation-and-training)\n- [Model Evaluation](#model-evaluation)\n- [Model Saving](#model-saving)\n- [Fetching Real-Time News and Sentiment Analysis](#fetching-real-time-news-and-sentiment-analysis)\n- [License](#license)\n\n## Installation\n\nTo install the necessary packages, use the following `requirements.txt`:\n\n```plaintext\ntensorflow==2.6.0\npandas==1.3.3\nnumpy==1.21.2\nscikit-learn==0.24.2\nnewsapi-python==0.2.6\n```\nInstall the required packages using pip:\n```bash\npip install -r requirements.txt\n```\n\n## Data Preprocessing\n### Load Financial Phrase Bank Data\nTo train the model, we use the Financial Phrase Bank dataset. The dataset contains labeled financial news sentences that are categorized as positive, neutral, or negative.\n```python\nimport os\nimport pandas as pd\n\ndef load_financial_phrase_bank(data_dir, encoding='utf-8'):\n    sentences = []\n    sentiments = []\n\n    for filename in os.listdir(data_dir):\n        if filename.startswith(\"Sentences_\"):\n            filepath = os.path.join(data_dir, filename)\n            try:\n                with open(filepath, 'r', encoding=encoding) as file:\n                    for line in file:\n                        line = line.strip()\n                        if line:\n                            sentence, sentiment = line.rsplit('@', 1)\n                            sentences.append(sentence.strip())\n                            sentiments.append(sentiment.strip())\n            except UnicodeDecodeError as e:\n                print(f\"Error decoding {filename} with encoding {encoding}: {e}\")\n                continue\n\n    df = pd.DataFrame({\n        'sentence': sentences,\n        'sentiment': sentiments\n    })\n\n    return df\n\n# Usage example\ndata_dir = \"Your Download Directory/FinancialPhraseBank-v1.0\"\nencodings = ['utf-8', 'latin1', 'cp1252', 'iso-8859-1']\n\nfor enc in encodings:\n    print(f\"Trying encoding: {enc}\")\n    df = load_financial_phrase_bank(data_dir, encoding=enc)\n    if not df.empty:\n        print(f\"Successfully loaded data with encoding: {enc}\")\n        break\nelse:\n    print(\"Failed to load data with tried encodings.\")\n```\n\n### Sentiment Label Mapping and Train-Test Split\nMap sentiment labels to numerical values and split the data into training and testing sets.\n```python\nfrom sklearn.model_selection import train_test_split\n\n# Mapping sentiment labels to numerical values\nlabel_mapping = {\"positive\": 1, \"neutral\": 0, \"negative\": -1}\ndf['sentiment'] = df['sentiment'].map(label_mapping)\n\n# Train-test split\nRANDOM_SEED = 42\ndf_train, df_test = train_test_split(df, test_size=0.1, random_state=RANDOM_SEED)\n\nprint(f\"Training samples: {len(df_train)}, Testing samples: {len(df_test)}\")\n```\n### Encoding and Vectorization\nEncode sentiment labels and vectorize the input sentences using TensorFlow's TextVectorization layer.\n```python\nfrom sklearn.preprocessing import LabelEncoder\nimport tensorflow as tf\n\n# Encode sentiment labels\nlabel_encoder = LabelEncoder()\ndf_train['sentiment'] = label_encoder.fit_transform(df_train['sentiment'])\ndf_test['sentiment'] = label_encoder.transform(df_test['sentiment'])\n\n# Define the TextVectorization layer\nmax_features = 20000  # Maximum vocabulary size\nsequence_length = 128  # Maximum sequence length\n\nvectorize_layer = tf.keras.layers.TextVectorization(\n    max_tokens=max_features,\n    output_mode='int',\n    output_sequence_length=sequence_length\n)\n\n# Adapt the vectorization layer on the training data\nvectorize_layer.adapt(df_train['sentence'].values)\n\n# Vectorize the sentences\ntrain_inputs = vectorize_layer(df_train['sentence'].values)\ntest_inputs = vectorize_layer(df_test['sentence'].values)\n\ntrain_labels = tf.convert_to_tensor(df_train['sentiment'].values)\ntest_labels = tf.convert_to_tensor(df_test['sentiment'].values)\n\n# Create TensorFlow datasets\ntrain_dataset = tf.data.Dataset.from_tensor_slices((train_inputs, train_labels))\ntest_dataset = tf.data.Dataset.from_tensor_slices((test_inputs, test_labels))\n\n# Shuffle, batch, and prefetch the datasets\nbatch_size = 32\n\ntrain_dataset = train_dataset.shuffle(buffer_size=1024).batch(batch_size).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)\ntest_dataset = test_dataset.batch(batch_size).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)\n\n```\n# Model Creation, Compilation, and Training\nCreate a Conv1D model with a trainable embedding layer for sentiment classification.\n```python\nfrom tensorflow.keras import layers, Model\n\nclass SentimentClassifier(Model):\n    def __init__(self, vocab_size, embedding_dim, n_classes):\n        super(SentimentClassifier, self).__init__()\n        self.embedding = layers.Embedding(input_dim=vocab_size, output_dim=embedding_dim)\n        self.conv = layers.Conv1D(128, 5, activation='relu')\n        self.global_pool = layers.GlobalMaxPooling1D()\n        self.dropout = layers.Dropout(0.5)\n        self.classifier = layers.Dense(n_classes, activation='softmax')\n\n    def call(self, inputs):\n        x = self.embedding(inputs)\n        x = self.conv(x)\n        x = self.global_pool(x)\n        x = self.dropout(x)\n        return self.classifier(x)\n\n# Initialize the model\nvocab_size = len(vectorize_layer.get_vocabulary())\nembedding_dim = 128\nn_classes = len(label_encoder.classes_)\n\nclassifier_model = SentimentClassifier(vocab_size, embedding_dim, n_classes)\n\n# Compile the model\nclassifier_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),\n                         loss='sparse_categorical_crossentropy',\n                         metrics=['accuracy'])\n\n# Train the model\nhistory = classifier_model.fit(\n    train_dataset,\n    epochs=20,\n    validation_data=test_dataset\n)\n\n```\n\n## Model Evaluation\nEvaluate the trained model on the test dataset and generate a classification report.\n\n```python\nfrom sklearn.metrics import classification_report\nimport numpy as np\n\n# Evaluate the model\nloss, accuracy = classifier_model.evaluate(test_dataset)\nprint(f\"Test Loss: {loss}\")\nprint(f\"Test Accuracy: {accuracy}\")\n\n# Predict labels for the test set\ny_pred_probs = classifier_model.predict(test_dataset)\ny_pred = np.argmax(y_pred_probs, axis=1)\n\n# Get the true labels\ny_true = np.concatenate([y for x, y in test_dataset], axis=0)\n\n# Convert integer class labels to strings\ntarget_names = [str(cls) for cls in label_encoder.classes_]\n\n# Print classification report\nprint(classification_report(y_true, y_pred, target_names=target_names))\n\n```\n\n## Model Saving\nSave the trained model to a file for future use.\n\n```python\nclassifier_model.save('sentiment_classifier_model.keras')\n\n```\n\n## Fetching Real-Time News and Sentiment Analysis\nUse the trained model to classify real-time financial news headlines fetched using the NewsAPI.\n```python\nfrom newsapi import NewsApiClient\n\nclass NewsFetcher:\n    def __init__(self, api_key):\n        self.newsapi = NewsApiClient(api_key=api_key)\n\n    def fetch_latest_news(self, query='stock market'):\n        all_articles = self.newsapi.get_everything(q=query,\n                                                   language='en',\n                                                   sort_by='publishedAt',\n                                                   page_size=5)\n        headlines = [article['title'] for article in all_articles['articles']]\n        return headlines\n\nnews_fetcher = NewsFetcher(api_key=\"your_api_key\")\nheadlines = news_fetcher.fetch_latest_news(query='stock market')\n\nclass SentimentAnalysisTrader:\n    def __init__(self, model, vectorize_layer):\n        self.model = model\n        self.vectorize_layer = vectorize_layer\n\n    def predict_sentiment(self, headlines):\n        inputs = self.vectorize_layer(headlines)\n        probs = self.model.predict(inputs)\n        sentiment_scores = np.argmax(probs, axis=1)\n        return sentiment_scores\n\n    def decide_trade_action(self, sentiment_score):\n        if sentiment_score == 2:\n            return \"buy\"\n        elif sentiment_score == 0:\n            return \"sell\"\n        else:\n            return \"hold\"\n\n# Initialize the trader\ntrader = SentimentAnalysisTrader(model=classifier_model, vectorize_layer=vectorize_layer)\n\n# Predict sentiment and decide trade action\nfor headline in headlines:\n    sentiment_score = trader.predict_sentiment([headline])\n    action = trader.decide_trade_action(sentiment_score[0])\n    print(f\"Headline: {headline}\\nSentiment: {sentiment_score[0]} -\u003e Action: {action}\\n\")\n\n```\n\n## License\nThis project is licensed under the MIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthepredictivedev%2Ffinancial-news-classifier-using-conv1d-nn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthepredictivedev%2Ffinancial-news-classifier-using-conv1d-nn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthepredictivedev%2Ffinancial-news-classifier-using-conv1d-nn/lists"}