Skip to content

Instantly share code, notes, and snippets.

@dvas0004
Created June 18, 2017 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dvas0004/1b53672df70a1339742dd9670d49c418 to your computer and use it in GitHub Desktop.
Save dvas0004/1b53672df70a1339742dd9670d49c418 to your computer and use it in GitHub Desktop.
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