import numpy as np
Библиотека random
A = np.random.randint(10, size=(5, 5))
B = np.random.randint(10, size=(5, 5))
A
array([[7, 0, 8, 6, 4], [9, 9, 6, 8, 9], [2, 6, 2, 7, 7], [6, 6, 7, 7, 7], [8, 6, 8, 0, 6]])
A/2
array([[3, 0, 4, 3, 2], [4, 4, 3, 4, 4], [1, 3, 1, 3, 3], [3, 3, 3, 3, 3], [4, 3, 4, 0, 3]])
A.astype(float)/2
array([[ 3.5, 0. , 4. , 3. , 2. ], [ 4.5, 4.5, 3. , 4. , 4.5], [ 1. , 3. , 1. , 3.5, 3.5], [ 3. , 3. , 3.5, 3.5, 3.5], [ 4. , 3. , 4. , 0. , 3. ]])
A/2.0
array([[ 3.5, 0. , 4. , 3. , 2. ], [ 4.5, 4.5, 3. , 4. , 4.5], [ 1. , 3. , 1. , 3.5, 3.5], [ 3. , 3. , 3.5, 3.5, 3.5], [ 4. , 3. , 4. , 0. , 3. ]])
A*B
# np.dot(A, B) == A.dot(B)
array([[49, 0, 56, 54, 20], [ 9, 54, 18, 32, 0], [16, 36, 8, 49, 14], [ 6, 36, 56, 35, 7], [24, 18, 0, 0, 42]])
np.dot(A, B)
array([[131, 117, 129, 173, 85], [155, 192, 178, 253, 128], [ 64, 117, 96, 133, 70], [132, 159, 144, 204, 100], [144, 126, 106, 188, 98]])
A = np.random.randint(10, size=(5, 3))
B = np.random.randint(10, size=(3, 5))
A.dot(B)
array([[ 45, 64, 60, 28, 47], [ 47, 92, 58, 44, 63], [ 73, 85, 115, 24, 86], [ 48, 102, 60, 48, 72], [ 84, 132, 131, 44, 125]])
import random as rd
A_l = [[random.random() for j in range(3)] for i in range(5)]
B_l = [[random.random() for j in range(5)] for i in range(3)]
%%timeit
# код перемножения матриц на языке python с использованием list
C_l = [[[sum([A_l[i, k]*B_l[k, j]]) for k in range(3)] for j in range(5)] for i in range(5)]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-37-932d35a8481d> in <module>() ----> 1 get_ipython().run_cell_magic(u'timeit', u'', u'# \u043a\u043e\u0434 \u043f\u0435\u0440\u0435\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u044f \u043c\u0430\u0442\u0440\u0438\u0446 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 python \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c list\nC_l = [[[sum([A_l[i, k]*B_l[k, j]]) for k in range(3)] for j in range(5)] for i in range(5)]') /home/jupyter/env/local/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell) 2291 magic_arg_s = self.var_expand(line, stack_depth) 2292 with self.builtin_trap: -> 2293 result = fn(magic_arg_s, cell) 2294 return result 2295 <decorator-gen-59> in timeit(self, line, cell) /home/jupyter/env/local/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k) 191 # but it's overkill for just that one bit of state. 192 def magic_deco(arg): --> 193 call = lambda f, *a, **k: f(*a, **k) 194 195 if callable(arg): /home/jupyter/env/local/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in timeit(self, line, cell) 1035 number = 1 1036 for _ in range(1, 10): -> 1037 time_number = timer.timeit(number) 1038 worst_tuning = max(worst_tuning, time_number / number) 1039 if time_number >= 0.2: /home/jupyter/env/local/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in timeit(self, number) 131 gc.disable() 132 try: --> 133 timing = self.inner(it, self.timer) 134 finally: 135 if gcold: <magic-timeit> in inner(_it, _timer) TypeError: list indices must be integers, not tuple
%timeit A.dot(B)
The slowest run took 31.53 times longer than the fastest. This could mean that an intermediate result is being cached 1000000 loops, best of 3: 1.11 µs per loop
The slowest run took 21.80 times longer than the fastest. This could mean that an intermediate result is being cached 1000000 loops, best of 3: 1.59 µs per loop
Универсальные функции
A = np.random.randint(10, size=(2, 3))
B = np.random.randint(10, size=(4, 2, 3))
A*B
array([[[18, 63, 12], [48, 28, 5]], [[ 3, 56, 18], [72, 0, 3]], [[ 3, 35, 21], [48, 20, 2]], [[24, 42, 27], [48, 16, 1]]])
A.astype(float)/2
np.logaddexp(A[..., np.newaxis], B)
# np.logaddexp(A, B) и np.logaddexp(A[np.newaxis ,...], B) не сработает,
array([[[ 8.00033541, 9.31326169, 8.69314718, 9.31326169, 8.31326169], [ 7.01814993, 5.12692801, 9.00247569, 5.12692801, 3.12692801], [ 3.31326169, 7.00671535, 2.12692801, 2.12692801, 4.12692801]], [[ 4.01814993, 9.00671535, 8.01814993, 9.00671535, 7.04858735], [ 7.04858735, 5.31326169, 9.00671535, 5.31326169, 4.04858735], [ 7.01814993, 7.69314718, 7.00091147, 7.00091147, 7.04858735]], [[ 3.04858735, 9.00247569, 8.00671535, 9.00247569, 7.01814993], [ 7.00671535, 5.04858735, 9.00091147, 5.04858735, 2.31326169], [ 7.01814993, 7.69314718, 7.00091147, 7.00091147, 7.04858735]], [[ 4.01814993, 9.00671535, 8.01814993, 9.00671535, 7.04858735], [ 7.00671535, 5.04858735, 9.00091147, 5.04858735, 2.31326169], [ 3.04858735, 7.00091147, 0.69314718, 0.69314718, 4.01814993]], [[ 8.00033541, 9.31326169, 8.69314718, 9.31326169, 8.31326169], [ 7.31326169, 6.31326169, 9.04858735, 6.31326169, 6.00671535], [ 3.31326169, 7.00671535, 2.12692801, 2.12692801, 4.12692801]]])
View
import numpy as np
A = np.arange(3*5).reshape(3, 5)
A.shape
(3, 5)
A = np.arange(3*5)
A.resize(3, 5)
A.shape
(3, 5)
C = A.view()
C.resize(5, 3)
C[0,0] = 0
A
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])
A = np.random.randint(10, size=(5, 5))
np.linalg.inv(A)
array([[-0.08346972, 0.03436989, 0.64484452, -0.3502455 , -0.22913257], [ 0.08783415, 0.19912711, 0.05346427, -0.10856519, -0.10529187], [ 0.18930715, -0.37206765, -0.71085652, 0.4740862 , 0.36933988], [-0.16475723, 0.18548827, 0.63884343, -0.42989634, -0.12547736], [ 0.01036552, 0.05455537, -0.34151664, 0.2853246 , 0.08074195]])
np.linalg.det(A)
1832.9999999999995
np.trace(A)
16
# по-другому?
16
np.linalg.svd(A)
(array([[-0.44931509, -0.16032869, -0.86447952, 0.03366394, 0.15476614], [-0.25207412, -0.25332579, 0.17159818, 0.88901125, -0.22912386], [-0.52525296, 0.14238137, 0.09654679, -0.3247974 , -0.76747784], [-0.5735895 , 0.57569264, 0.28435477, 0.07613486, 0.50291045], [-0.3601097 , -0.74727925, 0.36476062, -0.31182666, 0.28567209]]), array([ 18.86282043, 7.85997935, 6.39475134, 3.01855238, 0.64048955]), array([[-0.49903564, -0.33111394, -0.43052832, -0.31149391, -0.59911926], [ 0.51940254, -0.32015114, -0.31740059, -0.65501751, 0.31294277], [-0.27407375, -0.67705409, -0.17009964, 0.43436161, 0.49887619], [-0.41328443, 0.56502848, -0.52097402, -0.12600288, 0.47185606], [-0.48504019, -0.10103158, 0.64308196, -0.5190153 , 0.26757681]]))
np.linalg.eigvals(A)
array([ 18.16050163+0.j , -6.47495830+0.j , 2.59667721+3.31563195j, 2.59667721-3.31563195j, -0.87889775+0.j ])
A = np.random.randint(10, size=(5, 3))
b = np.random.randint(10, size=(1,5))
np.linalg.solve(A, b)
#np.allclose(np.dot(a, x), b)
--------------------------------------------------------------------------- LinAlgError Traceback (most recent call last) <ipython-input-52-95a5f6d2c09d> in <module>() 1 A = np.random.randint(10, size=(5, 3)) 2 b = np.random.randint(10, size=(1,5)) ----> 3 np.linalg.solve(A, b) 4 5 #np.allclose(np.dot(a, x), b) /home/kvandy/anaconda3/envs/py27/lib/python2.7/site-packages/numpy/linalg/linalg.pyc in solve(a, b) 356 a, _ = _makearray(a) 357 _assertRankAtLeast2(a) --> 358 _assertNdSquareness(a) 359 b, wrap = _makearray(b) 360 t, result_t = _commonType(a, b) /home/kvandy/anaconda3/envs/py27/lib/python2.7/site-packages/numpy/linalg/linalg.pyc in _assertNdSquareness(*arrays) 210 for a in arrays: 211 if max(a.shape[-2:]) != min(a.shape[-2:]): --> 212 raise LinAlgError('Last 2 dimensions of the array must be square') 213 214 def _assertFinite(*arrays): LinAlgError: Last 2 dimensions of the array must be square
Задача 1
Проверить матрицу с вещественными элементами на симметричность, унитарность, вырожденность
Что еще
Scipy tutorial - почти вся нужная информация
Остальное - stackoverflow
Scipy/Numpy differences between similar functions
Scipy/Numpy differences between importing submodules
import matplotlib.pyplot as plt
% matplotlib inline
circle1=plt.Circle((0,0),.2,color='r',alpha=0.1)
circle2=plt.Circle((.5,.5),.2,color='b', alpha=0.5)
circle3=plt.Circle((0.8,0.8),.2,color='g', alpha=1)
fig = plt.gcf()
fig.gca().axis('equal')
fig.gca().add_artist(circle1)
fig.gca().add_artist(circle2)
fig.gca().add_artist(circle3)
<matplotlib.patches.Circle at 0x7effdf3ebd10>
x = np.linspace(0, 10, num=50)
plt.plot(x, np.sin(x))
[<matplotlib.lines.Line2D at 0x49db950>]
plt.scatter(x, np.sin(x))
<matplotlib.collections.PathCollection at 0x4c59950>
plt.plot(x, np.sin(x), '--g', x, np.cos(x), '-r')
[<matplotlib.lines.Line2D at 0x554e590>, <matplotlib.lines.Line2D at 0x554e750>]