{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<img src=\"https://news.illinois.edu/files/6367/543635/116641.jpg\" alt=\"University of Illinois\" style=\"width: 200px;\"/>\n", "\n", "## Lists ##\n", "By Richard Sowers\n", "* <r-sowers@illinois.edu>\n", "* <https://publish.illinois.edu/r-sowers/>\n", "\n", "Copyright 2020 University of Illinois Board of Trustees. All Rights Reserved.\n", "Licensed under the MIT license" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook is part of the 2020 \"ISE Freshman\" effort" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### imports ###" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy\n", "import scipy.stats" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### constants ###" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "SEED=0\n", "N_samples=1000\n", "bias=0.2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "bias=0.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### code starts here ###" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "numpy.random.seed(SEED)\n", "X = scipy.stats.bernoulli.rvs(bias, size=N_samples)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the average number of one's ###" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 1 0 1]\n" ] } ], "source": [ "print(X[:20])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "average is: 0.203\n" ] } ], "source": [ "print(\"average is:\",numpy.mean(X))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Let's print the number of 1's in N elements starting from n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 0 0 1 1 0 0 0 0 1]\n" ] } ], "source": [ "N_min=4\n", "N=10\n", "XX=X[N_min:N_min+N]\n", "print(XX) #note fencepost issue" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sum is: 3\n" ] } ], "source": [ "print(\"sum is:\",numpy.sum(XX)) #actually starts at 5th element" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sum is: 3\n" ] } ], "source": [ "print(\"sum is:\",numpy.sum(XX==1)) #actually starts at 5th element" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "XX[0]=0; XX[1]=0; XX[2]=0; XX[3]=1; XX[4]=1; XX[5]=0; XX[6]=0; XX[7]=0; XX[8]=0; XX[9]=1; \n", "\n", "sum is: 3\n" ] } ], "source": [ "sum=0\n", "for nn in range(len(XX)):\n", " print(\"XX[{0:d}]={1:d}\".format(nn,XX[nn]), end=\"; \")\n", " sum+=XX[nn] #entries are either 1 or zero\n", "print(\"\\n\")\n", "print(\"sum is: \",sum)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "XX[4]=0; XX[5]=0; XX[6]=0; XX[7]=1; XX[8]=1; XX[9]=0; XX[10]=0; XX[11]=0; XX[12]=0; XX[13]=1; \n", "\n", "sum is: 3\n" ] } ], "source": [ "sum=0\n", "for nn in range(N_min,N_min+N):\n", " print(\"XX[{0:d}]={1:d}\".format(nn,X[nn]), end=\"; \")\n", " sum+=1 if (X[nn]==1) else 0 #conditional (ternary) operator\n", "print(\"\\n\")\n", "print(\"sum is: \",sum)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "XX[4]=0; XX[5]=0; XX[6]=0; XX[7]=1; XX[8]=1; XX[9]=0; XX[10]=0; XX[11]=0; XX[12]=0; XX[13]=1; \n", "\n", "sum is: 3\n" ] } ], "source": [ "sum=0\n", "for nn in range(N_min,N_min+N):\n", " print(\"XX[{0:d}]={1:d}\".format(nn,X[nn]), end=\"; \")\n", " sum+=(X[nn]==1) #booleans are 1 or 0\n", "print(\"\\n\")\n", "print(\"sum is: \",sum)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sum is: 3\n" ] } ], "source": [ "sum=0\n", "for x in XX:#sequence through list\n", " sum+=x\n", "print(\"sum is: \",sum)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sum is: 3\n" ] } ], "source": [ "print(\"sum is: \",numpy.sum([x for x in XX])) #list comprehension to make new list" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sum is: 3\n" ] } ], "source": [ "print(\"sum is: \",numpy.sum([1 for x in XX if x==1])) #list comprehension with conditional inclusion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Let's find out how many times 1 is followed by 1" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of repeated one's is: 42\n" ] } ], "source": [ "#direct array references, explicitly exclude last element\n", "sum=0\n", "for n in range(len(X)-1):\n", " sum+=(1 if ((X[n]==1) and (X[n+1]==1)) else 0)\n", "print(\"number of repeated one's is: \",sum)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of repeated one's is: 42\n" ] } ], "source": [ "#direct array references, exclude last element with try/except\n", "sum=0\n", "for n in range(len(X)):\n", " try:\n", " sum+=((X[n]==1) and (X[n+1]==1))\n", " except:\n", " print(\"error at n=\",n)\n", "print(\"number of repeated one's is: \",sum)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "error at n= 999\n", "number of repeated one's is: 42\n" ] } ], "source": [ "#direct array references, exclude last element with try/except. Note partial evaluation of and clauses\n", "sum=0\n", "for n in range(len(X)):\n", " try:\n", " sum+=((X[n+1]==1) and (X[n]==1))\n", " except:\n", " print(\"error at n=\",n)\n", "print(\"number of repeated one's is: \",sum)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of repeated one's is: 42\n" ] } ], "source": [ "#direct array references, exclude last element with try/except. 1/0's are True/False\n", "sum=0\n", "for n in range(len(X)):\n", " try:\n", " sum+=(X[n] and X[n+1])\n", " except:\n", " print(\"error at n=\",n)\n", "print(\"number of repeated one's is: \",sum)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of repeated one's is: 42\n" ] } ], "source": [ "#enumerate gives index and element\n", "sum=0\n", "for n,c in enumerate(X):\n", " try:\n", " sum+= (c and X[n+1])\n", " except:\n", " print(\"error at n=\",n)\n", "print(\"number of repeated one's is: \",sum)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of repeated one's is: 42\n" ] } ], "source": [ "#construct pairwise list of elements and ensuing elements. Note that lists are 1 element shorter due shift\n", "sum=0\n", "for (xx,xxnext) in zip(X[:-1],X[1:]):#note list definitions\n", " sum+=(xx and xxnext)\n", "print(\"number of repeated one's is: \",sum)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of repeated one's is: 42\n" ] } ], "source": [ "#inline list comprehension\n", "print(\"number of repeated one's is: \",numpy.sum([xx and xxnext for (xx,xxnext) in zip(X[:-1],X[1:])])) " ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of repeated one's is: 42\n" ] } ], "source": [ "#inline list comprehension\n", "print(\"number of repeated one's is: \",numpy.sum([(xx+xxnext)==2 for (xx,xxnext) in zip(X[:-1],X[1:])]))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of repeated one's is: 42\n" ] } ], "source": [ "#bitwise and of lists\n", "print(\"number of repeated one's is: \",numpy.sum(X[:-1] & X[1:]))" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of repeated one's is: 42\n" ] } ], "source": [ "#elementwise list combinations\n", "print(\"number of repeated one's is: \",numpy.sum((X[:-1]+X[1:])==2))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of repeated one's is: 42\n" ] } ], "source": [ "#logical and\n", "print(\"number of repeated one's is: \",numpy.sum(numpy.logical_and(X[:-1]==1,X[1:]==1)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Let's find out how many times 1 is followed by 0" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of 1-0's is: 161\n" ] } ], "source": [ "#elementwise and, using 1/0=True/False\n", "print(\"number of 1-0's is: \",numpy.sum(X[:-1] & (1-X[1:])))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of 1-0's is: 161\n" ] } ], "source": [ "#elementwise and, using 1/0=True/False\n", "print(\"number of 1-0's is: \",numpy.sum([xx and (1-xxnext) for (xx,xxnext) in zip(X[:-1],X[1:])]))" ] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 1 }