Python 3.6 - SciKitLearn - 模块没有属性' CategoricalEncoder'

时间:2018-01-12 18:22:20

标签: python scikit-learn python-import

我跟随着Scikit-Learn& amp; Tensorflow&#39 ;. 现在,我试图使用sklearn' CategoricalEncoder,但是python拒绝接受它。

这是我现在的代码:

from util.fetch import *
from data.data import *
import matplotlib.pyplot as plot
from sklearn.model_selection import *
from sklearn.preprocessing import *
from sklearn.pipeline import *
import pandas.plotting as pdp


def main():

    #
    #   Basic Data Exploration
    #

    fetch_housing_data()
    housing = load_housing_data()

    housing.hist(bins=50, figsize=(20,15))
    plot.savefig(os.path.join('datasets', 'histogram.png'))

    #
    #   Creating a Test Set
    #

    train_set, test_set = train_test_split(housing, test_size=0.2, random_state=42)

    housing['income_cat'] = np.ceil(housing['median_income'] / 1.5)
    housing['income_cat'].where(housing['income_cat'] < 5, 5.0, inplace=True)

    split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)
    for train_index, test_index in split.split(housing, housing['income_cat']):
        strat_train_set = housing.loc[train_index]
        strat_test_set = housing.loc[test_index]

    for set_ in (strat_train_set, strat_test_set):
        set_.drop('income_cat', axis=1, inplace=True)

    #
    #   In-depth exploration of Training Data
    #

    housing = strat_train_set.copy()

    #
    #       Plot Geographical Data
    #

    housing.plot(
        kind='scatter',
        x='longitude',
        y='latitude',
        alpha=0.4,
        s=housing['population']/100,
        label='population',
        figsize=(10,7),
        c='median_house_value',
        cmap=plot.get_cmap('jet'),
        colorbar=True)

    #plot.show()
    plot.savefig(os.path.join('datasets','geodata.png'))

    corr_matrix = housing.corr()

    print(corr_matrix['median_house_value'].sort_values(ascending=False))

    attributes = ['median_house_value','median_income','total_rooms','housing_median_age']
    pdp.scatter_matrix(housing[attributes], figsize=(12,8))
    plot.savefig(os.path.join('datasets', 'scatter_matrix.png'))

    housing.plot(
        kind='scatter',
        x='median_income',
        y='median_house_value',
        alpha=0.1)
    plot.savefig(os.path.join('datasets', 'median_income-median_house_value.png'))

    housing['rooms_per_household'] = housing['total_rooms'] / housing['households']
    housing['bedrooms_per_room'] = housing['total_bedrooms'] / housing['total_rooms']
    housing['population_per_household'] = housing['population'] / housing['households']

    corr_matrix = housing.corr()

    corr_file = open(os.path.join('datasets','median_house_value_corr.txt'), 'w+')
    corr_file.write(str(corr_matrix['median_house_value'].sort_values(ascending=False)))
    corr_file.close()

    #
    #   Preparing data for Machine Learning Algorithms
    #
    housing = strat_train_set.drop('median_house_value', axis=1)
    housing_labels = strat_train_set['median_house_value'].copy()

    num_attribs = list(housing_num)
    cat_attribs = ['ocean_proximity']

    num_pipeline: Pipeline = Pipeline([
        ('selector', DataFrameSelector(num_attribs)),
        ('imputer', Imputer(strategy='median')),
        ('attribs_adder', CombinedAttributesAdder()),
        ('std_scaler', StandardScaler()),
    ])

    cat_pipeline: Pipeline = Pipeline([
        ('selector', DataFrameSelector(cat_attribs)),
        ('cat_encoder', CategoricalEncoder(encoding='onehot-dense'))
    ])

    full_pipeline = FeatureUnion(transformer_list=[
        ('num_pipeline', num_pipeline),
        ('cat_pipeline', cat_pipeline),
    ])


if __name__ == '__main__':
    main()

这给了我最后的问题,但当它到达第123行时,我收到以下错误:

Traceback (most recent call last):
  File "***/Housing/main.py", line 133, in <module>
    main()
  File "***/Housing/main.py", line 123, in main
    ('cat_encoder', CategoricalEncoder(encoding='onehot-dense'))
NameError: name 'CategoricalEncoder' is not defined

(我在堆栈跟踪中检查了路径)

我已经尝试了各种各样的东西来让它正确导入,但遗憾的是我失败了。使用任何其他sklearn类,只使用CategoricalEncoder,没有任何问题。这本书提到了CategoricalEncoder类是相当新的,可能还没有出来,是这样吗?

我检查了sklearn文档,并提供了实现此功能的源代码的链接,当我查看虚拟环境中的相应文件时,我至少缺少400-500行代码我正在使用PyCharm并让它根据需要安装所有依赖项。

(我知道最后的管道现在还没有运行,因为脚本在那之前停止执行,它不是真正的问题......)

1 个答案:

答案 0 :(得分:3)

CategoricalEncoder仅在开发版本0.20.dev0中可用。 pip,conda或任何其他软件包管理器不会自动安装此版本,如果您想使用该版本,则需要专门安装它。

在这种情况下,您必须按照Sklean - Advanced Installation - Install Bleeding Edge中的说明获取开发版本。