【问题标题】:Python ValueError: object of too small depth for desired arrayPython ValueError:所需数组的深度太小
【发布时间】:2021-11-28 22:37:57
【问题描述】:

我想获得X 的每一列与我的数据框df 的目标y 之间的直接相关性。我的代码引发了ValueError: object of too small depth for desired array 错误。

import numpy as np 
import pandas as pd
from sklearn.preprocessing import LabelEncoder, OneHotEncoder

df = pd.read_csv("mushroom_dataset.csv")

arr = df.head().to_numpy()
print(arr)
[['p' 'x' 's' 'n' 't' 'p' 'f' 'c' 'n' 'k' 'e' 'e' 's' 's' 'w' 'w' 'p' 'w'
  'o' 'p' 'k' 's' 'u']
 ['e' 'x' 's' 'y' 't' 'a' 'f' 'c' 'b' 'k' 'e' 'c' 's' 's' 'w' 'w' 'p' 'w'
  'o' 'p' 'n' 'n' 'g']
 ['e' 'b' 's' 'w' 't' 'l' 'f' 'c' 'b' 'n' 'e' 'c' 's' 's' 'w' 'w' 'p' 'w'
  'o' 'p' 'n' 'n' 'm']
 ['p' 'x' 'y' 'w' 't' 'p' 'f' 'c' 'n' 'n' 'e' 'e' 's' 's' 'w' 'w' 'p' 'w'
  'o' 'p' 'k' 's' 'u']
 ['e' 'x' 's' 'g' 'f' 'n' 'f' 'w' 'b' 'k' 't' 'e' 's' 's' 'w' 'w' 'p' 'w'
  'o' 'e' 'n' 'a' 'g']]

# X and y
X = df.loc[:, df.columns != 'class'].values
y = df.loc[:, df.columns == 'class'].values.ravel()

# Encode X and y values
X = OneHotEncoder().fit_transform(X)
y = LabelEncoder().fit_transform(y)

# Direct correlation between each column of X and the target y
corrs = np.array([np.correlate(X[:,j], y)[0] for j in range(X.shape[1])])

追溯

> --------------------------------------------------------------------------- ValueError                                Traceback (most recent call
> last) /tmp/ipykernel_4932/3530216025.py in <module>
>       1 # Direct correlation between each column of X and the target y
> ----> 2 corrs = np.array([np.correlate(X[:,j], y)[0] for j in range(X.shape[1])])
>       3 
>       4 # Reverse sort
>       5 ranks = np.argsort((-corrs))
> 
> /tmp/ipykernel_4932/3530216025.py in <listcomp>(.0)
>       1 # Direct correlation between each column of X and the target y
> ----> 2 corrs = np.array([np.correlate(X[:,j], y)[0] for j in range(X.shape[1])])
>       3 
>       4 # Reverse sort
>       5 ranks = np.argsort((-corrs))
> 
> <__array_function__ internals> in correlate(*args, **kwargs)
> 
> ~/.local/lib/python3.8/site-packages/numpy/core/numeric.py in
> correlate(a, v, mode)
>     711     """
>     712     mode = _mode_from_name(mode)
> --> 713     return multiarray.correlate2(a, v, mode)
>     714 
>     715 
> 
> ValueError: object of too small depth for desired array

【问题讨论】:

    标签: python arrays pandas numpy machine-learning


    【解决方案1】:

    错误是由OneHotEncoder() 步骤引起的。请改用此代码:

    df = pd.read_csv("mushroom_dataset.csv")
    df = df.apply(LabelEncoder().fit_transform)
    
    X = df.loc[:, df.columns != 'class'].values
    y = df.loc[:, df.columns == 'class'].values.ravel()
    
    corrs = np.array([np.correlate(X[:,j], y)[0] for j in range(X.shape[1])])
    corrs
    

    【讨论】:

      猜你喜欢
      • 2020-05-03
      • 2017-01-07
      • 2018-07-17
      • 2020-12-29
      • 2015-02-25
      • 1970-01-01
      • 2017-01-21
      • 2012-10-28
      • 1970-01-01
      相关资源
      最近更新 更多