Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from sklearn.decomposition import DictionaryLearning
from sklearn.decomposition import SparseCoder
import pandas as pd
# load data from CSV
df = pd.read_csv('/mnt/c/Users/davev/Documents/test_sparse.csv')
# get rid of the "label" column - AS Number in our case
del df['AS Number']
# change data into required format from scikit learn
t=df.as_matrix()
# create a dictionary with 2 components (to make it easier to plot later)
# the dictionary is learnt by iterating over the data a 100 times
dict=DictionaryLearning(n_components=2, max_iter=100)
dict.fit(t)
# load the dictionary we just created into a Sparse Coder
sp = SparseCoder(dict.components_)
# instruct the sparse coder to represent our data in terms of the dictionary we previously "learnt"
sp.transform(t)
# ... [results displayed] ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment