Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yamasakih/3251b9e7feea427f450535403c82071a to your computer and use it in GitHub Desktop.
Save yamasakih/3251b9e7feea427f450535403c82071a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Comparison of speeds of DataFrame and svmlight files"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sys.version_info(major=3, minor=6, micro=2, releaselevel='final', serial=0)\n",
"sklearn version = 0.20.0\n"
]
}
],
"source": [
"from itertools import product\n",
"from pathlib import Path\n",
"import sys\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"import sklearn\n",
"from sklearn.datasets import dump_svmlight_file, load_svmlight_file\n",
"\n",
"\n",
"%matplotlib inline\n",
"\n",
"print(sys.version_info)\n",
"print(f'sklearn version = {sklearn.__version__}')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def make_dataset(num_samples, num_bits, on_bit_ratio):\n",
" np.random.seed(20181021)\n",
"\n",
" X = np.random.binomial(1, on_bit_ratio, size=(num_samples, num_bits))\n",
" X = pd.DataFrame(X)\n",
" y = np.random.rand(num_samples).round(3) \n",
" return X, y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Make dataset"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Make dataset directory"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"p = Path('./dataset')\n",
"p.mkdir(parents=True, exist_ok=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Make dataset parameters"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"num_sample_set = (1000, 2000, 4000, 8000)\n",
"num_bit_set = (256, 512, 1024, 2048, 4096)\n",
"on_bit_ratios = (.1, .3, .5, .7, .9)\n",
"random_seeds = (0, 1, 2, 3, 4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Make dataset in dataset directory"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"for num_samples, num_bits, on_bit_ratio, random_seed in product(num_sample_set, \n",
" num_bit_set, on_bit_ratios, random_seeds):\n",
" # Make dataset\n",
" X, y = make_dataset(num_samples, num_bits, on_bit_ratio)\n",
" file_name = f'{num_samples}_{num_bits}_{int(on_bit_ratio*100)}_{random_seed}.csv'\n",
"\n",
" # Save X and y as svmlight data\n",
" dump_svmlight_file(X, y, f'dataset/svmlight_{file_name}')\n",
"\n",
" # Save X and y as dataframe\n",
" X.insert(loc=0, column='y', value=y)\n",
" X.to_csv(f'dataset/dataframe_{file_name}', index=False, header=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# EOF"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment