Skip to content

Instantly share code, notes, and snippets.

@coreylynch
Created November 27, 2012 16:27
Show Gist options
  • Save coreylynch/4155258 to your computer and use it in GitHub Desktop.
Save coreylynch/4155258 to your computer and use it in GitHub Desktop.
saves compressed numpy file for fast loading later
import pandas, re, numpy as np
def load_file(filename, num_cols, dtypes, delimiter='\t'):
data = None
try:
data = np.fromfile(filename + '.npy', dtype=dtypes)
except:
splitter = re.compile(delimiter)
def items(infile):
for line in infile:
for item in splitter.split(line):
yield item
with open(filename, 'r') as infile:
data = np.fromiter(items(infile), dtypes, -1)
data = data.reshape((-1, num_cols))
np.save(filename, data)
return pandas.DataFrame(data)
if __name__ == "__main__":
df = pd.read_csv('downsample.csv')
types = []
for i in df.columns:
try:
i = float(i)
types.append(type(i))
except ValueError:
types.append(type(i))
dtypes = np.dtype([('feature_'+str(i),types[i]) for i in range(len(types))])
load_file('kddcup.data',42, dtypes, delimiter=',')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment