Run command in the terminal
ipython notebook
(on Mac & Linux, on Windows should work somehow as well).
You will get a new browser window with the list of your notebooks. If there none, you can create one!
You have to import several basic packages to start doing basic stuff for linear algebra and plotting.
There are a lot of useful Python packages in the world for almost every task: always look on the web
before writing your own code!
Basic libraries:
In [3]:
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
Notice the namespaces
In [4]:
import this
MATLAB is a well-known tool for doing numerical linear algebra computations, but it has many disadvantages.
If you are an experienced MATLAB user, then you can look at Numpy for MATLAB users.
In [10]:
for i in xrange(5):
print 'Hi'
print 'Hi2'
print 'end of Hi'
In [11]:
from IPython.display import YouTubeVideo
#Long-long tutorial
YouTubeVideo('3Fp1zn5ao2M')
Out[11]:
In [14]:
import numpy as np
a = np.linspace(0, 1, 2)
print a
In [15]:
a = 5
b = -7
c = a / b
print c
c = a * 1.0 / b
print c
In [20]:
a = [5, 6, 7, 8]
b = [9, 10, 11, 12]
c = [0, 0, 0, 0]
m = len(a)
for i in xrange(m): #Notice the whitespace!!!!! Notice the : sign!!!
c[i] = a[i] + b[i]
print c
Adding two lists....
In [21]:
c = a + b
print c
Adding two numpy arrays...
In [23]:
a = np.array(a)
b = np.array(b)
c = a * b
print c
In [29]:
#Matrix-by-vector
a = np.eye(10)
v = np.ones(10)
In [31]:
def mysum(a, b):
c = np.zeros(len(a)) #All zeros
for i in xrange(len(c)):
c[i] = a[i] + b[i]
return c
And add two large arrays!
In [32]:
M = 10 ** 5
a = np.ones(M) #All ones
%timeit c = mysum(a, a) #IPython magic function for timing
%timeit c = a + a #Numpy built-in function
In [38]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
#import matplotlib
x = np.linspace(-10, 10, 1024)
plt.plot(x, np.sinc(x))
Out[38]:
In [39]:
from IPython.display import Image
i = Image(filename='TheMatrix.jpg', retina = True)
i
Out[39]:
In [43]:
n = 100
m = 50
A = np.random.randn(n, m)
x = np.ones(m)
y = np.dot(A, x) #Unfortunately, no short operator for matmul
plt.plot(y)
Out[43]:
In [2]:
from IPython.core.display import HTML
def css_styling():
styles = open("./styles/custom.css", "r").read()
return HTML(styles)
css_styling()
Out[2]:
In [ ]: