In [1]:
from numpy import *
In [2]:
zeros((4,7))
Out[2]:
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]])
In [3]:
zeros((4))
Out[3]:
array([ 0.,  0.,  0.,  0.])
In [4]:
ones(8)
Out[4]:
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
In [5]:
eye(5)
Out[5]:
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  1.]])
In [6]:
random.rand(4,5)
Out[6]:
array([[ 0.01245441,  0.85266079,  0.04220636,  0.38366717,  0.12095561],
       [ 0.39472618,  0.29488045,  0.00442378,  0.2974854 ,  0.26396768],
       [ 0.44844616,  0.90513814,  0.86726263,  0.91901837,  0.32807573],
       [ 0.17332605,  0.48797795,  0.63572981,  0.06148893,  0.11070731]])
In [8]:
a = linspace(10,20,11)
a
Out[8]:
array([ 10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.])
In [9]:
type(a)
Out[9]:
numpy.ndarray
In [10]:
a.dtype
Out[10]:
dtype('float64')
In [11]:
d = ones(5,dtype=int)
In [12]:
d
Out[12]:
array([1, 1, 1, 1, 1])
In [13]:
a = random.randint(0,10,(7,4))
a
Out[13]:
array([[1, 8, 6, 4],
       [4, 6, 7, 6],
       [7, 3, 6, 2],
       [4, 9, 8, 8],
       [8, 8, 3, 6],
       [9, 7, 9, 4],
       [8, 5, 7, 4]])
In [14]:
a[1]
Out[14]:
array([4, 6, 7, 6])
In [15]:
a[5,3]
Out[15]:
4
In [16]:
a[5,3]=19
a
Out[16]:
array([[ 1,  8,  6,  4],
       [ 4,  6,  7,  6],
       [ 7,  3,  6,  2],
       [ 4,  9,  8,  8],
       [ 8,  8,  3,  6],
       [ 9,  7,  9, 19],
       [ 8,  5,  7,  4]])
In [21]:
#random.seed(46473)
random.randint(0,10,(7,4))
Out[21]:
array([[0, 7, 3, 7],
       [1, 4, 7, 4],
       [5, 1, 5, 0],
       [8, 0, 0, 7],
       [7, 1, 3, 2],
       [6, 0, 5, 9],
       [9, 1, 1, 8]])
In [22]:
a
Out[22]:
array([[ 1,  8,  6,  4],
       [ 4,  6,  7,  6],
       [ 7,  3,  6,  2],
       [ 4,  9,  8,  8],
       [ 8,  8,  3,  6],
       [ 9,  7,  9, 19],
       [ 8,  5,  7,  4]])
In [23]:
a[::2,:]
Out[23]:
array([[1, 8, 6, 4],
       [7, 3, 6, 2],
       [8, 8, 3, 6],
       [8, 5, 7, 4]])
In [24]:
a[::2,:-1]
Out[24]:
array([[1, 8, 6],
       [7, 3, 6],
       [8, 8, 3],
       [8, 5, 7]])
In [25]:
a[::2,:-1] = 0
In [26]:
a
Out[26]:
array([[ 0,  0,  0,  4],
       [ 4,  6,  7,  6],
       [ 0,  0,  0,  2],
       [ 4,  9,  8,  8],
       [ 0,  0,  0,  6],
       [ 9,  7,  9, 19],
       [ 0,  0,  0,  4]])
In [30]:
nr = 40
nc = 40
mask = zeros((nr,nc,3),dtype=uint8)
mask[:10,:10,:] = 1
In [32]:
import matplotlib.pyplot as plt
%matplotlib inline
In [38]:
plt.imshow(255*mask,interpolation='none');
In [33]:
a = ones((4,5))
a
Out[33]:
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])
In [34]:
5*a
Out[34]:
array([[ 5.,  5.,  5.,  5.,  5.],
       [ 5.,  5.,  5.,  5.,  5.],
       [ 5.,  5.,  5.,  5.,  5.],
       [ 5.,  5.,  5.,  5.,  5.]])
In [41]:
nr = 40
nc = 40
mask = zeros((nr,nc,3),dtype=uint8)
mask[::2,::2,:] = 1
mask[1::2,1::2,:] = 1
plt.imshow(255*mask,interpolation='none');
In [48]:
#a = zeros((nr,nc,3),dtype=uint8)
v = zeros_like(mask)
# let's make some stripes
v[:,:10,] = [150,0,150]
v[:,12:15,:] = [255,0,0]
h = v.transpose(1,0,2)
plt.imshow(mask*v + (1-mask)*h,interpolation='none');
In [49]:
from numpy import *
#matplotlib inline
from scipy.misc import imsave, imshow
import os
r = 2.
h = 500
w = 499
x = linspace(-r, r,w)
y = linspace( r,-r,h)

X,Y = meshgrid(x,y)
Z = X + 1.0j*Y

def newton(z):
	return 2.*z/3. + 1./(3.*z**2)
root1 = 1.
root2 = -.5 + 1.j*sqrt(3.)/2.
root3 = -.5 - 1.j*sqrt(3.)/2.

a = zeros((h,w,3))#,dtype=uint8)
red   = a[:,:,0]
green = a[:,:,1]
blue  = a[:,:,2]

for i in range(10):
	Z = newton(Z)
	red  [ abs(Z-root1)<.1 ] += 1
	green[ abs(Z-root2)<.1 ] += 1
	blue [ abs(Z-root3)<.1 ] += 1

#a *= 10
imsave('complex_newton.py.png',a)
#imshow(a)
os.system('eog complex_newton.py.png&')
Out[49]:
0
In [50]:
from scipy.io import wavfile
In [51]:
w = wavfile.read('black_holes_merge.wav')
In [52]:
len(w)
Out[52]:
2
In [53]:
w
Out[53]:
(44100, array([[ 1, -1],
        [ 1, -1],
        [ 0,  0],
        ..., 
        [-2,  2],
        [-2,  2],
        [-2,  2]], dtype=int16))
In [54]:
samplerate,data = wavfile.read('black_holes_merge.wav')
In [55]:
samplerate
Out[55]:
44100
In [57]:
type(data)
Out[57]:
numpy.ndarray
In [58]:
data.shape
Out[58]:
(1384704, 2)
In [67]:
plt.plot(data[70000:120000,0],'r',alpha=0.5,lw=3)
plt.plot(data[70000:120000,1],'b',alpha=0.5);
In [70]:
x = linspace(0,10,500)
plt.plot(x,sin(x));