【问题标题】:Get features for maximum value of Scikit-learn estimator获取 Scikit-learn 估计器最大价值的特征
【发布时间】:2020-10-31 14:19:47
【问题描述】:

我有以下非常简单的代码试图对一个简单的数据集进行建模:

from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import GridSearchCV

data = {'Feature_A': [1, 2, 3, 4], 'Feature_B': [7, 8, 9, 10], 'Feature_C': [2, 3, 4, 5], 'Label': [7, 7, 8, 9]}

data = pd.DataFrame(data)

data_labels = data['Label']

data = data.drop(columns=['Label'])

pipeline = Pipeline([('imputer', SimpleImputer()),
                         ('std_scaler', StandardScaler())])

data_prepared = pipeline.fit_transform(data)

lin_reg = LinearRegression()
lin_grid = {"n_jobs": [20, 50]}

error = "max_error"

grid_search = GridSearchCV(lin_reg, param_grid=lin_grid, verbose=3, cv=2, refit=True, scoring=error, return_train_score=True)

grid_search.fit(data_prepared, data_labels)

print(grid_search.best_estimator_.coef_)
print(grid_search.best_estimator_.intercept_)
print(list(data_labels))
print(list(grid_search.best_estimator_.predict(data_prepared)))

这给了我以下结果:

[0.2608746 0.2608746 0.2608746]
7.75
[7, 7, 8, 9]
[6.7, 7.4, 8.1, 8.799999999999999]

从那里,有没有一种方法可以在数据集的边界内计算给我最大标签的特征值?

【问题讨论】:

    标签: python-3.x machine-learning scikit-learn


    【解决方案1】:

    如果我正确理解您的问题,这应该有效:

    import numpy as np
    id_max = np.argmax(grid_search.predict(data)) # find id of the maximum predicted label
    print(data.loc[id_max])
    

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 2018-06-01
      • 2015-07-13
      • 2017-08-01
      • 2016-12-29
      • 2016-02-25
      • 2018-02-24
      相关资源
      最近更新 更多