In [1]:
print 1+1
In [2]:
countries = ['Portugal','Spain','United Kingdom']
print countries[:2]
In [3]:
a=1
while a <= 3:
print a
a += 1
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)
In [2]:
import numpy as np
np.var
np.random.normal
Out[2]:
In [6]:
import my_tools
my_tools.toString("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]
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]:
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()
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 )
In [19]:
import numpy as np
m = 3
n = 2
a = np.zeros([m,n])
print a
In [21]:
print a.shape
In [22]:
print a.dtype.name
In [23]:
a = np.array([[2,3],[3,4]])
print a
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
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]
Out[27]:
In [ ]:
import galton as galton
galton_data = galton.load( )