分类#
本示例演示了如何使用 mambular
包中的 Classification 模块。
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from mambular.models import MambularClassifier
# Set random seed for reproducibility
np.random.seed(0)
让我们生成一些随机数据用于分类。
# Number of samples
n_samples = 1000
n_features = 5
生成随机特征
X = np.random.randn(n_samples, n_features)
coefficients = np.random.randn(n_features)
生成目标变量
y = np.dot(X, coefficients) + np.random.randn(n_samples)
## Convert y to multiclass by categorizing into quartiles
y = pd.qcut(y, 4, labels=False)
创建一个 DataFrame 来存储数据
data = pd.DataFrame(X, columns=[f"feature_{i}" for i in range(n_features)])
data["target"] = y
将数据分割为特征和目标变量
X = data.drop(columns=["target"])
y = data["target"].values
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
实例化分类器并在训练数据上拟合模型
classifier = MambularClassifier()
# Fit the model on training data
classifier.fit(X_train, y_train, max_epochs=10)
print(classifier.evaluate(X_test, y_test))