{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "WISDM.ipynb",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true,
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "LhHGvYR3KNKG",
"colab_type": "text"
},
"source": [
"# Overview #\n",
"\n",
"- This is a notebook prepared for you to try out classification tasks using time-series data acquired using accelerometers of iPhone's.\n",
"\n",
"- You'll get to:\n",
" - train a model based on neural networks \n",
" - learn about ```CoreML```\n",
" \n",
"- Post-demo exercise: try to replicate below using the [Actitracker dataset](http://www.cis.fordham.edu/wisdm/includes/datasets/latest/WISDM_at_latest.tar.gz)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FqM7vSoRHyg3",
"colab_type": "text"
},
"source": [
"# A) Mount Google drive #\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "zKhYw_BxHyAa",
"colab_type": "code",
"outputId": "f70691c6-cf6f-4081-dd83-d0d8e7174b35",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')\n",
"\n",
"from pydrive.auth import GoogleAuth\n",
"from pydrive.drive import GoogleDrive\n",
"from google.colab import auth\n",
"from oauth2client.client import GoogleCredentials"
],
"execution_count": 47,
"outputs": [
{
"output_type": "stream",
"text": [
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rKZJs-RrIhKO",
"colab_type": "text"
},
"source": [
"# B) Create subfolder ``wisdm`` in your ```opensource_datasets``` folder #"
]
},
{
"cell_type": "code",
"metadata": {
"id": "MRx2Rs5oIfSe",
"colab_type": "code",
"outputId": "12fa9bc6-22d8-4b07-de80-fb8c1dafd3d2",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
}
},
"source": [
"try:\n",
" ! mkdir '/content/drive/My Drive/Colab Notebooks/opensource_datasets/'\n",
"except e as Exception:\n",
" pass \n",
"import os\n",
"try:\n",
" ! mkdir '/content/drive/My Drive/Colab Notebooks/opensource_datasets/wisdm'\n",
"except e as Exception:\n",
" pass \n",
"import os\n",
"os.chdir('/content/drive/My Drive/Colab Notebooks/opensource_datasets/wisdm')"
],
"execution_count": 48,
"outputs": [
{
"output_type": "stream",
"text": [
"mkdir: cannot create directory ‘/content/drive/My Drive/Colab Notebooks/opensource_datasets/’: File exists\n",
"mkdir: cannot create directory ‘/content/drive/My Drive/Colab Notebooks/opensource_datasets/wisdm’: File exists\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DXs3peqUI8QM",
"colab_type": "text"
},
"source": [
"# C) Download from website and unzip contents ##"
]
},
{
"cell_type": "code",
"metadata": {
"id": "b4hQnzCbIgvF",
"colab_type": "code",
"colab": {}
},
"source": [
"if os.path.isfile('wisdm.gz' )==False:\n",
" try:\n",
" ! wget -O wisdm.gz http://www.cis.fordham.edu/wisdm/includes/datasets/latest/WISDM_ar_latest.tar.gz\n",
" ! tar -xf wisdm.gz\n",
" print('done unzipping')\n",
" except:\n",
" pass\n",
"\n",
" ! ls"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "WuJS5m1SJvR1",
"colab_type": "text"
},
"source": [
"# D) Run the following demo code written by [Nils Ackermann](https://towardsdatascience.com/human-activity-recognition-har-tutorial-with-keras-and-core-ml-part-1-8c05e365dfa0?)# "
]
},
{
"cell_type": "code",
"metadata": {
"id": "VlO0ei_RIxxY",
"colab_type": "code",
"colab": {}
},
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"# https://towardsdatascience.com/human-activity-recognition-har-tutorial-with-keras-and-core-ml-part-1-8c05e365dfa0?\n",
"\n",
"def read_data(file_path):\n",
"\n",
" column_names = ['user-id',\n",
" 'activity',\n",
" 'timestamp',\n",
" 'x-axis',\n",
" 'y-axis',\n",
" 'z-axis']\n",
" df = pd.read_csv(file_path,\n",
" header=None,\n",
" names=column_names)\n",
" # Last column has a \";\" character which must be removed ...\n",
" df['z-axis'].replace(regex=True,\n",
" inplace=True,\n",
" to_replace=r';',\n",
" value=r'')\n",
" # ... and then this column must be transformed to float explicitly\n",
" df['z-axis'] = df['z-axis'].apply(convert_to_float)\n",
" # This is very important otherwise the model will not fit and loss\n",
" # will show up as NAN\n",
" df.dropna(axis=0, how='any', inplace=True)\n",
"\n",
" return df\n",
"\n",
"def convert_to_float(x):\n",
"\n",
" try:\n",
" return np.float(x)\n",
" except:\n",
" return np.nan\n",
" \n",
"def show_basic_dataframe_info(dataframe):\n",
"\n",
" # Shape and how many rows and columns\n",
" print('Number of columns in the dataframe: %i' % (dataframe.shape[1]))\n",
" print('Number of rows in the dataframe: %i\\n' % (dataframe.shape[0]))\n",
"\n",
"# Load data set containing all the data from csv\n",
"df = read_data('WISDM_ar_v1.1/WISDM_ar_v1.1_raw.txt')\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "kUEPILAbKbeS",
"colab_type": "text"
},
"source": [
"## Examine the data ##"
]
},
{
"cell_type": "code",
"metadata": {
"id": "sxIPjT6cKWtS",
"colab_type": "code",
"outputId": "8c853c7a-267f-4368-b031-bd438f68c557",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
}
},
"source": [
"df.head(5)"
],
"execution_count": 51,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"
\n", " | user-id | \n", "activity | \n", "timestamp | \n", "x-axis | \n", "y-axis | \n", "z-axis | \n", "
---|---|---|---|---|---|---|
0 | \n", "33 | \n", "Jogging | \n", "49105962326000 | \n", "-0.7 | \n", "12.7 | \n", "0.5 | \n", "
1 | \n", "33 | \n", "Jogging | \n", "49106062271000 | \n", "5.0 | \n", "11.3 | \n", "1.0 | \n", "
2 | \n", "33 | \n", "Jogging | \n", "49106112167000 | \n", "4.9 | \n", "10.9 | \n", "-0.1 | \n", "
3 | \n", "33 | \n", "Jogging | \n", "49106222305000 | \n", "-0.6 | \n", "18.5 | \n", "3.0 | \n", "
4 | \n", "33 | \n", "Jogging | \n", "49106332290000 | \n", "-1.2 | \n", "12.1 | \n", "7.2 | \n", "