3

I am trying to analyze my data using sklearn and see if there is some correlation between elements. My data set is a short protein motif which is quite diverse in sequence. My input looks like this:

  1p 2p 3p 4p 5p               genus
0  T  V  H  F  K  Enterobacteriaceae
1  T  V  M  F  M         Escherichia
2  E  I  H  V  K  Enterobacteriaceae
3  K  L  M  F  K  Enterobacteriaceae

There are 20 different letter possibilities at positions 1-5.

I wanted to use similar approach as it was shown in the sklearn Iris set to check dependencies between amino acids in different positions and bacterial genus. In the other words, I want to see if sequence of letters is genus specific and if letter at single position is somehow related to letters in other positions.

Problem is, as far as I know, only numbers can be used as an input for sklearn. I tried to subsitute letters with numbers: from 1e-10 to 1e10 for each individual letter, but I had problems with data visualization later. I hope there is other, way more efficient way of using this kind of input data. I would be very grateful for some hints. Thanks!

1
  • Sklearn needs numerical data, you have to convert to them. In visualization you can specify xlabel or ylabel specifically in matplotlib.
    – sarthak
    Jan 2, 2018 at 19:05

1 Answer 1

5

I would suggest the use of LabelEncoder

from sklearn.preprocessing import LabelEncoder

df
  1p 2p 3p 4p 5p               genus
0  T  V  H  F  K  Enterobacteriaceae
1  T  V  M  F  M         Escherichia
2  E  I  H  V  K  Enterobacteriaceae
3  K  L  M  F  K  Enterobacteriaceae
le = LabelEncoder()
le.fit(np.unique(df.drop('genus', axis=1)))
X = np.array([le.transform(samp) for samp in df.drop('genus', axis=1).values])
X
array([[7, 8, 2, 1, 4],
       [7, 8, 6, 1, 6],
       [0, 3, 2, 8, 4],
       [4, 5, 6, 1, 4]])

From there you should be able to check the correlations.

2
  • 1
    One more question: Is it possible, easily, to decompose the numerical data back to string elements?
    – Dawid
    Jan 4, 2018 at 19:58
  • @Dawid you can use the inverse_transform method to go from labels to category names
    – Grr
    Jan 10, 2018 at 15:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.