You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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)
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)
' ' '
The text was updated successfully, but these errors were encountered: