In [1]:
import numpy as np
In [2]:
x = [2, 3, 4, 6]
y = np.array(x)
In [3]:
print type(x), x
print type(y), y
In [4]:
print x[1:3]
In [5]:
print y[1:3]
In [6]:
print x[[0, 2]]
In [7]:
print y[[0, 2]]
In [8]:
print y[y>3]
In [9]:
print x * 5
In [10]:
print y * 5
In [11]:
print x ** 2
In [12]:
print y ** 2
In [13]:
matrix = [[1, 2, 4], [3, 1, 0]]
nd_array = np.array(matrix)
In [14]:
print matrix[1][2]
In [15]:
print nd_array[1, 2]
In [16]:
print np.random.rand()
In [17]:
print np.random.randn()
In [18]:
print np.random.randn(4)
In [19]:
print np.random.randn(4, 5)
In [20]:
print np.arange(0, 8, 0.1)
In [21]:
print range(0, 8, 0.1)
In [23]:
%timeit np.arange(0, 10000)
%timeit range(0, 10000)
In [24]:
from scipy import optimize
In [25]:
def f(x):
return (x[0] - 3.2) ** 2 + (x[1] - 0.1) ** 2 + 3
print f([3.2, 0.1])
In [26]:
x_min = optimize.minimize(f, [5, 5])
print x_min
In [27]:
print x_min.x
In [28]:
from scipy import linalg
In [29]:
a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])
b = np.array([2, 4, -1])
x = linalg.solve(a, b)
print x
In [30]:
print np.dot(a, x)
In [31]:
X = np.random.randn(4, 3)
U, D, V = linalg.svd(X)
print U.shape, D.shape, V.shape
print type(U), type(D), type(V)
In [33]:
%matplotlib inline
In [34]:
from matplotlib import pylab as plt
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.show()
In [35]:
x = np.arange(-10, 10, 0.1)
y = x ** 3
plt.plot(x, y)
plt.show()
In [36]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
In [37]:
x = np.arange(0, 10, 2)
y = np.exp(-x/3.0) + np.random.randn(len(x)) * 0.05
print x[:5]
print y[:5]
In [38]:
f = interpolate.interp1d(x, y, kind='quadratic')
xnew = np.arange(0, 8, 0.1)
ynew = f(xnew)
In [39]:
plt.plot(x, y, 'o', xnew, ynew, '-')
plt.show()
In [ ]: