# Python Numpy library

Python has a fantastic library called numpy to work with multi-dimensional arrays. It is easy to get started, just import it as a package and create a sample array of 100 elements

>>> import numpy as np

>>> x = np.arange(100)

>>> x

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,

34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,

51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,

68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,

85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])

**You can think of this list as an Excel file with 1 column and 100 rows.**

This array has a shape. It is a simple one dimensional array of 100 elements.

>>> x.shape

(100,)

You can change the shape of this array with the** reshape method of Numpy**

Let’s turn this one column, 100 rows thing into a 10 by 10 matrix

>>> y = x.reshape(10,10)

array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],

[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],

[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],

[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],

[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],

[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],

[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],

[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],

[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],

[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])

>>> y.shape

(10, 10)

You see now the shape of the data went from 100,0 to 10 by 10

If you want to turn this into an two dimensional matrix / grid of 20 by 5 you can pass that numbers as parameters to reshape

Python intro course here http://www.thecodinghands.com/thecodinghands-data.html

>>> x.reshape(20,5)

array([[ 0, 1, 2, 3, 4],

[ 5, 6, 7, 8, 9],

[10, 11, 12, 13, 14],

[15, 16, 17, 18, 19],

[20, 21, 22, 23, 24],

[25, 26, 27, 28, 29],

[30, 31, 32, 33, 34],

[35, 36, 37, 38, 39],

[40, 41, 42, 43, 44],

[45, 46, 47, 48, 49],

[50, 51, 52, 53, 54],

[55, 56, 57, 58, 59],

[60, 61, 62, 63, 64],

[65, 66, 67, 68, 69],

[70, 71, 72, 73, 74],

[75, 76, 77, 78, 79],

[80, 81, 82, 83, 84],

[85, 86, 87, 88, 89],

[90, 91, 92, 93, 94],

[95, 96, 97, 98, 99]])

Lastly, note that reshape fails with a `ValueError`

when there is not enough elements to fill in the requested shape

>>> x.reshape(20,6)

Traceback (most recent call last):

File “<stdin>”, line 1, in <module>

ValueError: cannot reshape array of size 100 into shape (20,6)

**Linear space**

Linspace will give you evenly spaced numbers. You can specify how many numbers you want and start and stop range. The easiest and most obvious one is to ask for 100 numbers and the range to be 1–100, this will make each number 1 apart from the other and give you the same result as doing a arange(100)

np.linspace(start=1, stop=100, num=100)

array([ 1., 2., 3., 4., 5., 6., 7., 8., 9.,

10., 11., 12., 13., 14., 15., 16., 17., 18.,

19., 20., 21., 22., 23., 24., 25., 26., 27.,

28., 29., 30., 31., 32., 33., 34., 35., 36.,

37., 38., 39., 40., 41., 42., 43., 44., 45.,

46., 47., 48., 49., 50., 51., 52., 53., 54.,

55., 56., 57., 58., 59., 60., 61., 62., 63.,

64., 65., 66., 67., 68., 69., 70., 71., 72.,

73., 74., 75., 76., 77., 78., 79., 80., 81.,

82., 83., 84., 85., 86., 87., 88., 89., 90.,

91., 92., 93., 94., 95., 96., 97., 98., 99., 100.])

It gets more interesting when you need numbers from 1–30 that are evenly spaced out

>>> np.linspace(start=1,stop=100,num=30)

array([ 1. , 4.4137931 , 7.82758621, 11.24137931,

14.65517241, 18.06896552, 21.48275862, 24.89655172,

28.31034483, 31.72413793, 35.13793103, 38.55172414,

41.96551724, 45.37931034, 48.79310345, 52.20689655,

55.62068966, 59.03448276, 62.44827586, 65.86206897,

69.27586207, 72.68965517, 76.10344828, 79.51724138,

82.93103448, 86.34482759, 89.75862069, 93.17241379,

96.5862069 , 100. ])

### Scatter plots

Simple line plot

plt.scatter(np.arange(100,step=0.2),np.arange(100, step=0.2))

plt.show()

Simple square function

x = np.arange(start=0,stop=100,step=0.2)

sq = lambda x: pow(x,2)

y = [pow(a,2) for a in x]

plt.scatter(x,y)

plt.show()

Random numbers

x = np.arange(start=0,stop=100,step=0.2)

len(x)

y = np.random.random(500)

plt.scatter(x,y,edgecolors=[‘red’,’blue’])

plt.show()

Trig functions

Recall that sin2(x) + cos2(x) is always 1

x = np.arange(start=0,stop=100,step=0.2)

fun = lambda x: pow(np.sin(x),2) + pow(np.cos(x),2)

y = [fun(a) for a in x]

plt.scatter(x,y,edgecolors=[‘red’,’blue’])

plt.show()

Remember that sin of an angle always goes from -1 to 1, so let us generate a list of 990 elements and plot them against their sin. (I was going to originally generate a 1000 numbers but the start=1 chopped off first ten elements)