In [1]:
import sklearn_crfsuite
In [2]:
X = ['CGGCGGAATTATTATTA', 'GCCGACTTATATATGGGGCCCGCTATATA']
y = ['++++++-----------', '++++++--------+++++++++------']
In [3]:
def extract_features(X):
    features = [] 
    
    for i in range(len(X)):    
        feature = {
            'bias': 1,
            'lower': X[i].lower(),
        }
        
        if i > 0:
            feature.update({
                '-1:lower': X[i - 1].lower(),
            })
            
        features.append([feature])
    
    return features
In [4]:
X_train = [extract_features(x) for x in X]
X_train
Out[4]:
[[[{'bias': 1, 'lower': 'c'}],
  [{'bias': 1, 'lower': 'g', '-1:lower': 'c'}],
  [{'bias': 1, 'lower': 'g', '-1:lower': 'g'}],
  [{'bias': 1, 'lower': 'c', '-1:lower': 'g'}],
  [{'bias': 1, 'lower': 'g', '-1:lower': 'c'}],
  [{'bias': 1, 'lower': 'g', '-1:lower': 'g'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 'g'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 'a'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 'a'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 'a'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 'a'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 't'}]],
 [[{'bias': 1, 'lower': 'g'}],
  [{'bias': 1, 'lower': 'c', '-1:lower': 'g'}],
  [{'bias': 1, 'lower': 'c', '-1:lower': 'c'}],
  [{'bias': 1, 'lower': 'g', '-1:lower': 'c'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 'g'}],
  [{'bias': 1, 'lower': 'c', '-1:lower': 'a'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 'c'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 'a'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 'a'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 'a'}],
  [{'bias': 1, 'lower': 'g', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 'g', '-1:lower': 'g'}],
  [{'bias': 1, 'lower': 'g', '-1:lower': 'g'}],
  [{'bias': 1, 'lower': 'g', '-1:lower': 'g'}],
  [{'bias': 1, 'lower': 'c', '-1:lower': 'g'}],
  [{'bias': 1, 'lower': 'c', '-1:lower': 'c'}],
  [{'bias': 1, 'lower': 'c', '-1:lower': 'c'}],
  [{'bias': 1, 'lower': 'g', '-1:lower': 'c'}],
  [{'bias': 1, 'lower': 'c', '-1:lower': 'g'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 'c'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 'a'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 't'}],
  [{'bias': 1, 'lower': 't', '-1:lower': 'a'}],
  [{'bias': 1, 'lower': 'a', '-1:lower': 't'}]]]
In [5]:
y_train = [[x for x in list(label)] for label in y]
y_train
Out[5]:
[['+',
  '+',
  '+',
  '+',
  '+',
  '+',
  '-',
  '-',
  '-',
  '-',
  '-',
  '-',
  '-',
  '-',
  '-',
  '-',
  '-'],
 ['+',
  '+',
  '+',
  '+',
  '+',
  '+',
  '-',
  '-',
  '-',
  '-',
  '-',
  '-',
  '-',
  '-',
  '+',
  '+',
  '+',
  '+',
  '+',
  '+',
  '+',
  '+',
  '+',
  '-',
  '-',
  '-',
  '-',
  '-',
  '-']]
In [ ]:
crf = sklearn_crfsuite.CRF(
    algorithm='lbfgs',
    c1=0.1,
    c2=0.1,
    max_iterations=100,
    all_possible_transitions=True
)

y_train[0]

crf.fit(X_train[0], y_train[0])