Why am i getting index error on this one hot encoding?

Viewed 60
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('netflixprice.csv')
x = dataset.iloc[:,0].values
y = dataset.iloc[:, 1:6].values

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [0])], remainder='passthrough')
x = np.array(ct.fit_transform(x))
IndexError                                Traceback (most recent call last)
Input In [8], in <cell line: 4>()
      2 from sklearn.preprocessing import OneHotEncoder
      3 ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [0])], remainder='passthrough')
----> 4 x = np.array(ct.fit_transform(x))

data structure

New to this. Also anywhere i can learn more about data processing ?

1 Answers

It's hard to tell anything without knowing the structure of your data. However, it seems like you may want to reshape your x:

x = dataset.iloc[:, 0].values.reshape(-1, 1)

I could find a dataset that might be similar to yours and tried it, it worked.

As for learning how to process the data: I personally try to refer to the documentation of a method I want to apply. In your case it's here. However, a clue to where the problem was I could find in the error message:

def _get_column_indices(X, key):
     """Get feature column indices for input data X and key.
 
     For accepted values of `key`, see the docstring of
     :func:`_safe_indexing_column`.
     """
-->  n_columns = X.shape[1]  # this is where the problem is
     key_dtype = _determine_key_type(key)
     if isinstance(key, (list, tuple)) and not key:
         # we get an empty list

IndexError: tuple index out of range

That made me suspect that you got an ndarray shaped (n,) when sliced x, which doesn't have columns that were required.

It also seems like you intended x to be the target rather than the only feature. With 6 other columns assigned to y you may want to swap x and y. You may still encode your target like you planned.

Related