This project is a simple implementation of a fake news detection model using Python, scikit-learn, and the PassiveAggressiveClassifier. The model can classify news articles as either REAL or FAKE based on their content.
The project uses two datasets:
- true.csv: Contains true news articles.
- fake.csv: Contains fake news articles.
true.csv: CSV file containing true news articles.fake.csv: CSV file containing fake news articles.index.py: The main Python script for training and testing the model.model.pkl: The saved machine learning model (optional, created after running the script).vectorizer.pkl: The saved TF-IDF vectorizer (optional, created after running the script).README.md: Project documentation.
-
Clone the repository:
git clone https://github.com/surya2821/machine.git
-
Install the required Python packages:
pip install pandas numpy scikit-learn
-
Add your datasets (
true.csvandfake.csv) to the project directory.
-
Run the
index.pyscript to train the model:index.py
-
The script will:
- Combine the
true.csvandfake.csvdatasets. - Train a
PassiveAggressiveClassifierto detect fake news. - Display the model's accuracy and confusion matrix.
- Save the trained model and vectorizer to
model.pklandvectorizer.pkl.
- Combine the
-
To predict whether a new piece of text is real or fake, use the
predict_fake_newsfunction in the script:example_text = "Your article text here" prediction = predict_fake_news(example_text) print(f"Prediction: {prediction}")
After running the script, you should see output similar to:
Datasets loaded successfully.
Datasets combined.
Dataset shuffled.
(44898, 5)
title ... label
0 You Can Smell It: Donald Trump Jr.... ... REAL
1 Is It An NFL Sunday If Odell Beckham... ... FAKE
Accuracy: 92.74%
Confusion Matrix:
[[5897 303]
[ 334 5829]]
Model and vectorizer saved.