scikit-learn中的目标转换和特征选择 - python

我在scikit-learn中使用RFECV进行功能选择。我想将简单线性模型(X,y)的结果与对数转换模型(使用X, log(y))的结果进行比较

简单模型:
RFECVcross_val_score提供相同的结果(我们需要比较所有特征交叉验证的平均得分与所有功能的RFECV得分:0.66 = 0.66,没问题,结果是可靠)

日志模型:
问题:RFECV似乎没有提供转换y的方法。在这种情况下的分数是0.55 vs 0.53。不过,这是完全可以预期的,因为我必须手动应用np.log来适应数据:log_seletor = log_selector.fit(X,np.log(y))。 r2分数是针对y = log(y)的,没有inverse_func,而我们需要的是一种使模型适合log(y_train)并使用exp(y_test)计算分数的方法。或者,如果尝试使用TransformedTargetRegressor,则会收到代码中显示的错误:分类器不公开“ coef_”或“ feature_importances_”属性

如何解决问题并确保功能选择过程可靠?

from sklearn.datasets import make_friedman1
from sklearn.feature_selection import RFECV
from sklearn import linear_model
from sklearn.model_selection import cross_val_score
from sklearn.compose import TransformedTargetRegressor
import numpy as np

X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
estimator = linear_model.LinearRegression()
log_estimator = TransformedTargetRegressor(regressor=linear_model.LinearRegression(),
                                                func=np.log,
                                                inverse_func=np.exp)
selector = RFECV(estimator, step=1, cv=5, scoring='r2')
selector = selector.fit(X, y)
###
# log_selector = RFECV(log_estimator, step=1, cv=5, scoring='r2')
# log_seletor = log_selector.fit(X,y) 
# #RuntimeError: The classifier does not expose "coef_" or "feature_importances_" attributes
###
log_selector = RFECV(estimator, step=1, cv=5, scoring='r2')
log_seletor = log_selector.fit(X,np.log(y))

print("**Simple Model**")
print("RFECV, r2 scores: ", np.round(selector.grid_scores_,2))
scores = cross_val_score(estimator, X, y, cv=5)
print("cross_val, mean r2 score: ", round(np.mean(scores),2), ", same as RFECV score with all features") 
print("no of feat: ", selector.n_features_ )

print("**Log Model**")
log_scores = cross_val_score(log_estimator, X, y, cv=5)
print("RFECV, r2 scores: ", np.round(log_selector.grid_scores_,2))
print("cross_val, mean r2 score: ", round(np.mean(log_scores),2)) 
print("no of feat: ", log_selector.n_features_ )

输出:

**Simple Model**
RFECV, r2 scores:  [0.45 0.6  0.63 0.68 0.68 0.69 0.68 0.67 0.66 0.66]
cross_val, mean r2 score:  0.66 , same as RFECV score with all features
no of feat:  6

**Log Model**
RFECV, r2 scores:  [0.39 0.5  0.59 0.56 0.55 0.54 0.53 0.53 0.53 0.53]
cross_val, mean r2 score:  0.55
no of feat:  3

参考方案

您需要做的就是将这些属性添加到TransformedTargetRegressor

class MyTransformedTargetRegressor(TransformedTargetRegressor):
    @property
    def feature_importances_(self):
        return self.regressor_.feature_importances_

    @property
    def coef_(self):
        return self.regressor_.coef_

然后在您的代码中使用:

log_estimator = MyTransformedTargetRegressor(regressor=linear_model.LinearRegression(),
                                             func=np.log,
                                             inverse_func=np.exp)

Python-scikit_learn中的克里金(高斯过程) - python

我正在考虑使用此方法对我拥有的3D点进行插值。作为输入,我在一个区域中的各个高度具有大气浓度的气体。我所获得的数据显示为垂直高度每隔几英尺延伸几十英尺,但水平分开数百英尺的值(因此,“列”紧密堆积)。假定在任何给定时间点,值在垂直方向上的变化比在水平方向上的变化大得多。我想在考虑到这一假设的情况下执行3D克里金法(作为我可以调整的参数,或者是经过统计定义的参…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Python:无法识别Pip命令 - python

这是我拍摄的屏幕截图。当我尝试在命令提示符下使用pip时,出现以下错误消息:pip无法识别为内部或外部命令,可操作程序或批处理文件。我已经检查了这个线程:How do I install pip on Windows?我所能找到的就是我必须将"C:\PythonX\Scripts"添加到我的类路径中,其中X代表python版本。如您在我的…

scikit-学习LogisticRegression.predict_proba的返回值 - python

LogisticRegression.predict_proba函数究竟返回什么?在我的示例中,我得到这样的结果:[[ 4.65761066e-03 9.95342389e-01] [ 9.75851270e-01 2.41487300e-02] [ 9.99983374e-01 1.66258341e-05]] 根据其他计算,使用S形函数,我知道第二列是概…

Python:如何将有效的uuid从String转换为UUID? - python

我收到的数据是 { "name": "Unknown", "parent": "Uncategorized", "uuid": "06335e84-2872-4914-8c5d-3ed07d2a2f16" }, 我需要将uuid从Strin…