-1

I want to get the direct correlation between each column of X and the target y of my dataframe df. My code raised ValueError: object of too small depth for desired array error.

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])])

Traceback

> --------------------------------------------------------------------------- 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 Answer 1

0

The error is caused by that OneHotEncoder() step. Use this code instead:

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.