def test_sparse_vectorss(): svss = sparse_vectorss() assert len(svss) == 0 svss.resize(5) for svs in svss: assert len(svs) == 0 svss.clear() assert len(svss) == 0 svss.extend([ sparse_vectors([ sparse_vector([pair(1, 2), pair(3, 4)]), sparse_vector([pair(5, 6), pair(7, 8)]) ]) ]) assert len(svss) == 1 assert svss[0][0][0].first == 1 assert svss[0][0][0].second == 2 assert svss[0][0][1].first == 3 assert svss[0][0][1].second == 4 assert svss[0][1][0].first == 5 assert svss[0][1][0].second == 6 assert svss[0][1][1].first == 7 assert svss[0][1][1].second == 8 deser = pickle.loads(pickle.dumps(svss, 2)) assert deser == svss
def sentence_to_sparse_vectors(sentence): vects = dlib.sparse_vectors() has_cap = dlib.sparse_vector() no_cap = dlib.sparse_vector() # make has_cap equivalent to dlib.vector([1]) has_cap.append(dlib.pair(0,1)) # Since we didn't add anything to no_cap it is equivalent to dlib.vector([0]) for word in sentence.split(): if (word[0].isupper()): vects.append(has_cap) else: vects.append(no_cap) return vects
def sentence_to_sparse_vectors(sentence): vects = dlib.sparse_vectors() has_cap = dlib.sparse_vector() no_cap = dlib.sparse_vector() # make has_cap equivalent to dlib.vector([1]) has_cap.append(dlib.pair(0, 1)) # Since we didn't add anything to no_cap it is equivalent to dlib.vector([0]) for word in sentence.split(): if (word[0].isupper()): vects.append(has_cap) else: vects.append(no_cap) return vects
def training_data(): r = Random(0) predictors = vectors() sparse_predictors = sparse_vectors() response = array() for i in range(30): for c in [-1, 1]: response.append(c) values = [r.random() + c * 0.5 for _ in range(3)] predictors.append(vector(values)) sp = sparse_vector() for i, v in enumerate(values): sp.append(pair(i, v)) sparse_predictors.append(sp) return predictors, sparse_predictors, response
def training_data(): r = Random(0) predictors = vectors() sparse_predictors = sparse_vectors() response = array() for i in range(30): for c in [-1, 1]: response.append(c) values = [r.random() + c * 0.5 for _ in range(3)] predictors.append(vector(values)) sp = sparse_vector() for i, v in enumerate(values): sp.append(pair(i, v)) sparse_predictors.append(sp) return predictors, sparse_predictors, response