In [7]:
%pylab inline
In [2]:
from IPython.display import Latex
In [3]:
import numpy as np
arr = np.arange(1e7)
In [4]:
arr
Out[4]:
In [5]:
larr = arr.tolist()
print type(larr)
In [6]:
larr[:10]
Out[6]:
In [7]:
larr[:2] * 3 # Do not support array multiplication (broadcasting).
Out[7]:
In [8]:
arr * 3 # ndarray support broadcasting.
Out[8]:
In [9]:
arr = np.zeros((3, 3, 3))
arr
Out[9]:
In [10]:
print "\n".join(np.matrix.__doc__.split("\n")[:7])
print
mat = np.matrix(np.zeros((3, 2)))
print mat
In [11]:
# Converting a list to ndarray (using np.array: array-like object --> numpy array)
alist = [1, 2, 3]
arr = np.array(alist)
arr
Out[11]:
In [12]:
# Instanciating an numpy array using np.ndarray. (shape --> numpy array)
array1 = np.ndarray(1000)
print array1.shape
array2 = np.ndarray((1000, 1))
print array2.shape
In [13]:
array1[:10]
Out[13]:
In [14]:
print np.ndarray.__doc__
In [15]:
np.linspace(0, 10, 10000) # zero to 10 with 10000 steps.
Out[15]:
In [16]:
cube = np.zeros((5, 5, 5))
cube.dtype
Out[16]:
In [17]:
cube2 = cube.astype(int) + 1
cube2[0:2]
Out[17]:
In [18]:
cube2.dtype
Out[18]:
In [19]:
int_arr = np.zeros((1, 10), dtype=int)
In [20]:
int_arr.tolist()
Out[20]:
In [21]:
int_arr2 = np.zeros(10, dtype=int)
In [22]:
int_arr2.tolist()
Out[22]:
In [23]:
arr1d = np.zeros((1000, 1))
In [24]:
arr3d = arr1d.reshape((10, 10, 10))
In [25]:
arr3d[9]
Out[25]:
In [26]:
arr = arr3d.ravel() # Flat a multidimensional array to 1-D array.
arr.shape
Out[26]:
In [27]:
recarr = np.zeros((2,), dtype = ('i4, f4, a10'))
toadd = [(1, 2, 'Hello'), (2, 3., 'World')]
In [28]:
recarr[:] = toadd
In [29]:
recarr
Out[29]:
In [30]:
recarr.dtype.names = ("Int", 'Float', "String")
recarr
Out[30]:
In [31]:
recarr["Int"]
Out[31]:
In [32]:
recarr["String"]
Out[32]:
In [33]:
arr = np.arange(100).reshape((10, 10))
In [34]:
print arr[1, 9]
print arr[2, 3]
In [35]:
arr[1, :]
Out[35]:
In [36]:
arr[2:4, 0:3]
Out[36]:
In [37]:
ind = np.where(arr >= 30)
ind
Out[37]:
In [38]:
arr[ind].reshape((np.unique(ind[0]).shape[0], np.unique(ind[1]).shape[0]))
Out[38]:
In [39]:
np.unique(ind[0]).shape[0]
Out[39]:
In [40]:
img1 = np.zeros((20, 20)) + 3
img1[4:-4, 4:-4] = 6
img1[7:-7, 7:-7] = 9
In [41]:
img1
Out[41]:
In [42]:
imshow(img1)
Out[42]:
In [43]:
compound_ind = (img1 > 3) & (img1 < 7)
In [44]:
import copy
In [45]:
%time img2 = copy.deepcopy(img1)
In [46]:
%time img3 = np.copy(img1)
In [47]:
img2 = np.copy(img1)
In [48]:
img2[compound_ind] = 0
imshow(img2)
Out[48]:
In [49]:
img2
Out[49]:
In [50]:
ind3 = img1 == 9
ind4 = compound_ind & ind3
In [51]:
img3 = np.copy(img1)
In [52]:
img3[ind3] = 1
In [53]:
imshow(img3)
Out[53]:
In [11]:
import numpy.random as rand
In [12]:
print rand.randn.__doc__
In [32]:
a = rand.randn(100)
In [33]:
ind = a > 0.2
b = a[ind]
In [34]:
b = b**2 - 10
In [35]:
a[ind] = b
imshow(a.reshape((10, 10)))
Out[35]:
In [60]:
%%file test.txt
XR21 32.789 1
XR22 33.091 2
In [61]:
table = np.loadtxt('test.txt', dtype = {'names': ('ID', 'Result', 'Type'), 'formats':('S4', 'f4', 'i2')})
In [62]:
table
Out[62]:
In [63]:
data = np.empty((1000, 1000))
In [64]:
np.save("test.npy", data) # Save as binary data for a np.array
In [65]:
np.savez("test.npz", data) # Save as compressed binary data.
In [66]:
!tree .
In [67]:
restored_data = np.load("test.npy")
In [68]:
restored_data
Out[68]:
In [69]:
arr = rand.randint(0, 100, 10)
In [70]:
arr.sort() # sort method will modify the original array
In [71]:
arr
Out[71]:
In [72]:
arr2 = np.random.rand(100)
In [73]:
arr2[5] = np.nan
In [74]:
print (np.max(arr2))
In [75]:
print (np.nanmax(arr2))
In [75]:
In [76]:
array = np.random.rand(10000).reshape((100, 100))
In [77]:
rind = np.random.randint(2, size=(100, 100)) == 1
In [78]:
array[rind] = np.nan
In [79]:
array
Out[79]:
In [80]:
not_nan = ~np.isnan(array)
In [81]:
not_nan
Out[81]:
In [82]:
print np.std(array[not_nan])
In [83]:
A = np.matrix([[3, 6, -5], [1, -3, 2], [5, -1, 4]])
In [84]:
B = np.matrix([[12], [-2], [10]])
In [85]:
X = A**(-1) * B
In [86]:
X
Out[86]:
In [87]:
A * X
Out[87]:
Other way to solve above linear system with numpy (but more efficient).
In [88]:
A = np.array([[3, 6, -5], [1, -3, 2], [5, -1, 4]])
B = np.array([[12], [-2], [10]])
In [89]:
X = np.linalg.inv(A).dot(B)
In [90]:
X
Out[90]:
In [90]:
In [91]:
a = np.random.rand(10)
print a
np.random.permutation(a) # premutation will not affect a
Out[91]:
In [92]:
a
Out[92]:
In [93]:
np.random.shuffle(a) # shuffle will affect a.
In [94]:
a
Out[94]:
In [95]:
def temp_and_rain(lat, min_temp = 10, max_temp = 10, points = 1000):
lat = np.abs(lat)
temp = np.random.normal(scale = 5, size = lat.shape) + lat/2
temp = np.abs(temp)
tobe_ornot = np.random.randint(0, high = 2)
print tobe_ornot
if tobe_ornot:
rain = np.random.normal(scale = 3, size=lat.shape) + temp * 2
else:
rain = np.random.uniform(high = 100, size = lat.shape)
return temp, rain
In [96]:
lat = np.random.uniform(low = -90, high = 90, size = 100)
temp, rain = temp_and_rain(lat)
In [97]:
result = np.corrcoef(temp, rain)
In [98]:
result
Out[98]:
In [99]:
temp, rain = temp_and_rain(lat)
In [100]:
result = np.corrcoef(temp, rain)
result
Out[100]:
In [101]:
print np.histogram2d.__doc__
In [102]:
import matplotlib.pyplot as plt
# Generate 2 by 1000000 array by normal dist and assign rows to x, y respectively.
x, y = np.random.randn(2, 1000000)
img, xedges, yedges = np.histogram2d(x, y, bins = (20, 20))
# For imshow. (Define the extend of x and y axis)
extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]
fig = plt.figure(figsize = (10, 5))
ax1 = fig.add_subplot(121) # 1 by 2 plot's subplot 1
ax2 = fig.add_subplot(122) # 1 by 2 plot's subplot 2
yarg = plt.cm.gist_yarg # plt.cm: color maps module
ax1.imshow(img, extent = extent, interpolation='nearest', cmap=yarg, origin='lower left')
ax2.hexbin(x, y, gridsize=20, cmap=yarg)
ax1.set_title("Rectangular binning")
ax2.set_title("Hexagonal binning")
ax1.set_xlim(-4, 4)
ax1.set_ylim(-4, 4)
ax2.set_xlim(-4, 4)
ax2.set_ylim(-4, 4)
ax1.set_aspect("equal")
ax2.set_aspect("equal")
In [104]:
fig.savefig("binning.png")
In [104]:
In [105]:
from numpy.polynomial import polynomial as P
In [106]:
x = np.linspace(0, 8 * np.pi, 50)
y1 = np.cos(x)
y2 = np.sin(x - np.pi)
In [107]:
print P.polyroots.__doc__
In [108]:
coeff, stats = P.polyfit(x, y1, 20, full = True)
roots = np.real(P.polyroots(coeff))
fit = P.Polynomial(coeff)
yf1 = fit(x)
In [109]:
fig = plt.figure(figsize = (10, 5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.plot(x, y1)
ax2.plot(x, yf1)
ax1.set_title("Original Data")
ax2.set_title("Fitting Date")
Out[109]:
In [110]:
print P.polyder.__doc__
In [111]:
## Differentiating
new_coeff = P.polyder(coeff)
dfit = P.Polynomial(new_coeff)
yf2 = dfit(x)
In [129]:
fig = plt.figure(figsize=(8, 6))
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.set_title("Data and fitting")
ax2.set_title("Differentiation")
ax1.set_xlim(0, x.max())
ax2.set_xlim(0, x.max())
ax1.set_ylim(-1.25, 1.25)
ax2.set_ylim(-1.25, 1.25)
ax1.plot(x, yf1, label="Fitted", color="gray")
ax1.scatter(x, y1, label="Original", edgecolor="none", facecolor = "red")
ax1.scatter(roots, np.zeros(roots.shape), label="Roots", edgecolor="black", facecolor="none", s=50)
ax1.legend(loc=0)
ax2.plot(x, yf2, label="Differentiated", color="gray")
ax2.scatter(x, y2, label="Original", edgecolor = "none", facecolor="red")
ax2.legend(loc=1)
Out[129]:
In [113]:
fig.savefig("polynomials.png")
In [ ]: