Skip to content

Instantly share code, notes, and snippets.

@jfpuget
Created March 21, 2016 18:54

Revisions

  1. jfpuget created this gist Mar 21, 2016.
    198 changes: 198 additions & 0 deletions Std.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,198 @@
    {
    "cells": [
    {
    "cell_type": "code",
    "execution_count": 1,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "import math\n",
    "import numpy as np\n",
    "import random"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 2,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def mean(lst):\n",
    " return sum(lst) / len(lst)\n",
    "\n",
    "\n",
    "def standard_deviation(lst):\n",
    " m = mean(lst)\n",
    " variance = sum([(value - m) ** 2 for value in lst])\n",
    " return math.sqrt(variance / len(lst))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 3,
    "metadata": {
    "collapsed": false
    },
    "outputs": [],
    "source": [
    "lst = [random.random() for i in range(0,25)]"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 4,
    "metadata": {
    "collapsed": false
    },
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "The slowest run took 5.50 times longer than the fastest. This could mean that an intermediate result is being cached \n",
    "100000 loops, best of 3: 10 µs per loop\n"
    ]
    }
    ],
    "source": [
    "%timeit standard_deviation(lst)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 5,
    "metadata": {
    "collapsed": false
    },
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "The slowest run took 4.05 times longer than the fastest. This could mean that an intermediate result is being cached \n",
    "10000 loops, best of 3: 37.8 µs per loop\n"
    ]
    }
    ],
    "source": [
    "%timeit np.std(lst)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 6,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "def np_array(lst):\n",
    " return np.array(lst)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 7,
    "metadata": {
    "collapsed": false
    },
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "The slowest run took 9.61 times longer than the fastest. This could mean that an intermediate result is being cached \n",
    "100000 loops, best of 3: 2.09 µs per loop\n"
    ]
    }
    ],
    "source": [
    "%timeit np_array(lst)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 8,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": [
    "lsta = np_array(lst)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 9,
    "metadata": {
    "collapsed": false
    },
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "10000 loops, best of 3: 30.4 µs per loop\n"
    ]
    }
    ],
    "source": [
    "%timeit np.std(lsta)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 10,
    "metadata": {
    "collapsed": false
    },
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "The slowest run took 4.02 times longer than the fastest. This could mean that an intermediate result is being cached \n",
    "10000 loops, best of 3: 35.6 µs per loop\n"
    ]
    }
    ],
    "source": [
    "%timeit np.std(np_array(lst))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {
    "collapsed": true
    },
    "outputs": [],
    "source": []
    }
    ],
    "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.5.1"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 0
    }