/code/preprocessing.ipynb
Jupyter | 188 lines | 188 code | 0 blank | 0 comment | 0 complexity | a0a8f9df18794247acdcb7fe8ee76246 MD5 | raw file
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import pandas as pd\n",
- "import numpy as np"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "accel00001 = pd.read_csv('/SPHERE-Challenge/data/raw_data/public_data/train/00001/acceleration.csv')\n",
- "target00001 = pd.read_csv('/SPHERE-Challenge/data/raw_data/public_data/train/00001/targets.csv')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {
- "collapsed": false,
- "scrolled": true
- },
- "outputs": [],
- "source": [
- "def window_feat(t_start, t_end, df, columns):\n",
- " \n",
- " ''' Extract statistical features for a given window and sensor data frame df '''\n",
- " \n",
- " window_size = t_end - t_start\n",
- " window_data = df[(df['t'] >= t_start) & (df['t'] < t_end)]\n",
- " window_data = window_data[columns]\n",
- " \n",
- " feat = [] # empty features list\n",
- " fnames = [] # empty feature names list\n",
- "\n",
- " # quantiles\n",
- " feat = feat + window_data.quantile(q = [0, 0.25, 0.50, 0.75, 1]).values.flatten().tolist()\n",
- " fnames = ( fnames + \n",
- " [ (column + '_W' + str(window_size) + '_0Q') for column in window_data.columns] + \n",
- " [ (column + '_W' + str(window_size) + '_25Q') for column in window_data.columns] +\n",
- " [ (column + '_W' + str(window_size) + '_50Q') for column in window_data.columns] +\n",
- " [ (column + '_W' + str(window_size) + '_75Q') for column in window_data.columns] +\n",
- " [ (column + '_W' + str(window_size) + '_100Q') for column in window_data.columns] )\n",
- " \n",
- " # mean\n",
- " feat = feat + window_data.mean().tolist()\n",
- " fnames = fnames + [ (column + '_W' + str(window_size) + '_mean') for column in window_data.columns]\n",
- " \n",
- " # std\n",
- " feat = feat + window_data.std().tolist()\n",
- " fnames = fnames + [ (column + '_W' + str(window_size) + '_std') for column in window_data.columns]\n",
- " \n",
- " return feat, fnames"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "feats = []\n",
- "\n",
- "for idx in target00001.index: # [0,1823]\n",
- " \n",
- " s = target00001.loc[idx,'start']\n",
- " e = target00001.loc[idx,'end']\n",
- " \n",
- " # extract features\n",
- " feat_W1, fnames_W1 = window_feat(t_start = e - 1, t_end = e, df = accel00001, columns = ['x', 'y', 'z'])\n",
- " feat_W2, fnames_W2 = window_feat(t_start = e - 2, t_end = e, df = accel00001, columns = ['x', 'y', 'z'])\n",
- " feat_W3, fnames_W3 = window_feat(t_start = e - 3, t_end = e, df = accel00001, columns = ['x', 'y', 'z'])\n",
- " feat_W4, fnames_W4 = window_feat(t_start = e - 4, t_end = e, df = accel00001, columns = ['x', 'y', 'z'])\n",
- " \n",
- " # consolidate features\n",
- " feat = feat_W1 + feat_W2 + feat_W3 + feat_W4\n",
- " fnames = fnames_W1 + fnames_W2 + fnames_W3 + fnames_W4\n",
- " \n",
- " feats.append(feat)\n",
- " \n",
- "feats = pd.DataFrame(feats, columns = fnames)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "# create features for all train data\n",
- "targets = target00001.iloc[:,2:22]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "feats = feats[np.isfinite(targets.sum(axis=1, skipna=False))]\n",
- "targets = targets[np.isfinite(targets.sum(axis=1, skipna=False))]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 52,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "import xgboost"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 54,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "ename": "ValueError",
- "evalue": "DataFrame for label cannot have multiple columns",
- "output_type": "error",
- "traceback": [
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
- "\u001b[1;32m<ipython-input-54-4c763af0299c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mxgboost\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDMatrix\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfeats\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlabel\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtargets\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[1;32m/opt/conda/lib/python3.5/site-packages/xgboost-0.4-py3.5.egg/xgboost/core.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, data, label, missing, weight, silent, feature_names, feature_types)\u001b[0m\n\u001b[0;32m 220\u001b[0m \u001b[0mfeature_names\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 221\u001b[0m feature_types)\n\u001b[1;32m--> 222\u001b[1;33m \u001b[0mlabel\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_maybe_pandas_label\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 223\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 224\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mSTRING_TYPES\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;32m/opt/conda/lib/python3.5/site-packages/xgboost-0.4-py3.5.egg/xgboost/core.py\u001b[0m in \u001b[0;36m_maybe_pandas_label\u001b[1;34m(label)\u001b[0m\n\u001b[0;32m 164\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mDataFrame\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 165\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 166\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'DataFrame for label cannot have multiple columns'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 167\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 168\u001b[0m \u001b[0mlabel_dtypes\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlabel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdtypes\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;31mValueError\u001b[0m: DataFrame for label cannot have multiple columns"
- ]
- }
- ],
- "source": [
- "xgboost.DMatrix(data = feats, label = targets)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python [Root]",
- "language": "python",
- "name": "Python [Root]"
- },
- "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.5.2"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
- }