[英]Fit clustering outputs into Machine Learning model
只是一個機器學習/數據科學問題。
a)假設我有一個包含 20 個特征的數據集,我決定使用 3 個特征來執行無監督聚類學習 - 理想情況下這會產生 3 個集群(A、B 和 C)。
b)然后我將該輸出結果(集群 A、B 或 C)作為新特征(即現在總共 21 個特征)擬合回我的數據集。
c)我運行一個回歸模型來預測具有 21 個特征的標簽值。
想知道步驟b)是否是多余的(因為這些特征已經存在於早期的數據集中),我是否使用更強大的模型(隨機森林,XGBoost),以及如何從數學上解釋這一點。
任何意見和建議都會很棒!
啊哈不錯! 您可能認為您正在使用兩個模型,但實際上您將兩個模型合並為一個,並使用跳過連接。 由於它是一種模型,因此根據 No Free Lunch Theorem,無法事先確定最好的架構是什么。 因此,實際上,您必須嘗試一下,並且從數學上講,由於沒有免費午餐定理,因此事先不知道。
好主意:試一試,看看效果如何。 正如您所猜測的,這高度依賴於您的數據集和模型選擇。 很難預測添加這種類型的特征會如何表現,就像任何其他特征工程一樣。 但請注意,在某些情況下,它甚至不會提高您的性能。 使用 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.