<kaggle_start><code># ## Aşağıdakı kod, kagglein verdiyi hazir koddur. Meqsedi competitionda yüklənmiş bütün faylları göstərməkdir
import numpy as np  # linear algebra
import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)
import os

for dirname, _, filenames in os.walk("/kaggle/input"):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# Aşagıdaki import etdiyim modullar,
# 1. pandas --> faylları oxumaq üçün
# 2. CountVectorizer --> text inputlarını vectora çevirmek üçün
# 3. accuracy_score  --> accuracy metricini hesablamaq üçün
# 4. precision_recall_fscore_support --> precision, recall ve fscore metriclerini hesablamaq üçün
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import accuracy_score, precision_recall_fscore_support

# Aşağıda torch ile bağlı modulları import  etmişem
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader


# Aşağıda SpamModel 2 layerli ve arasinda relu activation function olan neural network qururuq
class SpamModel(nn.Module):
    def __init__(self):
        super().__init__()
        # Please Fill 3 commented lines
        # 1. first feed forward layer, input size len(vectorizer.vocabulary), output_size 256
        # 2. relu activation function
        # 3. second feed forward layer input size output size of first feedforward layer, output size number of target labels

    def forward(self, x):
        x = x.view(-1, len(vectorizer.vocabulary_))
        x = self.fc1(x)
        x = self.relu(x)
        return self.fc2(x)


# Aşağıda Training Data üçün sentiment dataset adlı dataset yaradırıq
class SentimentDataset(Dataset):
    def __init__(self, emails, labels, vectorizer):
        self.emails = emails
        self.labels = labels
        self.vectorizer = vectorizer

    def __len__(self):
        # 1. please fill this line
        pass

    def __getitem__(self, idx):
        email = self.emails[idx].lower()
        label = self.labels[idx]
        # 1. fill this line convert labels so that spam is 1 and ham is 0
        features = vectorizer.transform([email]).todense()
        return {
            "features": torch.tensor(features, dtype=torch.float32),
            "labels": torch.tensor(label, dtype=torch.long),
        }


class SentimentTestDataset(Dataset):
    def __init__(self, emails, vectorizer):
        self.emails = emails
        self.vectorizer = vectorizer

    def __len__(self):
        # 1. please fill this line
        pass

    def __getitem__(self, idx):
        email = self.emails[idx].lower()
        # fill this line to convert email to necessary format
        # fill this line, return necessary object


train_data = pd.read_csv("/kaggle/input/spam-detection-competition/train.csv")
test_data = pd.read_csv("/kaggle/input/spam-detection-competition/test.csv")
train_features = train_data["email"]
train_labels = train_data["label"]
test_features = test_data["email"]


# Bu setirde evvel count vectorizer instanceni yaradırıq
# sonra onu training datasetin featurelarına fit edirik.
# Yeni ki vectorizerin Vocabularysini training datasetimiz ile qururuq.
count_vect = CountVectorizer(stop_words="english")
vectorizer = count_vect.fit(train_features)
train_dataset = SentimentDataset(train_features, train_labels, vectorizer)
test_dataset = SentimentTestDataset(test_features, vectorizer)
train_data_loader = DataLoader(dataset=train_dataset, batch_size=256, shuffle=True)
test_data_loader = DataLoader(dataset=test_dataset, batch_size=256, shuffle=False)
model = SpamModel()
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=2e-3)
for epoch in range(10):
    total_train_loss = 0.0
    for batch in train_data_loader:
        labels = batch["labels"]
        features = batch["features"]
        logits = model(features)
        loss_train = loss_fn(logits, labels)
        optimizer.zero_grad()
        loss_train.backward()
        optimizer.step()
        total_train_loss += loss_train.item()
    print(total_train_loss / len(train_data_loader))

# Evaluation aşaması. Bu aşamada modelin test data üzerinde nece işlediyini görürük
model = model.eval()
all_predicted = []
with torch.no_grad():
    for data in test_data_loader:
        features = data["features"]
        predicted = model(features)
        predicted = torch.sigmoid(predicted).detach().numpy()
        predicted = predicted.argmax(axis=1)
        all_predicted.extend(predicted.tolist())

