亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在sklearn中使用DataFrameMapper()進行PolynomialFeature

在sklearn中使用DataFrameMapper()進行PolynomialFeature

慕尼黑5688855 2023-08-22 17:16:34
對于住房數據集,我嘗試使用 sklearn_pandas 中的 DataFrameMapper() 在選定的列上應用多項式特征。我的代碼:from sklearn.preprocessing import PolynomialFeatures from sklearn_pandas import DataFrameMapper mapper = DataFrameMapper([('houseAge_income', PolynomialFeatures(2)),('median_income', PolynomialFeatures(2)),(['latitude', 'housing_median_age', 'total_rooms', 'population', 'median_house_value', 'ocean_proximity']], None) ]) poly_feature = mapper.fit_transform(housing) 當我嘗試使用houseAge_income.reshape(-1, 1)在 DataFrameMapper() 中,我遇到了另一個問題:---------------------------------------------------------------------------KeyError                                  Traceback (most recent call last)/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)   2645             try:-> 2646                 return self._engine.get_loc(key)   2647             except KeyError:pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()KeyError: 'houseAge_income.reshape(-1, 1)'During handling of the above exception, another exception occurred:KeyError                                  Traceback (most recent call last)5 frames/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)   2646                 return self._engine.get_loc(key)   2647             except KeyError:-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)   2650         if indexer.ndim > 1 or indexer.size > 1:pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()誰能告訴我,我錯過了什么?
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

  • 從文檔中

    • 'column'將列選擇器指定為(作為簡單字符串)和['column'](作為包含一個元素的列表)之間的區別在于傳遞給轉換器的數組的形狀。在第一種情況下,將傳遞一個一維數組,而在第二種情況下,將傳遞一個具有一列的二維數組,即列向量。

  • 所有列必須使用相同類型的列選擇器傳遞。

    • 在本例中,為 a?list,因為需要list保留一些未轉換的列。

import pandas as pd

from sklearn.preprocessing import PolynomialFeatures

from sklearn_pandas import DataFrameMapper


# load data

df = pd.read_csv('https://raw.githubusercontent.com/ageron/handson-ml2/master/datasets/housing/housing.csv')


# create houseAge_income

df['houseAge_income'] = df.housing_median_age.mul(df.median_income)


# configure mapper with all columns passed as lists

mapper = DataFrameMapper([(['houseAge_income'], PolynomialFeatures(2)),

? ? ? ? ? ? ? ? ? ? ? ? ? (['median_income'], PolynomialFeatures(2)),

? ? ? ? ? ? ? ? ? ? ? ? ? (['latitude', 'housing_median_age', 'total_rooms', 'population', 'median_house_value', 'ocean_proximity'], None)])


# fit

poly_feature = mapper.fit_transform(df)


# display(pd.DataFrame(poly_feature).head())

? 0? ? ? ?1? ? ? ? ? ?2? 3? ? ? ?4? ? ? ?5? ? ? 6? ?7? ? ?8? ? ?9? ? ? ? ? 10? ? ? ? 11

0? 1? 341.33? 1.1651e+05? 1? 8.3252? 69.309? 37.88? 41? ?880? ?322? 4.526e+05? NEAR BAY

1? 1? 174.33? ? ? ?30391? 1? 8.3014? 68.913? 37.86? 21? 7099? 2401? 3.585e+05? NEAR BAY

2? 1? 377.38? 1.4242e+05? 1? 7.2574? ?52.67? 37.85? 52? 1467? ?496? 3.521e+05? NEAR BAY

3? 1? 293.44? ? ? ?86108? 1? 5.6431? 31.845? 37.85? 52? 1274? ?558? 3.413e+05? NEAR BAY

4? 1? ? ?200? ? ? ?40001? 1? 3.8462? 14.793? 37.85? 52? 1627? ?565? 3.422e+05? NEAR BAY




查看完整回答
反對 回復 2023-08-22
  • 1 回答
  • 0 關注
  • 272 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號