【问题标题】:Fit clustering outputs into Machine Learning model将聚类输出拟合到机器学习模型中
【发布时间】:2020-06-11 05:59:54
【问题描述】:

只是一个机器学习/数据科学问题。

a) 假设我有一个包含 20 个特征的数据集,我决定使用 3 个特征来执行聚类的无监督学习 - 理想情况下,这会产生 3 个聚类(A、B 和 C)。

b) 然后我将输出结果(集群 A、B 或 C)作为新特征(即现在共有 21 个特征)重新放入我的数据集中。

c)我运行一个回归模型来预测具有 21 个特征的标签值。

想知道步骤 b) 是否是多余的(因为早期数据集中已经存在这些特征),我是否使用更强大的模型(随机森林,XGBoost),以及如何解释这是数学上的。

任何意见和建议都会很棒!

【问题讨论】:

  • 快速尝试一下这个想法怎么样?例如比较有/没有从集群中获取的附加功能的性能?看到这里使用 Iris 虽然不太可靠towardsdatascience.com/…
  • 你当然可以这样做。正如@Frederik Bode 所说,您将为此使用两个单独的模型。因此,可以将无监督模型的使用视为特征工程的进一步发展。或者,也可以使用此方法从输入数据中去除噪声,但在这种情况下,您宁愿不将原始特征发送到第二个模型中。我会尝试哪种变体最适合。
  • 我很想知道是否有一种数学方式来思考这个问题。但也许这只是通过实验......哈哈

标签: machine-learning data-science feature-engineering


【解决方案1】:

啊哈不错!您可能认为您正在使用两个模型,但实际上您正在将两个模型合并为一个,并带有跳过连接。由于它是一种模型,因此根据无免费午餐定理,无法事先确定最好的架构是什么。因此,实际上,您必须尝试一下,并且在数学上,由于没有免费午餐定理,事先无法知道它。

【讨论】:

  • 不确定这对作者有什么帮助——你有什么进一步的理由吗?还是建议?
  • 因为它是一种模型,根据“无免费午餐定理”,无法事先确定最好的架构是什么。因此,实际上,您必须尝试一下,而且在数学上,由于没有免费午餐定理,事先无法知道。
  • 你觉得这样更好吗?如果不是,我将删除我的答案。
  • 嗯,猜猜唯一的方法是实验?没有数学方法来分析它?
【解决方案2】:

好主意:试一试,看看效果如何。正如您所猜测的,这高度依赖于您的数据集和模型选择。很难预测添加这种类型的特性会如何表现,就像任何其他特性工程一样。但请注意,在某些情况下,它甚至不会提高您的表现。请参阅下面的测试,其中使用 Iris 数据集实际上会降低性能:

import numpy as np
from sklearn.cluster import KMeans
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn import metrics

# load data
iris = load_iris()
X = iris.data[:, :3]  # only keep three out of the four available features to make it more challenging
y = iris.target

# split train / test
indices = np.random.permutation(len(X))
N_test = 30
X_train, y_train = X[indices[:-N_test]], y[indices[:-N_test]]
X_test, y_test = X[indices[N_test:]], y[indices[N_test:]]

# compute a clustering method (here KMeans) based on available features in X_train
kmeans = KMeans(n_clusters=3, random_state=0).fit(X_train)
new_clustering_feature_train = kmeans.predict(X_train)
new_clustering_feature_test = kmeans.predict(X_test)

# create a new input train/test X with this feature added
X_train_with_clustering_feature = np.column_stack([X_train, new_clustering_feature_train])
X_test_with_clustering_feature = np.column_stack([X_test, new_clustering_feature_test])

现在让我们比较仅在 X_train 或 X_train_with_clustering_feature 上学习的两个模型:

model1 = SVC(kernel='rbf', gamma=0.7, C=1.0).fit(X_train, y_train)
print(metrics.classification_report(model1.predict(X_test), y_test))

              precision    recall  f1-score   support

           0       1.00      1.00      1.00        45
           1       0.95      0.97      0.96        38
           2       0.97      0.95      0.96        37

    accuracy                           0.97       120
   macro avg       0.97      0.97      0.97       120
weighted avg       0.98      0.97      0.97       120

还有其他型号:

model2 = SVC(kernel='rbf', gamma=0.7, C=1.0).fit(X_train_with_clustering_feature, y_train)
print(metrics.classification_report(model2.predict(X_test_with_clustering_feature), y_test))

           0       1.00      1.00      1.00        45
           1       0.87      0.97      0.92        35
           2       0.97      0.88      0.92        40

    accuracy                           0.95       120
   macro avg       0.95      0.95      0.95       120
weighted avg       0.95      0.95      0.95       120

【讨论】:

    猜你喜欢
    • 2020-06-19
    • 1970-01-01
    • 2020-03-01
    • 2022-12-10
    • 2016-10-27
    • 2018-06-03
    • 2019-01-24
    • 2019-08-14
    • 1970-01-01
    相关资源
    最近更新 更多