Lsmlx 2017

Day 0


In [1]:
print 1+1


2

In [2]:
countries = ['Portugal','Spain','United Kingdom']
print countries[:2]


['Portugal', 'Spain']

In [3]:
a=1
while a <= 3:
    print a
    a += 1


1
2
3

In [7]:
def greet(hour):
    if hour < 0 or hour > 23:
        raise ValueError("Invalid input value.")
    if hour < 12:
        print 'Good morning!'
    elif hour >= 12 and hour < 20:
        print 'Good afternoon!'
    else:
        print 'Good evening!'

In [11]:
greet(-1.4)


Invalid hour: it should be between 0 and 24.

In [2]:
import numpy as np
np.var
np.random.normal


Out[2]:
<function normal>

In [6]:
import my_tools
my_tools.toString("This works!")


This works!

In [9]:
# This will import the numpy library
# and give it the np abbreviation
import numpy as np
# This will import the plotting library
import matplotlib.pyplot as plt
# Linspace will return 1000 points,
# evenly spaced between -4 and +4
X = np.linspace(-4, 4, 1000)
# Y[i] = X[i]**2
Y = X**2
# Plot using a red line ('r')
plt.plot(X, Y, 'r')
# arange returns integers ranging from -4 to +4
# (the upper argument is excluded!)
Ints = np.arange(-4,5)
# We plot these on top of the previous plot
# using blue circles (o means a little circle)
plt.plot(Ints, Ints**2, 'bo')
# You may notice that the plot is tight around the line
# Set the display limits to see better
plt.xlim(-5,5)
plt.ylim(-1,17)
plt.show( )



In [12]:
import numpy as np
A = np.array([
    [1,2,3],
    [2,3,4],
    [4,5,6]])
print A[0,:] # This is [1,2,3]
print A[0] # This is [1,2,3] as well
print A[:,0] # this is [1,2,4]
print A[1:,0] # This is [ 2, 4 ]. Why?
# Because it is the same as A[1:n,0] where n is the size of the array.

B = np.array([[9,8,7],[6,5,4]])
print B[0,0]


[1 2 3]
[1 2 3]
[1 2 4]
[2 4]
9

In [13]:
import matplotlib.pyplot as plt
import numpy as np
X = np.linspace(0, 4 * np.pi, 1000)
C = np.cos(X)
S = np.sin(X)
plt.plot(X, C)
plt.plot(X, S)


Out[13]:
[<matplotlib.lines.Line2D at 0x107bfe7d0>]

In [14]:
import numpy as np
A = np.arange(100)
# These two lines do exactly the same thing
print np.mean(A)
print A.mean()
C = np.cos(A)
print C.ptp()


49.5
49.5
1.99996082639

In [15]:
import numpy as np
np.ptp?

In [18]:
import math
import numpy as np

np.sum(  )



def f( x ):
    return pow( x, 2 )

print f( 2 )


4

In [19]:
import numpy as np
m = 3
n = 2
a = np.zeros([m,n])
print a


[[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]

In [21]:
print a.shape


(3, 2)

In [22]:
print a.dtype.name


float64

In [23]:
a = np.array([[2,3],[3,4]])
print a


[[2 3]
 [3 4]]

In [26]:
a = np.array([[2,3],[3,4]])
b = np.array([[1,1],[1,1]])
a_dim1, a_dim2 = a.shape
b_dim1, b_dim2 = b.shape
c = np.zeros([a_dim1,b_dim2])
for i in xrange(a_dim1):
    for j in xrange(b_dim2):
        for k in xrange(a_dim2):
            c[i,j] += a[i,k]*b[k,j]
print c
d = np.dot(a,b)
print d


[[ 5.  5.]
 [ 7.  7.]]
[[5 5]
 [7 7]]

In [27]:
I = np.eye(2)
x = np.array([2.3, 3.4])
print I
print np.dot(I,x)
[[ 1., 0.],
[ 0., 1.]]
[2.3, 3.4]


[[ 1.  0.]
 [ 0.  1.]]
[ 2.3  3.4]
Out[27]:
[2.3, 3.4]

In [ ]:
import galton as galton
galton_data = galton.load( )