代码之家  ›  专栏  ›  技术社区  ›  Chris Arthur

scikit学习中的预处理-单个样本-折旧警告

  •  48
  • Chris Arthur  · 技术社区  · 9 年前

    在Ubuntu下的Anaconda新安装上……在使用Scikit Learn进行分类任务之前,我正在以各种方式预处理我的数据。

    from sklearn import preprocessing
    
    scaler = preprocessing.MinMaxScaler().fit(train)
    train = scaler.transform(train)    
    test = scaler.transform(test)
    

    这一切都很好,但如果我有一个新的样本(下面是临时的),我想进行分类(因此我想以相同的方式进行预处理),那么我会得到

    temp = [1,2,3,4,5,5,6,....................,7]
    temp = scaler.transform(temp)
    

    然后我收到一个不推荐的警告。。。

    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. 
    

    所以问题是我应该如何像这样重新缩放单个样本?

    我想另一种选择(不是很好的选择)是。。。

    temp = [temp, temp]
    temp = scaler.transform(temp)
    temp = temp[0]
    

    但我相信还有更好的方法。

    8 回复  |  直到 8 年前
        1
  •  50
  •   Prometheus    5 年前

    听听警告告诉你的:

    如果数据只有一个特征/列,则可以使用X.Reshape(-1,1)重塑数据 如果X.reshape(1,-1)包含单个样本。

    对于示例类型(如果您有多个要素/列):

    temp = temp.reshape(1,-1) 
    

    对于一个特征/列:

    temp = temp.reshape(-1,1)
    
        2
  •  34
  •   Ami Tavory    9 年前

    事实上,看起来警告告诉你该怎么做。

    作为的一部分 sklearn.pipeline stages' uniform interfaces ,根据经验法则:

    • 当你看到 X ,它应该是 np.array 具有二维

    • 当你看到 y ,它应该是 np阵列 具有单一维度。

    因此,您应考虑以下事项:

    temp = [1,2,3,4,5,5,6,....................,7]
    # This makes it into a 2d array
    temp = np.array(temp).reshape((len(temp), 1))
    temp = scaler.transform(temp)
    
        3
  •  9
  •   Bharath M Shetty    7 年前

    这可能会有所帮助

    temp = ([[1,2,3,4,5,6,.....,7]])
    
        4
  •  3
  •   Arun Vinoth PrecogTechnologies    7 年前

    .values.reshape(-1,1) 将在没有警报/警告的情况下接受

    .reshape(-1,1) 将被接受,但与贬低战争

        5
  •  0
  •   shan89    7 年前

    我面临着同样的问题,也得到了同样的警告。当我收到消息时,我正在使用一个[23276]的numpy数组。我试着按照警告重塑它,结果一无所获。然后我从numpy数组中选择每一行(因为我一直在迭代),并将其分配给一个列表变量。当时它毫无征兆地工作了。

    array = []
    array.append(temp[0])
    

    然后,您可以使用python列表对象(此处为“数组”)作为sk-learn函数的输入。不是最有效的解决方案,但对我有效。

        6
  •  0
  •   vimuth    5 年前

    你总是可以像这样重塑:

    temp = [1,2,3,4,5,5,6,7]
    
    temp = temp.reshape(len(temp), 1)
    

    因为,主要的问题是当你的temp.shape是: (8,)

    你需要 (8,1)

        7
  •  0
  •   Kuntal Bhattacharjee    2 年前

    -1是数组的未知维度。阅读有关“newshape”参数的更多信息 numpy.reshape 文件-

    # X is a 1-d ndarray
    
    # If we want a COLUMN vector (many/one/unknown samples, 1 feature)
    X = X.reshape(-1, 1)
    
    # you want a ROW vector (one sample, many features/one/unknown)
    X = X.reshape(1, -1)
    
        8
  •  0
  •   gregor256    2 年前
    from sklearn.linear_model import LinearRegression
    X = df[['x_1']] 
    X_n = X.values.reshape(-1, 1)
    y = df['target']  
    y_n = y.values
    model = LinearRegression()
    model.fit(X_n, y)
    
    y_pred = pd.Series(model.predict(X_n), index=X.index)