Skip to content

Include basis functions #18

@plgreenLIRU

Description

@plgreenLIRU

From chatgpt:

import numpy as np
from sklearn.linear_model import LinearRegression

def gaussian_basis(X, centers, width):
# Compute squared Euclidean distance from each point to each center
diff = X[:, np.newaxis, :] - centers[np.newaxis, :, :] # shape: (n_samples, n_centers, n_features)
dist_sq = np.sum(diff ** 2, axis=2) # shape: (n_samples, n_centers)
return np.exp(-dist_sq / (2 * width ** 2))

X is (n_samples, n_features)

Choose centers (e.g., via k-means or grid over feature space)

from sklearn.cluster import KMeans

n_centers = 20
kmeans = KMeans(n_clusters=n_centers, random_state=0).fit(X_train)
centers = kmeans.cluster_centers_
width = 1.0 # tune this based on spread of data

Transform features

X_train_transformed = gaussian_basis(X_train, centers, width)
X_test_transformed = gaussian_basis(X_test, centers, width)

Fit model

model = LinearRegression()
model.fit(X_train_transformed, y_train)

Predict

y_pred = model.predict(X_test_transformed)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions