Первое знакомство с NumPy, SciPy и Matplotlib

Numpy


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


<type 'list'> [2, 3, 4, 6]
<type 'numpy.ndarray'> [2 3 4 6]

In [4]:
print x[1:3]


[3, 4]

In [5]:
print y[1:3]


[3 4]

In [6]:
print x[[0, 2]]


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-1703eef47ef6> in <module>()
----> 1 print x[[0, 2]]

TypeError: list indices must be integers, not list

In [7]:
print y[[0, 2]]


[2 4]

In [8]:
print y[y>3]


[4 6]

In [9]:
print x * 5


[2, 3, 4, 6, 2, 3, 4, 6, 2, 3, 4, 6, 2, 3, 4, 6, 2, 3, 4, 6]

In [10]:
print y * 5


[10 15 20 30]

In [11]:
print x ** 2


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-8f3433247060> in <module>()
----> 1 print x ** 2

TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

In [12]:
print y ** 2


[ 4  9 16 36]

In [13]:
matrix = [[1, 2, 4], [3, 1, 0]]
nd_array = np.array(matrix)

In [14]:
print matrix[1][2]


0

In [15]:
print nd_array[1, 2]


0

In [16]:
print np.random.rand()


0.670717102263

In [17]:
print np.random.randn()


-0.665706808438

In [18]:
print np.random.randn(4)


[ 0.01156574  0.01818502  0.66564365  0.91744438]

In [19]:
print np.random.randn(4, 5)


[[ 1.32288172 -1.13837024  0.40151387  1.74625101 -0.61821887]
 [-0.65268278  0.29979205  0.49014459  0.94569491  0.52527019]
 [ 0.51502133  2.29836596 -1.08681062  0.71351264  0.28765788]
 [-0.70784283  0.34124652 -1.80889795 -1.15800519 -2.03843127]]

In [20]:
print np.arange(0, 8, 0.1)


[ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.   1.1  1.2  1.3  1.4
  1.5  1.6  1.7  1.8  1.9  2.   2.1  2.2  2.3  2.4  2.5  2.6  2.7  2.8  2.9
  3.   3.1  3.2  3.3  3.4  3.5  3.6  3.7  3.8  3.9  4.   4.1  4.2  4.3  4.4
  4.5  4.6  4.7  4.8  4.9  5.   5.1  5.2  5.3  5.4  5.5  5.6  5.7  5.8  5.9
  6.   6.1  6.2  6.3  6.4  6.5  6.6  6.7  6.8  6.9  7.   7.1  7.2  7.3  7.4
  7.5  7.6  7.7  7.8  7.9]

In [21]:
print range(0, 8, 0.1)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-8fe6ca95c7b3> in <module>()
----> 1 print range(0, 8, 0.1)

TypeError: range() integer step argument expected, got float.

In [23]:
%timeit np.arange(0, 10000)
%timeit range(0, 10000)


The slowest run took 19.55 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 10 µs per loop
10000 loops, best of 3: 98.5 µs per loop

SciPy


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])


3.0

In [26]:
x_min = optimize.minimize(f, [5, 5])
print x_min


      fun: 3.0000000000000058
 hess_inv: array([[1, 0],
       [0, 1]])
      jac: array([ -1.49011612e-07,   5.96046448e-08])
  message: 'Optimization terminated successfully.'
     nfev: 12
      nit: 1
     njev: 3
   status: 0
  success: True
        x: array([ 3.19999993,  0.10000002])

In [27]:
print x_min.x


[ 3.19999993  0.10000002]

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


[ 2. -2.  9.]

In [30]:
print np.dot(a, x)


[ 2.  4. -1.]

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)


(4, 4) (3,) (3, 3)
<type 'numpy.ndarray'> <type 'numpy.ndarray'> <type 'numpy.ndarray'>

Matplotlib


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]


[0 2 4 6 8]
[ 0.90728735  0.5265296   0.26898089  0.15414147  0.12880009]

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 [ ]: