Created
January 30, 2017 04:59
-
-
Save bnaul/fe918dbc2bb862ee47dc7827466f6f09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np\n", | |
| "import pandas as pd\n", | |
| "%matplotlib inline\n", | |
| "import matplotlib.pyplot as plt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": [ | |
| "Loaded data from cached archive.\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n", | |
| "/home/bnaul/miniconda3/envs/deep/lib/python3.5/site-packages/cesium/features/lomb_scargle.py:237: RuntimeWarning: divide by zero encountered in true_divide\n", | |
| " out_dict['gcv_weight'] = (1 - 3. / ntime) / Tr\n" | |
| ] | |
| }, | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "CPU times: user 5.51 s, sys: 1.32 s, total: 6.82 s\n", | |
| "Wall time: 4min 49s\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%time\n", | |
| "from cesium.datasets import fetch_asas_training\n", | |
| "from cesium.featurize import featurize_time_series\n", | |
| "from cesium.features.graphs import CADENCE_FEATS, GENERAL_FEATS, LOMB_SCARGLE_FEATS\n", | |
| "\n", | |
| "data = fetch_asas_training()\n", | |
| "fset = featurize_time_series(data['times'], data['measurements'], data['errors'], targets=data['classes'],\n", | |
| " features_to_use=GENERAL_FEATS + LOMB_SCARGLE_FEATS, labels=list(data['metadata'].index))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "classes_to_drop = [label for label, count in pd.Series(fset.target.values).value_counts().iteritems() if count < 10]\n", | |
| "inds = [i for i, t in enumerate(fset.target.values) if t not in classes_to_drop]\n", | |
| "fset = fset.isel(name=inds)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from sklearn.model_selection import train_test_split\n", | |
| "\n", | |
| "np.random.seed(0)\n", | |
| "train, test = train_test_split(np.arange(len(fset.target)), train_size=0.8, test_size=0.2, stratify=fset.target.values)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from cesium.build_model import build_model_from_featureset\n", | |
| "from sklearn.ensemble import RandomForestClassifier\n", | |
| "\n", | |
| "asas_model = RandomForestClassifier(n_estimators=500)\n", | |
| "asas_model = build_model_from_featureset(fset, asas_model)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "******************** ASAS train score: 1.0\n", | |
| "******************** ASAS test score: 1.0\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from cesium.predict import model_predictions\n", | |
| "\n", | |
| "pred = model_predictions(fset, asas_model, return_probs=False)\n", | |
| "print('*' * 20, 'ASAS train score:', np.mean(pred.prediction.values[train] == fset.target.values[train]))\n", | |
| "print('*' * 20, 'ASAS test score:', np.mean(pred.prediction.values[test] == fset.target.values[test]))" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "deep", | |
| "language": "python", | |
| "name": "deep" | |
| }, | |
| "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": 1 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment