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 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
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