|
|
|
"""ML.ipynb |
|
|
|
Automatically generated by Colab. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1N6R2R3PY04PitBN4M6QNX-tuBPdqglVz |
|
""" |
|
|
|
from google.colab import drive |
|
drive.mount('/content/drive') |
|
|
|
import pandas as pd |
|
|
|
file_path = '/content/drive/My Drive/CIS 519 Final Project/Dataset/cleaned_headlines.csv' |
|
df = pd.read_csv(file_path) |
|
df |
|
|
|
class_counts = df['outlet'].value_counts() |
|
print(class_counts) |
|
|
|
from sklearn.model_selection import train_test_split |
|
X = df['title'] |
|
y = df['labels'] |
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
|
|
|
from sklearn.feature_extraction.text import TfidfVectorizer |
|
tfidf = TfidfVectorizer(max_features=5000, ngram_range=(1, 2), stop_words='english') |
|
X_train_tfidf = tfidf.fit_transform(X_train) |
|
X_test_tfidf = tfidf.transform(X_test) |
|
|
|
"""# Logistic Regression""" |
|
|
|
from sklearn.linear_model import LogisticRegression |
|
from sklearn.metrics import accuracy_score, classification_report |
|
model = LogisticRegression(max_iter=200) |
|
model.fit(X_train_tfidf, y_train) |
|
y_pred = model.predict(X_test_tfidf) |
|
accuracy = accuracy_score(y_test, y_pred) |
|
print(f"Logistic Regression Accuracy: {accuracy:.4f}") |
|
print(classification_report(y_test, y_pred)) |
|
|
|
"""# Random Forest""" |
|
|
|
from sklearn.ensemble import RandomForestClassifier |
|
model = RandomForestClassifier(n_estimators=100, random_state=42) |
|
|
|
model.fit(X_train_tfidf, y_train) |
|
y_pred = model.predict(X_test_tfidf) |
|
accuracy = accuracy_score(y_test, y_pred) |
|
print(f"Random Forest Accuracy: {accuracy:.4f}") |
|
print(classification_report(y_test, y_pred)) |
|
|
|
"""# Support Vector Machine""" |
|
|
|
from sklearn.svm import SVC |
|
svm_model = SVC(kernel='linear', random_state=42) |
|
svm_model.fit(X_train_tfidf, y_train) |
|
y_pred = svm_model.predict(X_test_tfidf) |
|
accuracy = accuracy_score(y_test, y_pred) |
|
print(f"Random Forest Accuracy: {accuracy:.4f}") |
|
print(classification_report(y_test, y_pred)) |
|
|
|
|