簡體   English   中英

多目標同時具有因變量的分類和回歸功能?

[英]Multi-target having dependent variables as both classification and regression?

我有兩個輸入作為我的自變量,我想根據它預測3個因變量。

我的3個因變量屬於2個多類別類,而1個具有連續值。 以下是我的目標變量。

typeid_encodedreporttype_encodedlog_count

typeid_encodedreporttype_encoded屬於分類類型,其中每個變量至少具有5個不同的類別。

log_count是連續變量。

我在Google上搜索了很多,發現的全部是使用兩種不同的模型。 但是我找不到任何可以這樣做的例子。 請發布一些示例,以便對我有幫助?

還是可以在一個模型中使用神經網絡的其他方法?

我需要一個使用sci-kit學習的示例。 提前致謝!

sklearn中沒有專門為此設計的東西,但是您可以使用一些小技巧來制作這樣的分類器。

請注意 ,這些不一定適合您的問題,很難猜測對您的數據有什么用。

我首先想到的兩個是Knn和Random Forests,但是您可以從本質上調整任何多輸出回歸算法來執行這些操作。

import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.neighbors import NearestNeighbors

# Create some data to look like yours
n_samples = 100
n_features = 5

X = np.random.random((n_samples, n_features))
y_classifcation = np.random.random((n_samples, 2)).round()
y_regression = np.random.random((n_samples))

y = np.hstack((y_classifcation, y_regression[:, np.newaxis]))

現在我有一個包含兩個二進制變量和一個連續變量的數據集

從Knn開始,您也可以使用KNeighborsRegressor來執行此KNeighborsRegressor ,但是我覺得這更好地說明了解決方案

# use an odd number to prevent tie-breaks
nn = NearestNeighbors(n_neighbors=5)
nn.fit(X, y)

idxs = nn.kneighbors(X, return_distance=False)
# take the average of the nearest neighbours to get the predictions
y_pred = y[idxs].mean(axis=1)
# all predictions will be continous so just round the continous ones
y_pred[:, 2] = y_pred[:, 2].round()

現在,我們的y_pred是分類和回歸預測的向量。 現在讓我們來看一個隨機森林。

# use an odd number of trees to prevent predictions of 0.5
rf = RandomForestRegressor(n_estimators=11)
rf.fit(X, y)
y_pred = rf.predict(X)

# all predictions will be continous so just round the continous ones
y_pred[:, 2] = y_pred[:, 2].round()

我想說這些“黑客”是相當合理的,因為它們與這些算法的分類設置的工作方式相距不遠。

如果您有一個多類別問題,並且您已經對其進行了一個熱編碼,那么您將需要選擇概率最高的類別,而不是像我上面所做的那樣將概率四舍五入為二進制類別。 您可以使用以下類似方法輕松完成此操作

n_classes_class1 = 3
n_classes_class2 = 4
y_pred_class1 = np.argmax(y_pred[:, :n_classes_class1], axis=1)
y_pred_class2 = np.argmax(y_pred[:, n_classes_class1:-1], axis=1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM