回归

回归#

此示例演示了如何使用 mambular 包中的 Regression 模块。

 1# Simulate data
 2import numpy as np
 3import pandas as pd
 4from mambular.models import MambularRegressor
 5from sklearn.model_selection import train_test_split
 6
 7# Set random seed for reproducibility
 8np.random.seed(0)
 9
10# Number of samples
11n_samples = 1000
12n_features = 5
13
14# Generate random features
15X = np.random.randn(n_samples, n_features)
16coefficients = np.random.randn(n_features)
17
18# Generate target variable
19y = np.dot(X, coefficients) + np.random.randn(n_samples)
20
21# Create a DataFrame to store the data
22data = pd.DataFrame(X, columns=[f"feature_{i}" for i in range(n_features)])
23data["target"] = y
24
25# Split data into features and target variable
26X = data.drop(columns=["target"])
27y = np.array(data["target"])
28
29
30X_train, X_test, y_train, y_test = train_test_split(
31    X, y, test_size=0.2, random_state=42
32)
33
34
35# Instantiate the regressor
36regressor = MambularRegressor()
37
38# Fit the model on training data
39regressor.fit(X_train, y_train, max_epochs=10)
40
41print(regressor.evaluate(X_test, y_test))