import numpy as np from sklearn.kernel_ridge import KernelRidge # Generate random data X_train = np.random.rand(100, 5) y_train = np.random.rand(100, 1) X_test = np.random.rand(50, 5) # Train the model clf = KernelRidge(alpha=1.0) clf.fit(X_train, y_train) # Predict on test data y_pred = clf.predict(X_test)
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score from sklearn.kernel_ridge import KernelRidge # Load the dataset iris = load_iris() # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2) # Train the model clf = KernelRidge(alpha=1.0, kernel='rbf') clf.fit(X_train, y_train) # Predict on test data y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print('Accuracy Score:', accuracy)In this example, we use the KernelRidge function to train a model with a radial basis function kernel. We then predict on some new test data and calculate the accuracy score of the model. The package library used in these examples is scikit-learn or sklearn.