Skip to content

Instantly share code, notes, and snippets.

@kanungo
Created September 14, 2014 20:57
Show Gist options
  • Save kanungo/a702d6a5c49642387898 to your computer and use it in GitHub Desktop.
Save kanungo/a702d6a5c49642387898 to your computer and use it in GitHub Desktop.
Creating arrays in numpy
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Creating arrays \n",
"# 1. Using the array module (not numpy)\n",
"# 2. Using lists\n",
"\n",
"# Using the array module, not recommmended - but is is 'simple'\n",
"import array as Array\n",
"myonedim = Array.array('i', range(0,9))\n",
"print myonedim[0]\n",
"\n",
"# What is the difference between python.array and numpy.array\n",
"# Answer: See -- overflow.com/questions/111983/python-array-versus-numpy-array\n",
"\n",
"# From the slides\n",
"# Make an array out of a nested sequence (can be nested tuples, nested lists, or combination of them)\n",
"a = [[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]]\n",
"print a[0]\n",
"print a[0][0]\n",
"print \n",
"In [ ]:\n",
"\ufffc\n",
"# Using numpy\n",
"import numpy as np # This is how numpy is 'typically' imported\n",
" \n",
"# Using 'arange'\n",
"# This is the easiest way to create an array of sequences\n",
"arr1 = np.arange(3)\n",
"arr2 = np.arange(3.0)\n",
"arr3 = np.arange(3,10) # start,stop\n",
"arr4 = np.arange(3,10,2) # start,stop,step\n",
"print arr1\n",
"print arr2\n",
"print arr3\n",
"print arr4\n",
"In [12]:\n",
"\ufffc\n",
"# Using numpy\n",
"import numpy as np\n",
" \n",
"# Using 'linspace' and 'logspace'\n",
"linarr1 = np.linspace(2.0, 3.0, num=5)\n",
"linarr2 = np.linspace(2.0, 3.0, num=5, endpoint=False)\n",
"linarr3 = np.linspace(2.0, 3.0, num=5, retstep=True)\n",
" \n",
"print linarr1\n",
"print linarr2\n",
"print linarr3\n",
" \n",
"logarr1 = np.logspace(0,4,5)\n",
"print logarr1\n",
" \n",
"# if you want see this in non-scientific notation use 'set_printoptions'\n",
"np.set_printoptions(suppress=True)\n",
"print logarr1\n",
" # Using numpy\n",
"import numpy as np\n",
"\n",
"# Using 'linspace' and 'logspace'# Using numpy\n",
"import numpy as np\n",
"\n",
"# Using 'linspace' and 'logspace'# Using numpy\n",
"import numpy as np\n",
"\n",
"# Using 'linspace' and 'logspace'# Using numpy\n",
"import numpy as np\n",
"\n",
"# Using 'linspace' and 'logspace'a[0][0][1]\n",
"# Using numpy\n",
"import numpy as np\n",
"\n",
"# Using 'linspace' and 'logspace'# Using numpy\n",
"import numpy as np\n",
"\n",
"# Using 'linspace' and 'logspace'"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Using numpy\n",
"import numpy as np # This is how numpy is 'typically' imported\n",
"\n",
"# Using 'arange'\n",
"# This is the easiest way to create an array of sequences\n",
"arr1 = np.arange(3)\n",
"arr2 = np.arange(3.0)\n",
"arr3 = np.arange(3,10) # start,stop\n",
"arr4 = np.arange(3,10,2) # start,stop,step\n",
"print arr1\n",
"print arr2\n",
"print arr3\n",
"print arr4"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Using numpy\n",
"import numpy as np\n",
"\n",
"# Using 'linspace' and 'logspace'\n",
"linarr1 = np.linspace(2.0, 3.0, num=5)\n",
"linarr2 = np.linspace(2.0, 3.0, num=5, endpoint=False)\n",
"linarr3 = np.linspace(2.0, 3.0, num=5, retstep=True)\n",
"\n",
"print linarr1\n",
"print linarr2\n",
"print linarr3\n",
"\n",
"logarr1 = np.logspace(0,4,5)\n",
"print logarr1\n",
"\n",
"# if you want see this in non-scientific notation use 'set_printoptions'\n",
"np.set_printoptions(suppress=True)\n",
"print logarr1\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Using numpy\n",
"import numpy as np\n",
"\n",
"# Using 'mgrid'\n",
"mygrid = np.mgrid[0:2, 0:4]\n",
"grid_x, grid_y = np.mgrid[0:2, 0:4]\n",
"print 'mygrid', mygrid\n",
"print 'grid_x', grid_x\n",
"print 'grid_y', grid_y\n",
"\n",
"# Change the numbers and see what happens"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Using numpy\n",
"import numpy as np\n",
"\n",
"# Using 'random' to generate an array of random numbers\n",
"# Check this location out -- http://docs.scipy.org/doc/numpy/reference/routines.random.html\n",
"\n",
"from numpy import random as nprandom\n",
"\n",
"# uniform random numbers in [0,1]\n",
"a = nprandom.rand(5,5)\n",
"print a\n",
"\n",
"# standard normal distributed random numbers\n",
"b = nprandom.randn(5,5)\n",
"print b\n",
"\n",
"# Draw samples from a binomial distribution\n",
"# p(success) = 0.156\n",
"# N = number of trials = 20\n",
"c = nprandom.binomial(20, 0.156, [2,3])\n",
"\n",
"print c\n",
"In [ ]:\n",
"\ufffc\n",
"# NPY format\n",
"from numpy import random as nrand\n",
"import os\n",
" \n",
"a = nrand.randn(2,3)\n",
" \n",
"# Get working directory\n",
"wd = os.getcwd()\n",
"print \"Working directory is\", wd\n",
" \n",
"\n",
"In [ ]:\n",
"\ufffc\n",
"# NPY format\n",
"from numpy import random as nrand\n",
"import os\n",
" \n",
"a = nrand.randn(2,3)\n",
" \n",
"# Get working directory\n",
"wd = os.getcwd()\n",
"print \"Working directory is\", wd\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Using numpy\n",
"import numpy as np\n",
"\n",
"# a diagonal matrix\n",
"a = np.diag([1,2,3])\n",
"print a\n",
"\n",
"# diagonal with offset from main diagonal\n",
"b = np.diag([1,2,3], k=2)\n",
"\n",
"# matrix with all zeros\n",
"c = np.zeros((3,3))\n",
"print c\n",
"\n",
"# matrix with all ones\n",
"d = np.ones((3,3))\n",
"print d\n",
"In [ ]:\n",
"\ufffc\n",
"# NPY format\n",
"from numpy import random as nrand\n",
"import os\n",
" \n",
"a = nrand.randn(2,3)\n",
" \n",
"# Get working directory\n",
"wd = os.getcwd()\n",
"print \"Working directory is\", wd\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Creating a numpy matrix from a csv file\n",
"\n",
"# First create teh csv file. You can do it using a spreadsheet\n",
"# Here I will create one using Python code \n",
"import csv\n",
"mylist = [['Name', 'Age', 'Height'],\n",
" ['Tom', 23, 167],\n",
" ['Dick', 28, 172],\n",
" ['Mary', 27, 168]]\n",
"\n",
"with open(\"mycsv.csv\", \"wb\") as f:\n",
" mywriter = csv.writer(f)\n",
" mywriter.writerows(mylist)\n",
"\n",
"# Now read the file that you created into a numpy array\n",
"# Using numpy\n",
"import numpy as np \n",
"mydata = np.loadtxt('mycsv.csv', delimiter=',', dtype=str)\n",
"print mydata\n",
"print 'dtype is ', mydata.dtype\n",
"\n",
"# Note what happens when you do not force the data type to str\n",
"# You should obtain an array of tuples \n",
"mydata = np.genfromtxt('mycsv.csv', dtype=None, delimiter=',', names=True) \n",
"print mydata\n",
"print \"Dick's height is\", mydata[1][2], \"centimeters\"\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# NPY format\n",
"from numpy import random as nrand\n",
"import os\n",
"\n",
"a = nrand.randn(2,3)\n",
"\n",
"# Get working directory\n",
"wd = os.getcwd()\n",
"print \"Working directory is\", wd\n",
"\n",
"fullfilepath = wd + '/' + 'myarray'\n",
"np.save('fullfilepath', a)\n",
"\n",
"retrievepath = wd + '/' + 'myarray' + '.npy'\n",
"\n",
"b = np.load(retrievepath)\n",
"print b"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment