【发布时间】:2021-11-06 10:52:51
【问题描述】:
如何使用 sklearn 或其他 python 包执行逆局部线性嵌入 (LLE)?
我想对一些表格数据 X 执行分类机器学习算法(SVM、神经网络...),其中 y 是目标类变量。
像往常一样,程序如下:
将 X 和 y 拆分为 X_train、y_train、X_test、y_test。由于我有大量参数(列)。我可以使用 X_train 上的 LLE 减少参数数量以获得 X_train_lle。 y 是一个目标变量,它不进行任何转换。之后,我可以简单地在 X_train_lle 上训练一个模型。当我想在 y_test 上使用经过训练的模型时,就会出现问题。如果在 X_test 和 X_train 上执行 LLE,则会引入数据泄漏。此外,如果 LLE 仅在 X_test 上执行,则新的 X_test_lle 可能会完全不同,因为该算法使用 k 个最近邻。我想正确的过程应该是在 X_test 上使用 X_train 上获得的参数执行逆 LLE,然后在 X_test_lle 上使用分类模型。
我查看了一些参考资料,第 2.4.1 节处理的是逆 LLE。 https://arxiv.org/pdf/2011.10925.pdf
如何用python(最好是sklearn)做逆LLE?
这是一个代码示例:
import numpy as np
from sklearn import preprocessing
from sklearn import svm, datasets
from sklearn.manifold import LocallyLinearEmbedding
### Generating dummy data
n_row = 10000 # these numbers are much bigger for the real problem
n_col = 50 #
X = np.random.random(n_row, n_col)
y = np.random.randint(5, size=n_row) # five different classes labeled from 0 to 4
### Preprocessing ###
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size = 0.5, random_state = 1)
#standardization using StandardScaler applied to X_train and then scaling X_train and X_test
scaler = preprocessing.StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
### Here is the part with LLE ###
# We reduce the parameter space to 10 with 15 nearest neighbours
X_train_lle = LocallyLinearEmbedding(n_neighbors=15, n_components=10, method='modified', eigen_solver='dense')
### Here is the training part ###
# we want to apply SVM to transformed data X_train_lle
#Create a svm Classifier
clf = svm.SVC(kernel='linear') # Linear Kernel
#Train the model using the training sets
clf.fit(X_train_lle, y_train)
# Here should go the code to do inverse LLE on X_test
#i.e. where do values of X_test_lle fit in the manufold X_train_lle
### After the previous part of the code was successfully solved by stackoverflow community :)
#Predict the response for test dataset
y_pred = clf.predict(X_test_lle)
【问题讨论】:
-
您可以先查看这个 scikit-learn 课程:scikit-learn.org/stable/modules/generated/… 如果您有更具体的问题,查看代码示例会有所帮助。跨度>
-
你说得对,我会放一些代码以便更好地理解问题
-
请避免与问题无关的导入墙(已编辑)。
标签: python-3.x machine-learning scikit-learn