Python ValueError:所需数组的深度太小

Posted

技术标签:

【中文标题】Python ValueError:所需数组的深度太小【英文标题】:Python ValueError: object of too small depth for desired array 【发布时间】: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

【问题讨论】:

【参考方案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

【讨论】:

以上是关于Python ValueError:所需数组的深度太小的主要内容,如果未能解决你的问题,请参考以下文章

ValueError: '对象对于所需数组来说太深'

ValueError:对象太深,无法在 optimize.curve_fit 中找到所需数组

对象深度以获得所需的数组,numpy随机选择

Python:ValueError:使用序列设置数组元素

python-sklearn中出现“ValueError:预期的二维数组,得到一维数组”错误[重复]

ValueError:使用序列python,numpy设置数组元素[重复]