Skip to content

Instantly share code, notes, and snippets.

@ibayer
Created July 9, 2012 21:13
Show Gist options
  • Save ibayer/3078964 to your computer and use it in GitHub Desktop.
Save ibayer/3078964 to your computer and use it in GitHub Desktop.
Testing how sparse datasets that are only available as RData file could be converted to the mldata.org hdf5 format. This is not working as the hdf5 specification stated on mldata.org is not recognized from the there parser.
from scipy.io.mmio import mminfo,mmread,mmwrite
import numpy as np
import h5py
import os
os.chdir('/home/mane/workspace/benchmark/')
print mminfo('InternetAd')
X = mmread('InternetAd.mtx')
y = np.loadtxt('InternetAd.target', skiprows=0)
f = h5py.File('InternetAd.h5', 'w')
# HDF5 attributes on the root level
f.attrs['mldata'] = 0
f.attrs['name'] = 'InternetAd'
f.attrs['comment'] = 'Internet-Ad [Kushmerick, 1999]: document classification problem with \
mostly binary features.'
#HDF5 group 'data_descr'
f.create_group('/data_descr')
f['/data_descr/ordering'] = np.array(['label', 'data'])
#HDF5 group 'data'
f.create_group('/data')
f['/data/data'] = X.data
f['/data/data_indices'] = X.tocsc().indices
f['/data/data_indptr'] = X.tocsc().indptr
f['/data/label'] = y
f.close()
load('InternetAd.RData')
file = file.path(getwd(), 'InternetAd.mtx')
library(Matrix)
writeMM(InternetAd$x, file=file)
file = file.path(getwd(), 'InternetAd.target')
write(InternetAd$y, file=file, ncolumns=1)
load('NewsGroup.RData')
file = file.path(getwd(), 'NewGroup.mtx')
library(Matrix)
writeMM(NewsGroup$x, file=file)
file = file.path(getwd(), 'NewsGroup.target')
write(NewsGroup$y, file=file, ncolumns=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment