-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
43 lines (35 loc) · 1.2 KB
/
Copy pathmodel.py
File metadata and controls
43 lines (35 loc) · 1.2 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
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
import re
# LOAD TRAINING DATA
train_df = pd.read_csv('data_train_hw4_problem1.csv', encoding='latin1')
train_df['spam'] = train_df['spam'].astype(int)
# LOAD TEST DATA
test_df = pd.read_csv('data_test_hw4_problem1.csv', encoding='latin1')
# PREPROCESS FUNCTION
def clean_text(text):
text = text.lower()
text = re.sub(r'[^\w\s]', '', text)
text = re.sub(r'\d+', '', text)
return text
# CLEAN TEXTS
train_df['text'] = train_df['text'].apply(clean_text)
test_df['text'] = test_df['text'].apply(clean_text)
# VECTORIZE
vectorizer = TfidfVectorizer(stop_words='english', max_features=5000)
X_train = vectorizer.fit_transform(train_df['text'])
X_test = vectorizer.transform(test_df['text'])
y_train = train_df['spam']
# TRAIN MODEL
model = LinearSVC(random_state=42, max_iter=10000)
model.fit(X_train, y_train)
# PREDICT
predictions = model.predict(X_test)
# OUTPUT PREDICTIONS
import csv
with open('predictions.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['spam'])
for pred in predictions:
writer.writerow(['TRUE' if pred == 1 else 'FALSE'])