Repository navigation
Expand file tree
/
Copy pathrandom_forest.py
More file actions
48 lines (40 loc) · 2.02 KB
/
Copy pathrandom_forest.py
File metadata and controls
48 lines (40 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from tqdm import tqdm
import time
def log_progress(message):
timestamp = time.strftime("[%Y-%m-%d %H:%M:%S]", time.localtime())
print(f"{timestamp} {message}")
try:
# Load the dataset
log_progress("Loading dataset...")
df = pd.read_csv('./CSV Files/training_dataset.csv')
# Drop rows with missing values in the 'Cleaned tweets' column
log_progress("Dropping rows with missing values...")
df.dropna(subset=['Cleaned tweets'], inplace=True)
# Split the dataset into training and testing sets
log_progress("Splitting dataset into train and test sets...")
X_train, X_test, y_train, y_test = train_test_split(df['Cleaned tweets'], df['Sentiment'], test_size=0.2, random_state=42)
# Vectorize the tweets
log_progress("Vectorizing tweets...")
vectorizer = CountVectorizer()
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)
# Initialize and train the Random Forest classifier
log_progress("Training Random Forest...")
with tqdm(total=100, desc="Training Progress", bar_format='{l_bar}{bar}{r_bar}') as pbar:
rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)
rf_classifier.fit(X_train_vec, y_train)
pbar.update(100) # Increment progress bar to 100%
# Predict sentiment on the test set using Random Forest
log_progress("Predicting sentiment with Random Forest...")
with tqdm(total=len(X_test), desc="Prediction Progress", bar_format='{l_bar}{bar}{r_bar}') as pbar:
rf_y_pred = rf_classifier.predict(X_test_vec)
pbar.update(len(X_test)) # Increment progress bar to 100%
rf_accuracy = accuracy_score(y_test, rf_y_pred)
log_progress(f"Random Forest Accuracy: {rf_accuracy}")
except Exception as e:
log_progress(f"An error occurred: {e}")