text_plain_output_3.png text_plain_output_2.png text_plain_output_1.png
from IPython.display import display

from torch.utils.data import Dataset, DataLoader

import matplotlib.pyplot as plt

import os

import pandas as pd

import torch

import torch.nn as nn

DATA_PATH = '/kaggle/input/playground-series-s3e14/'
TRAIN_FILE = 'train.csv'
TEST_FILE = 'test.csv'
train_df = pd.read_csv(os.path.join(DATA_PATH, TRAIN_FILE), index_col=0)
test_df = pd.read_csv(os.path.join(DATA_PATH, TEST_FILE), index_col=0)
target = 'yield'

cols = [col for col in test_df.columns]

fig, axes = plt.subplots(4, 4, figsize=(16, 16))
for i, col in enumerate(cols):
    axes[i//4, i%4].hist(train_df[col], bins=20)
    axes[i//4, i%4].set_title(col, fontsize=14)
plt.show()

import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

class BlueberryDataset(Dataset):
    def __init__(self, X, y=None, train=False):
        self.train = train
        if self.train:
            self.X = X
            self.y = y
        else:
            self.X = X
    def __len__(self):
        return len(self.X)
    def __getitem__(self, idx):
        if self.train:
            x = torch.tensor(self.X.iloc[idx]).to(dtype=torch.float32)
            y = torch.tensor(self.y.iloc[idx]).to(dtype=torch.float32)
            return (x, y)
        else:
            x = torch.tensor(self.X.iloc[idx]).to(dtype=torch.float32)
            return x
train_dataset = BlueberryDataset(X_train, y_train, train=True)
train_dataloader = DataLoader(train_dataset, batch_size=128, shuffle=True)
val_dataset = BlueberryDataset(X_val, y_val, train=True)
val_dataloader = DataLoader(val_dataset, batch_size=128, shuffle=True)
test_dataset = BlueberryDataset(test_df, train=False)
test_dataloader = DataLoader(test_dataset, batch_size=128, shuffle=False)

class SimpleModel(nn.Module):
    def __init__(self, in_features=test_df.shape[1], out_features=1):
        super().__init__()
        self.fc1 = nn.Sequential(nn.Linear(in_features, 64), nn.ReLU(), nn.Linear(64, 64), nn.ReLU(), nn.Linear(64, 16), nn.ReLU(), nn.Linear(16, out_features))
    def forward(self, x):
        return self.fc1(x)

model = SimpleModel().to(device)
sample = torch.randn(64, test_df.shape[1]).to(device)

criterion = nn.L1Loss()
optimizer = torch.optim.AdamW(params=model.parameters(), lr=0.001)

EPOCHS = 50
for epoch in range(EPOCHS):
    model.train()
    train_loss = 0
    for i, (X_tr, y_tr) in enumerate(train_dataloader):
        X_tr = X_tr.to(device)
        y_tr = y_tr.to(device).reshape(-1, 1)
        y_pred = model(X_tr)
        loss = criterion(y_pred, y_tr)
        train_loss += loss.item()
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    val_loss = 0
    model.eval()
    with torch.inference_mode():
        for X_v, y_v in val_dataloader:
            X_v = X_v.to(device)
            y_v = y_v.to(device).reshape(-1, 1)
            val_pred = model(X_v)
            loss = criterion(val_pred, y_v)
            val_loss += loss.item()
    train_loss /= len(train_dataloader)
    val_loss /= len(val_dataloader)

results = []
model.eval()
with torch.inference_mode():
    for X_ts in test_dataloader:
        test_pred = model(X_ts)
        results.extend(test_pred.reshape(-1).tolist())