# Neticeleri csv fayla yazıb submit edirik
df = pd.DataFrame({"id": list(range(len(test_dataset))), "label": all_predicted})
df.to_csv("submission_count-vector.csv", index=None)
<|endoftext|>
[{"Id": 121748209, "ScriptId": 35906894, "ParentScriptVersionId": NaN, "ScriptLanguageId": 9, "AuthorUserId": 1092846, "CreationDate": "03/11/2023 08:13:55", "VersionNumber": 1.0, "Title": "Example Template To Fill - dummy", "EvaluationDate": "03/11/2023", "IsChange": false, "TotalLines": 141.0, "LinesInsertedFromPrevious": 0.0, "LinesChangedFromPrevious": 0.0, "LinesUnchangedFromPrevious": 141.0, "LinesInsertedFromFork": 0.0, "LinesDeletedFromFork": 0.0, "LinesChangedFromFork": 0.0, "LinesUnchangedFromFork": 141.0, "TotalVotes": 0}]
# ## Aşağıdakı kod, kagglein verdiyi hazir koddur. Meqsedi competitionda yüklənmiş bütün faylları göstərməkdir
import numpy as np  # linear algebra
import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)
import os

for dirname, _, filenames in os.walk("/kaggle/input"):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# Aşagıdaki import etdiyim modullar,
# 1. pandas --> faylları oxumaq üçün
# 2. CountVectorizer --> text inputlarını vectora çevirmek üçün
# 3. accuracy_score  --> accuracy metricini hesablamaq üçün
# 4. precision_recall_fscore_support --> precision, recall ve fscore metriclerini hesablamaq üçün
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import accuracy_score, precision_recall_fscore_support

# Aşağıda torch ile bağlı modulları import  etmişem
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader


# Aşağıda SpamModel 2 layerli ve arasinda relu activation function olan neural network qururuq
class SpamModel(nn.Module):
    def __init__(self):
        super().__init__()
        # Please Fill 3 commented lines
        # 1. first feed forward layer, input size len(vectorizer.vocabulary), output_size 256
        # 2. relu activation function
        # 3. second feed forward layer input size output size of first feedforward layer, output size number of target labels

    def forward(self, x):
        x = x.view(-1, len(vectorizer.vocabulary_))
        x = self.fc1(x)
        x = self.relu(x)
        return self.fc2(x)


# Aşağıda Training Data üçün sentiment dataset adlı dataset yaradırıq
class SentimentDataset(Dataset):
    def __init__(self, emails, labels, vectorizer):
        self.emails = emails
        self.labels = labels
        self.vectorizer = vectorizer

    def __len__(self):
        # 1. please fill this line
        pass

    def __getitem__(self, idx):
        email = self.emails[idx].lower()
        label = self.labels[idx]
        # 1. fill this line convert labels so that spam is 1 and ham is 0
        features = vectorizer.transform([email]).todense()
        return {
            "features": torch.tensor(features, dtype=torch.float32),
            "labels": torch.tensor(label, dtype=torch.long),
        }


class SentimentTestDataset(Dataset):
    def __init__(self, emails, vectorizer):
        self.emails = emails
        self.vectorizer = vectorizer

    def __len__(self):
        # 1. please fill this line
        pass

    def __getitem__(self, idx):
        email = self.emails[idx].lower()
        # fill this line to convert email to necessary format
        # fill this line, return necessary object


train_data = pd.read_csv("/kaggle/input/spam-detection-competition/train.csv")
test_data = pd.read_csv("/kaggle/input/spam-detection-competition/test.csv")
train_features = train_data["email"]
train_labels = train_data["label"]
test_features = test_data["email"]


# Bu setirde evvel count vectorizer instanceni yaradırıq
# sonra onu training datasetin featurelarına fit edirik.
# Yeni ki vectorizerin Vocabularysini training datasetimiz ile qururuq.
count_vect = CountVectorizer(stop_words="english")
vectorizer = count_vect.fit(train_features)
train_dataset = SentimentDataset(train_features, train_labels, vectorizer)
test_dataset = SentimentTestDataset(test_features, vectorizer)
train_data_loader = DataLoader(dataset=train_dataset, batch_size=256, shuffle=True)
test_data_loader = DataLoader(dataset=test_dataset, batch_size=256, shuffle=False)
model = SpamModel()
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=2e-3)
for epoch in range(10):
    total_train_loss = 0.0
    for batch in train_data_loader:
        labels = batch["labels"]
        features = batch["features"]
        logits = model(features)
        loss_train = loss_fn(logits, labels)
        optimizer.zero_grad()
        loss_train.backward()
        optimizer.step()
        total_train_loss += loss_train.item()
    print(total_train_loss / len(train_data_loader))

# Evaluation aşaması. Bu aşamada modelin test data üzerinde nece işlediyini görürük
model = model.eval()
all_predicted = []
with torch.no_grad():
    for data in test_data_loader:
        features = data["features"]
        predicted = model(features)
        predicted = torch.sigmoid(predicted).detach().numpy()
        predicted = predicted.argmax(axis=1)
        all_predicted.extend(predicted.tolist())

# Neticeleri csv fayla yazıb submit edirik
df = pd.DataFrame({"id": list(range(len(test_dataset))), "label": all_predicted})
df.to_csv("submission_count-vector.csv", index=None)
