Home > database >  i can't apply labelencoder to array of bool
i can't apply labelencoder to array of bool

Time:01-12

I am on a machine learning project. I did import all libraries. I took one column of data(this column is array of bool) and i want to apply it labelencoder. Here is my whole code.

data = pd.read_csv('odev_tenis.csv')

le = preprocessing.LabelEncoder()

ruzgar = data.iloc[:,3:4].values #the column i want to apply labelencoder
ruzgar[:,3:4] = le.fit_transform(data.iloc[:,3:4])

And here is the error i got.

ValueError: could not broadcast input array from shape (14,) into shape (14,0)

CodePudding user response:

The issue is with the line ruzgar[:,3:4] = le.fit_transform(data.iloc[:,3:4]).

The reason for the error is that you are trying to assign the transformed values to a slice of the array which has no elements. The slice ruzgar[:,3:4] is equivalent to ruzgar[:,3:3] which selects no elements from the array.

Instead of slicing the array, you can directly assign the transformed values to the whole array:

ruzgar = le.fit_transform(data.iloc[:,3:4].values)

This will correctly apply the label encoder to the values in the 'ruzgar' column and store the transformed values in the 'ruzgar' array.

Additionally, you can use .values after calling data.iloc[:,3:4] to convert the column to numpy array and it will work as well.

ruzgar = le.fit_transform(data.iloc[:,3:4].values.ravel())

You can use .ravel() function to flatten the array and avoid the broadcasting error.

CodePudding user response:

data.iloc[:,3:4] does not return a Series but a DataFrame of 1 column. Try:

ruzgar = le.fit_transform(data.iloc[:, 3])
  • Related