Python Sklearn LabelEncoder inverse transform

AI Thread Summary
To inverse a label transformation using sklearn's LabelEncoder and obtain an array with the shape (10, 7), the 'reshape' function can be applied after using 'inverse_transform'. The process involves first transforming the original array into a label-encoded format and then using 'inverse_transform' to revert it back. The final step is to reshape the resulting array to the desired dimensions. This method effectively restores the original array structure while maintaining the integrity of the data.
BRN
Messages
107
Reaction score
10
Hi everyone!

I need to inverse an label transform with sklearn. I found this example on web:

[CODE lang="python" title="example"]
from sklearn.preprocessing import LabelEncoder

np.random.seed(1)
y = np.random.randint(0, 2, (10, 7))
y = y[np.where(y.sum(axis=1) != 0)[0]]
[/CODE]
[CODE title="output:"]
array([[1, 1, 0, 0, 1, 1, 1],
[1, 1, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 0],
[1, 0, 0, 0, 1, 1, 1],
[1, 1, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 1, 1],
[0, 0, 1, 0, 0, 1, 1],
[1, 0, 1, 0, 0, 1, 1],
[0, 1, 1, 1, 1, 0, 0]])
[/CODE]

[CODE lang="python" title="example"]
le = LabelEncoder()
y_new = le.fit_transform([''.join(str(l)) for l in y])
[/CODE]
[CODE title="output:"]
array([7, 6, 3, 3, 2, 5, 8, 0, 4, 1])
[/CODE]

[CODE lang="python" title="example"]
y_old = le.inverse_transform(y_new)
[/CODE]
[CODE title="output:"]
array(['[1 1 0 0 1 1 1]', '[1 1 0 0 1 0 1]', '[1 0 0 1 0 0 0]',
'[1 0 0 1 0 0 0]', '[1 0 0 0 1 1 1]', '[1 1 0 0 0 1 1]',
'[1 1 1 1 0 1 1]', '[0 0 1 0 0 1 1]', '[1 0 1 0 0 1 1]',
'[0 1 1 1 1 0 0]'], dtype='<U15')
[/CODE]

With 'inverse_transform' How can I obtain an array with shape (10, 7)?

Thaks!
 
Technology news on Phys.org


Hi there!

To obtain an array with shape (10, 7), you can use the 'reshape' function after applying the 'inverse_transform' method. Here's an example:

y_old = le.inverse_transform(y_new).reshape(10,7)

This will reshape the array to have 10 rows and 7 columns. Hope this helps!
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top