Dados não estruturados

Projetando Workflows de Machine Learning em Python

Dr. Chris Anagnostopoulos

Honorary Associate Professor

Estruturados versus não estruturados

  Class   AGE  SEX  STEROID    ...        
0    2.0  50.0  2.0      1.0    ...      
1    2.0  40.0  1.0      1.0    ...       
...
           label                                           sequence
0          VIRUS  AVTVVPDPTCCGTLSFKVPKDAKKGKHLGTFDIRQAIMDYGGLHSQ...
1  IMMUNE SYSTEM  QVQLQQPGAELVKPGASVKLSCKASGYTFTSYWMHWVKQRPGRGLE...
2  IMMUNE SYSTEM  QAVVTQESALTTSPGETVTLTCRSSTGAVTTSNYANWVQEKPDHLF...
3          VIRUS  MSQVTEQSVRFQTALASIKLIQASAVLDLTEDDFDFLTSNKVWIAT...
...

Podemos criar um detector que marque vírus como anomalias nesses dados?

Projetando Workflows de Machine Learning em Python
import stringdist
stringdist.levenshtein('abc', 'acc')
1
stringdist.levenshtein('acc', 'cce')
2
             label   sequence
169  IMMUNE SYSTEM  ILSALVGIV
170  IMMUNE SYSTEM  ILSALVGIL
stringdist.levenshtein('ILSALVGIV', 'ILSALVGIL')
1
Projetando Workflows de Machine Learning em Python

Depurando um pouco

# Isso não vai funcionar
pdist(proteins['sequence'].iloc[:3], metric=stringdist.levenshtein)
Traceback (most recent call last):
ValueError: A 2-dimensional array must be passed.
Projetando Workflows de Machine Learning em Python

Depurando um pouco

sequences = np.array(proteins['sequence'].iloc[:3]).reshape(-1,1)

# Isto não vai funcionar por outro motivo pdist(sequences, metric=stringdist.levenshtein)
Traceback (most recent call last):
TypeError: argument 1 must be str, not numpy.ndarray
Projetando Workflows de Machine Learning em Python

Depurando um pouco

# Este funciona!!
def my_levenshtein(x, y):
    return stringdist.levenshtein(x[0], y[0])

pdist(sequences, metric=my_levenshtein)
array([136.,   2., 136.])
Projetando Workflows de Machine Learning em Python

Outliers em proteínas com matrizes precomputadas

# Isso leva 2 minutos para ~1000 exemplos
M = pdist(sequences, my_levenshtein)

Detector LoF com matriz de distâncias precomputada:

# Isso leva 3 segundos
detector = lof(metric='precomputed', contamination=0.1)
preds = detector.fit_predict(M)
roc_auc_score(proteins['label'] == 'VIRUS', preds == -1)
0.64
Projetando Workflows de Machine Learning em Python

Escolha sua distância

Projetando Workflows de Machine Learning em Python

Preparing Video For Download...