Numpy CheatSheet

Notes:

The bolded bits are arbitrary. Shape means the number of items inside an array.

  • Importing module: import numpy as np

Basics

  • Creating a one-dimensional array: dataset = np.array([0,1,2,3])
  • Creating a two-dimensional array: dataset = np.array([[0,1,2],[3,4,5]])

Non-empty arrays

  • Creating an ordered sequence of 10 numbers inside an array: dataset = np.arange(10)
  • Creating an ordered sequence of evenly spaced numbers inside a predetermined interval: dataset = np.linspace(startnumber, endnumber, numberofmidpoints)
  • Creating an array of ones with a certain shape and dimensions: dataset = np.ones((2,3))
  • Creating an array of zeros with a certain shape and dimensions: dataset = np.zeros((2,3))
  • Creating an array with a user-given number of random values: dataset = np.random.rand(4)

Metadata

  • Checking the number of dimensions of an array: dataset.ndim
  • Checking the number of items inside an array:  dataset.shape

Statistics

Measures of central tendency

Starting with the following array: dataset = np.array([0,1,2,3,4])

  • Mean: print dataset.mean()
  • Median: print np.median(dataset)
  • SD: print np.std(dataset)

I/O

  • Opening a textfile: dataset = np.loadtxt(‘insert here path relative to the location of the program’) Note: Make sure the first row has an asterisk
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s