【发布时间】: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