In [1]:
import numpy as np
ax = np.array([1,2,3,4,5])
print(ax,id(ax),len(ax))
In [2]:
x=['1','2','3']
xi = np.array(x,'int')
xf = np.array(x,'float')
xs = np.array(x,'str')
print(xi,xf,xs,sep='\n')
In [3]:
x = np.array([13,24,21.2,17.6,21.7],'float')
print(x.sum(),x.mean(),x.std(),sep='\n')
In [4]:
x=[[0,1,2,3,4,5],[10,11,12,13,14,15],[20,21,22,23,24,25]]
ax=np.array(x,float)
print(ax)
In [5]:
ax[1,3] #indexing
Out[5]:
In [6]:
ax[1:3,2:4]
#ax[:,2:]
Out[6]:
In [7]:
print(ax.shape)
ax.reshape(9,2)
#ax.reshape(10,3)
Out[7]:
In [25]:
ax = np.arange(10)
print(ax)
ay = np.array([np.arange(2,10,2),np.arange(10)])
print(ay)
In [9]:
ax = np.ones(10)
print(ax)
In [10]:
ax = np.arange(10)**2
print(ax)
In [11]:
np.identity(10)
Out[11]:
In [12]:
ax = np.arange(10)
ay = np.array([ax,ax])
#Scalar multiplication
ay*2
Out[12]:
In [13]:
np.dot(ay,ay.reshape(10,2)) #Dot product
Out[13]:
In [15]:
n=10
ax = np.array([np.arange(n)**2,np.arange(n)**3])
ay = ax.transpose()
print(ax)
print(ay)
np.dot(ax,ay)
Out[15]:
In [16]:
def dotproduct(n):
ax = np.array([np.arange(n)**2,np.arange(n)**3])
ay = ax.transpose()
import datetime
start = datetime.datetime.now()
np.dot(ax,ay)
end = datetime.datetime.now()
return end-start
dotproduct(10)
Out[16]:
In [30]:
def dot_product_lists(n):
x = [x**2 for x in range(n)]
y = [x**3 for x in range(n)]
ax = [x,y]
ay = [list(i) for i in zip(*ax)]
import datetime
start = datetime.datetime.now()
[[sum(a*b for a,b in zip(X_row,Y_col)) for Y_col in zip(*ay)] for X_row in ax]
end = datetime.datetime.now()
return end-start
dot_product_lists(10000)
Out[30]:
In [31]:
for n in [10,100,1000,10000]:
numpy_result = dotproduct(n)
list_result = dot_product_lists(n)
print(n,numpy_result,list_result,sep='\t')
In [32]:
x=[[0,1,2,3,4,5],[10,11,12,13,14,15],[20,21,22,23,24,25]]
ax=np.array(x,float)
np.where(ax%2==0,1,0)
Out[32]:
In [20]:
#linalg, a linear algebra module
#functions dealing with polynomials, differentials, etc
In [21]:
import scipy
scipy.nanmean(x)
Out[21]:
In [ ]:
np.random.normal(size=10)
#np.random.normal(size=(100,100))
#np.random.exponential()
#np.random.exponential(1.0,size=(6,3))
#np.random.randint(-10,10,size=(9,9))
In [49]:
Z = np.random.random(10)
In [50]:
print(Z)
In [53]:
Z = Z.view().sort(axis=0)
In [54]:
print(Z)
In [57]:
a = np.arange(12)
b = a
c = a.view()
d = a.copy()
print(id(a),id(b),id(c),id(d))
In [58]:
print(a,b,c,d,sep='\n')
In [59]:
d[0]=1
In [60]:
print(b)
In [77]:
Z = np.random.random(10)
#Z.sort()
#print(Z)
Z = np.sort(Z)
print(Z)
In [123]:
Z = np.random.uniform(0,1,10)
print(Z)
q = 0.5
print(np.abs(Z-q))
print(np.min(np.abs(Z-q)))
print(np.argmin(np.abs(Z-q)))
#m = np.argmin(np.abs(Z-q))
m = Z.flat[np.abs(Z-q).argmin()]
print(m)
In [ ]: