NumPy Video Lectures
Link http://showmedo.com/videotutorials/video?name=10370020&fromSeriesID=1037
Video1 – Introducing NumPy
- functions covered are arange, meshgrid,zeros,ones, eyes, flatten,
- flatten - order determines the dimension along which it is flattened
- repeat function
- tile function
Video2 – Using dtypes
<= import numpy as np print np.fromfunction(lambda i,j : i+j , (4,4)) print np.fromfunction(lambda i,j : abs(i-j)<1 , (4,4)) print np.fromfunction(lambda i,j : abs(i-j)<1 , (4,4)).astype(int) dt = np.dtype(‘f4,c8’) print array([(1,1)] , dtype=dt)
Video3 – Loading Data
from numpy import *
x = loadtxt(r"C:\Cauldron\garage\DeliberatePractice\Python\NumPyLectures\navs.csv",delimiter=",",skiprows=1,usecols =(2,3),dtype=dtype(‘f4’,‘f4’))
print x[1:5,0:1]
x = genfromtxt(r"C:\Cauldron\garage\DeliberatePractice\Python\NumPyLectures\navs.csv",delimiter=",",skiprows=1,usecols =(1,2,3),dtype=dtype(‘f4’,‘f4’,‘f4’))
print x[1:5,0:1]
print isnan(x[1:10,0])
[[ 106.55950165]
[ 107.45999908]
[ 106.5786972 ]
[ 106.48329926]]
[[ 529.21069336]
[ nan]
[ 527.71520996]
[ 525.87158203]]
[False True False False False False False False False]
- You can use genfromtext to load text that contain missing values
Video4 - Slicing and dicing
from numpy import *
x = arange(50).reshape(5,10)
print x[where(x>5)]
print logical_and(x >5 , x 5 , x <20)]
print sqrt(x)
[ 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]
[[False False False False False False True True True True]
[ True True True True True True True True True True]
[False False False False False False False False False False]
[False False False False False False False False False False]
[False False False False False False False False False False]]
[ 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[[ 0. 1. 1.41421356 1.73205081 2. 2.23606798
2.44948974 2.64575131 2.82842712 3. ]
[ 3.16227766 3.31662479 3.46410162 3.60555128 3.74165739 3.87298335
- 4.12310563 4.24264069 4.35889894]
[ 4.47213595 4.58257569 4.69041576 4.79583152 4.89897949 5.
5.09901951 5.19615242 5.29150262 5.38516481]
[ 5.47722558 5.56776436 5.65685425 5.74456265 5.83095189 5.91607978
- 6.08276253 6.164414 6.244998 ]
[ 6.32455532 6.40312424 6.4807407 6.55743852 6.63324958 6.70820393
6.78232998 6.8556546 6.92820323 7. ]]
-
You can use genfromtext to load text that contain missing values
-
Unary functions numpy.sqrt, numpy.cos , etc are designed to be used on arrays
Video5 - Linear Algebra
from numpy import *
x = arange(50).reshape(5,10)
print matrix(x).T*matrix(x)
print linalg.det(matrix(x).T*matrix(x))
print linalg.eig(matrix(x).T*matrix(x))
print linalg.eigvals(matrix(x).T*matrix(x))
[[3000 3100 3200 3300 3400 3500 3600 3700 3800 3900]
[3100 3205 3310 3415 3520 3625 3730 3835 3940 4045]
[3200 3310 3420 3530 3640 3750 3860 3970 4080 4190]
[3300 3415 3530 3645 3760 3875 3990 4105 4220 4335]
[3400 3520 3640 3760 3880 4000 4120 4240 4360 4480]
[3500 3625 3750 3875 4000 4125 4250 4375 4500 4625]
[3600 3730 3860 3990 4120 4250 4380 4510 4640 4770]
[3700 3835 3970 4105 4240 4375 4510 4645 4780 4915]
[3800 3940 4080 4220 4360 4500 4640 4780 4920 5060]
[3900 4045 4190 4335 4480 4625 4770 4915 5060 5205]]
4.35587270447e-94
(array([ 4.03227003e+04 +0.00000000e+00j,
-1.47530628e-12 +0.00000000e+00j,
1.02299696e+02 +0.00000000e+00j,
1.86546894e-12 +0.00000000e+00j,
-4.99759293e-13 +5.37521382e-13j,
-4.99759293e-13 -5.37521382e-13j,
-3.45582910e-13 +3.20560565e-13j,
-3.45582910e-13 -3.20560565e-13j,
-2.50392269e-13 +0.00000000e+00j, 4.13002980e-13 +0.00000000e+00j\]), matrix(\[\[ -2.71496515e-01+0.j , -8.09039835e-01+0.j ,
5.21290886e-01+0.j , -1.27860533e-01+0.j ,
9.46244843e-02-0.01579802j, 9.46244843e-02+0.01579802j,
-7.54436211e-02-0.08322526j, -7.54436211e-02+0.08322526j,
-5.61064662e-03+0.j , 4.69050382e-02+0.j \],
\[ -2.81165278e-01+0.j , 3.59573260e-01+0.j ,
4.11619891e-01+0.j , -4.64933814e-01+0.j ,
-5.66373677e-02+0.05072311j, -5.66373677e-02-0.05072311j,
1.35932901e-02+0.01090524j, 1.35932901e-02-0.01090524j,
7.50382870e-03+0.j , 1.53202762e-02+0.j \],
\[ -2.90834042e-01+0.j , 2.92153274e-01+0.j ,
3.01948896e-01+0.j , 3.48350555e-01+0.j ,
7.78773449e-02+0.15820573j, 7.78773449e-02-0.15820573j,
-4.41180098e-01+0.j , -4.41180098e-01+0.j ,
-7.20823008e-01+0.j , -8.06180700e-02+0.j \],
\[ -3.00502805e-01+0.j , 2.24733287e-01+0.j ,
1.92277901e-01+0.j , -1.47676847e-01+0.j ,
1.90176135e-02-0.27365343j, 1.90176135e-02+0.27365343j,
3.10645335e-01+0.25972686j, 3.10645335e-01-0.25972686j,
4.52683125e-01+0.j , 5.57992484e-01+0.j \],
\[ -3.10171569e-01+0.j , 1.57313301e-01+0.j ,
8.26069064e-02+0.j , 1.50983420e-01+0.j ,
-2.42105636e-01-0.24398526j, -2.42105636e-01+0.24398526j,
3.10284046e-01+0.12742365j, 3.10284046e-01-0.12742365j,
2.60741829e-01+0.j , -6.51527196e-01+0.j \],
\[ -3.19840332e-01+0.j , 8.98933150e-02+0.j ,
-2.70640886e-02+0.j , 6.81194584e-01+0.j ,
8.26063415e-02+0.2667188j , 8.26063415e-02-0.2667188j ,
3.50693821e-01-0.21370131j, 3.50693821e-01+0.21370131j,
3.36861644e-01+0.j , 2.68463694e-02+0.j \],
\[ -3.29509096e-01+0.j , 2.24733287e-02+0.j ,
-1.36735084e-01+0.j , 1.98274668e-02+0.j ,
-4.03006845e-01+0.01355545j, -4.03006845e-01-0.01355545j,
-1.17471397e-01-0.251166j , -1.17471397e-01+0.251166j ,
7.52306555e-02+0.j , 7.71483375e-02+0.j \],
\[ -3.39177859e-01+0.j , -4.49466575e-02+0.j ,
-2.46406078e-01+0.j , 4.57301880e-02+0.j ,
4.94304845e-01+0.j , 4.94304845e-01+0.j ,
-2.64233377e-01-0.1001103j , -2.64233377e-01+0.1001103j ,
-1.50168842e-01+0.j , -3.70095134e-01+0.j \],
\[ -3.48846623e-01+0.j , -1.12366644e-01+0.j ,
-3.56077073e-01+0.j , -3.12815701e-01+0.j ,
4.27451474e-02+0.38326256j, 4.27451474e-02-0.38326256j,
-2.78679629e-01+0.27482986j, -2.78679629e-01-0.27482986j,
-2.56382508e-01+0.j , 3.30659787e-01+0.j \],
\[ -3.58515386e-01+0.j , -1.79786630e-01+0.j ,
-4.65748068e-01+0.j , -1.92799318e-01+0.j ,
-1.09425928e-01-0.33902893j, -1.09425928e-01+0.33902893j,
1.91791629e-01-0.02468273j, 1.91791629e-01+0.02468273j,
-3.60776951e-05+0.j , 4.73681068e-02+0.j \]\]))
[ 4.03227003e+04 +0.00000000e+00j -1.46772213e-12 +0.00000000e+00j
1.02299696e+02 +0.00000000e+00j 1.86546894e-12 +0.00000000e+00j
-4.99759293e-13 +5.37521382e-13j -4.99759293e-13 -5.37521382e-13j
-3.45582910e-13 +3.20560565e-13j -3.45582910e-13 -3.20560565e-13j
-2.50392269e-13 +0.00000000e+00j 4.13002980e-13 +0.00000000e+00j]
- linalg module has aton of matrix related functions
Video6 - Masked Aarrays
- They maintain the shape of the array but suppress the elements
Video7 - Broad Casting
- Recycling rule in Python
Video8 - Using Numpy temporary variables
- There are times when you can cut the temporary variable creation by using the same output matrix for input
Video9 - Functional Programming in NumPy
-
apply_along_axis, apply_along_axes fuctions are covered
-
vectorize function is also covered
Video10 - Polynomiam Functions
x = linspace(0,3,30)
print x
y = x+1+x**3
y+= random.randn(*y.shape)
print polyfit(x,y,3)
print poly1d(polyfit(x,y,3))
p = poly1d(polyfit(x,y,3))
print p
print p(x)
print p*p+3
print roots(p)
print polyder(p)
print polyint(p)
[ 0. 0.10344828 0.20689655 0.31034483 0.4137931 0.51724138
0.62068966 0.72413793 0.82758621 0.93103448 1.03448276 1.13793103
1.24137931 1.34482759 1.44827586 1.55172414 1.65517241 1.75862069
1.86206897 1.96551724 2.06896552 2.17241379 2.27586207 2.37931034
2.48275862 2.5862069 2.68965517 2.79310345 2.89655172 3. ]
[ 1.11946512 -0.57078042 1.72519246 0.8239446 ]
3 2
1.119 x - 0.5708 x + 1.725 x + 0.8239
3 2
1.119 x - 0.5708 x + 1.725 x + 0.8239
[ 0.8239446 0.99754386 1.16636253 1.33783646 1.51940153
1.71849359 1.94254853 2.19900219 2.49529046 2.8388492
3.23711427 3.69752154 4.22750687 4.83450614 5.52595521
6.30928995 7.19194622 8.18135989 9.28496682 10.51020289
11.86450396 13.3553059 14.99004456 16.77615583 18.72107556
20.83223963 23.11708389 25.58304422 28.23755648 31.08805654]
6 5 4 3 2
1.253 x - 1.278 x + 4.188 x - 0.1247 x + 2.036 x + 2.843 x + 3.679
[ 0.44954229+1.29959068j 0.44954229-1.29959068j -0.38921570+0.j ]
2
3.358 x - 1.142 x + 1.725
4 3 2
0.2799 x - 0.1903 x + 0.8626 x + 0.8239 x
-
There are a lot of functions that support polynomial stuff in NumPy
-
The above code shows the various stuff I played around with, atleast the video played around with and I replicated the examples
Video11 - Understanding Strides
from numpy.lib import stride_tricks
a=arange(10)
x =stride_tricks.as_strided(a , shape=(8,3), strides=(4,4))
print x
print x.strides
y = arange(24).reshape(8,3)
print y.strides
x[0,2]=9999
print x
[[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]
[5 6 7]
[6 7 8]
[7 8 9]]
(4, 4)
(12, 4)
[[ 0 1 9999]
[ 1 9999 3]
[9999 3 4]
[ 3 4 5]
[ 4 5 6]
[ 5 6 7]
[ 6 7 8]
[ 7 8 9]]
-
strides tells the bytes to move to cover each dimension
-
Creates overlapped arrays and the values point to the same memory location