[英]Multi-target having dependent variables as both classification and regression?
我有两个输入作为我的自变量,我想根据它预测3个因变量。
我的3个因变量属于2个多类别类,而1个具有连续值。 以下是我的目标变量。
typeid_encoded
, reporttype_encoded
, log_count
typeid_encoded
和reporttype_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.