Python/sklearn - 预处理.MinMaxScaler 1d 废弃警告

9

我希望将数据框中的一列缩放到0和1之间的值。为此,我使用了一个MinMaxScaler,它可以很好地工作,但是让我有些犹豫。我正在这样做:

x = df['Activity'].values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df['Activity'] = pd.Series(x_scaled)

这段代码的第一条信息是一个警告:

DeprecationWarning: 0.17版本已经弃用将1d数组作为数据传递,0.19版本将会引发ValueError错误。如果您的数据只有一个特征,请使用X.reshape(-1,1)来调整数据格式;如果它只包含一个样本,请使用X.reshape(1,-1)。

显然,以后将不再支持1d数组,因此让我们按照建议进行调整:

x = df['Activity'].values.reshape(-1, 1)

现在代码甚至无法运行:异常:数据必须是一维的被抛出。所以我很困惑。1d即将被弃用,但数据也必须是1d?如何安全地解决这个问题?这里有什么问题?
根据@sascha的要求进行编辑: x看起来像这样:
array([ 0.00568953,  0.00634314,  0.00718003, ...,  0.01976002,
        0.00575024,  0.00183782])

重塑后:

array([[ 0.00568953],
       [ 0.00634314],
       [ 0.00718003],
       ..., 
       [ 0.01976002],
       [ 0.00575024],
       [ 0.00183782]])

整个警告:

/usr/local/lib/python3.5/dist-packages/sklearn/preprocessing/data.py:321: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)
/usr/local/lib/python3.5/dist-packages/sklearn/preprocessing/data.py:356: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)

当我进行重塑时出现错误:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-132-df180aae2d1a> in <module>()
      2 min_max_scaler = preprocessing.MinMaxScaler()
      3 x_scaled = min_max_scaler.fit_transform(x)
----> 4 telecom['Activity'] = pd.Series(x_scaled)

/usr/local/lib/python3.5/dist-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    225             else:
    226                 data = _sanitize_array(data, index, dtype, copy,
--> 227                                        raise_cast_failure=True)
    228 
    229                 data = SingleBlockManager(data, index, fastpath=True)

/usr/local/lib/python3.5/dist-packages/pandas/core/series.py in _sanitize_array(data, index, dtype, copy, raise_cast_failure)
   2918     elif subarr.ndim > 1:
   2919         if isinstance(data, np.ndarray):
-> 2920             raise Exception('Data must be 1-dimensional')
   2921         else:
   2922             subarr = _asarray_tuplesafe(data, dtype=dtype)

Exception: Data must be 1-dimensional

展示 x 重塑前后的形状。同时阅读有关期望数组形式的文档。输入始终类似于一维,但需要如单行展示,例如(1个样本情况下;因此形状实际上是2)。 - sascha
@sascha 根据要求更新了问题。 - lte__
你确定错误不是在pd.Series()这一行吗?这并不奇怪。我真的不明白为什么人们不发布整个堆栈跟踪/错误信息...仅显示错误本身而没有更多上下文,会丢失那么多信息。 - sascha
1
现在你添加了整个堆栈跟踪:你看到我猜对了问题,而且它甚至告诉你问题的具体是什么。提示:与sklearn无关。提示2:你想使用sklearn的数据格式来构建一个系列,但它并不是一个类似系列的格式。 - sascha
很奇怪它不能在一维数组上工作,这似乎是一个自然的使用情况。 - eric
显示剩余2条评论
1个回答

22
你可以简单地使用 pd.Series
import pandas as pd
from sklearn import preprocessing
df = pd.DataFrame({'Activity': [ 0.00568953,  0.00634314,  0.00718003, 
                                0.01976002, 0.00575024,  0.00183782]})
x = df['Activity'].values.reshape(-1, 1) #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df['Activity'] = x_scaled

或者您可以明确获取x_scaled的第一列:

df['Activity'] = pd.Series(x_scaled[:, 0])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接