Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the predictions for regression task? #488

Open
ED20B012 opened this issue Nov 4, 2024 · 0 comments
Open

How to get the predictions for regression task? #488

ED20B012 opened this issue Nov 4, 2024 · 0 comments

Comments

@ED20B012
Copy link

ED20B012 commented Nov 4, 2024

This is my simple code to load data from a csv and train the model for a regression task.
How do I retrieve the predictions so that I can visualise them and calculate metrics?
' ' '
import numpy as np
import pandas as pd
import torch
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from kan import * # Importing PyKAN

def get_csv_data(file_path, test_split=0.2, device="cpu"):
# Load the data from the CSV file
data = pd.read_csv(file_path)

# Separate features (X) and target (y)
target_column = data.columns[-1]
print(target_column)
X = data.drop(columns=[target_column]).values  # All columns except the target
y = data[target_column].values.reshape(-1, 1)  # Target column as a 2D array for scaling

# Scale features and target
scaler_X = StandardScaler()
scaler_y = StandardScaler()
X = scaler_X.fit_transform(X)
y = scaler_y.fit_transform(y).flatten()  # Flatten to keep y as a 1D array for PyTorch

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_split, random_state=0)

# Convert data to PyTorch tensors
dataset = {
    'train_input': torch.tensor(X_train, dtype=torch.float32).to(device),
    'train_label': torch.tensor(y_train, dtype=torch.float32).to(device),
    'test_input': torch.tensor(X_test, dtype=torch.float32).to(device),
    'test_label': torch.tensor(y_test, dtype=torch.float32).to(device)
}

# Return dataset and scalers for potential inverse transformation later
return dataset, scaler_X, scaler_y

Specify your CSV file path and target column

file_path = 'data.csv' # Replace with your CSV file path

target_column = 'target' # Replace with the name of the target column

Load the dataset and scalers

dataset, scaler_X, scaler_y = get_csv_data(file_path, test_split=0.2, device="cpu")

Define the KAN model

model = KAN(width=[dataset['train_input'].shape[1], 4, 1], grid=3, k=3, seed=0, device="cpu")

Train the model

model.fit(dataset, opt="LBFGS", steps=50)

' ' '

@ED20B012 ED20B012 closed this as completed Nov 4, 2024
@ED20B012 ED20B012 reopened this Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant