Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i can't apply labelencoder to array of bool

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading