Difference makes the DIFFERENCE
N = 100000000
%%time
list_ = list(range(N))
for i in range(N):
list_[i] = list_[i] * list_[i]
Finetune the for loop in the above code
%%time
list_ = list(range(N))
list_ = [item * item for item in list_]
further., finetune the above list
%%time
list_ = list(range(N))
list_ = map(lambda x: x * x, list_)
if, in case we want to add all the numbers in the list... !!
%%time
list_ = list(range(N))
list_sum = 0
for item in list_:
list_sum += item
finetune with in-built function for addition
%%time
list_ = list(range(N))
list_sum = sum(list_)
import numpy as np
%%time
arr = np.arange(N)
arr = arr * arr
finetune the above code with in-built function
%%time
arr = np.arange(N)
arr_sum = np.sum(arr)
the above code and respective exection time conveys that numpy arrays are significantly faster than normal lists
arr = np.arange(6)
print(arr)
print(arr.dtype)
print(arr.size, arr.dtype)
print(arr.itemsize)
print(arr.ndim)
arr2d = np.array([
[1,2,3],
[4,5,6]
])
arr2d
print(arr2d.dtype, arr2d.ndim)
arr2d.size
arr2d.ndim
arr2d.shape
arr2d = np.array([
[1,2,3],
[4,5,6]
])
arr3d = np.array([
[[1,2,3],
[4,5,6]],
[[7,8,9],
[10,11,12]]
])
arr3d
arr3d = np.array([
[
[1,2,3],
[4,5,6]
],
[
[7,8,9],
[10,11,12]
]
])
print(arr3d)
arr3d
print(arr3d.size)
print(arr3d.itemsize)
print(arr3d.ndim)
arr3d.shape
np.ones((3,4)) # a two dimensional array - observe the number of square braces over here
np.ones((2,3,4)) # a three dimensional array - observe the number of square braces over here
np.ones((2,3,4,5)) # a four dimensional array - observe the number of square braces over here
arr5d = np.ones((2,3,4,5,6)) # a four dimensional array - observe the number of square braces over here
print(arr5d)
1729 * np.ones((2,3,4))
np.zeros((2,3,4))
np.random.randn(2,3)
np.random.randn(3,3)
np.random.rand(2,3)
np.random.randint(10, 80, (3,4))
np.arange(2, 10, 7) # - with step size of 7
np.arange(2, 30, 2) # with step size of 2
np.arange(2, 40, 12) # with step size of 12
np.linspace(7, 70, 10) # linspace or linear space generates 10 numbers between 2 and 4
np.linspace(1,2, 20) # twenty numbers between 1 and 2 inclusive of 2
np.array([2.4,3,'and'])
np.array(['and', '2.4', '5.654'])
np.array(['and', 2.3, 'dan'])
np.array(['and', 23, 'dan'])
np.array(['and', 2, 'dan'])
np.array(['and', 'dan', 'nad', 'adn'])
print(arr3d)
arr3d = np.array([
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
],
[
[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]
]
])
print(arr3d.shape)
arr3d[0,0,0]
print(arr3d)
print(arr3d[:,:,1]) # z, row, col
print(arr3d[:,1,:])
arr3d.shape
print(arr3d)
print(arr3d[:,:, 0:2])
import numpy as np
arr1 = np.zeros((3,4))
arr2 = np.ones((3,4))
arr1 + arr2
arr1 = np.random.rand(3, 4)
arr2 = np.random.randn(3, 4)
arr1
arr2
arr1 + arr2
arr1 - arr2
arr1*arr2
np.exp(arr1)
np.log(arr1)
arr1
np.log(np.exp(arr1))
np.sqrt(arr1)
arr_inv = 1/arr1
print(arr_inv)
15*arr1
ndim = 2
npoints = 1000000
points = np.random.rand(npoints, ndim)
dfo = np.zeros((npoints, 1))
outside_points = 0
print(points[0:4])
%%time
dfo = np.zeros((npoints, 1))
outside_points = 0
for i in range(npoints):
for j in range(ndim):
dfo[i] += points[i, j] ** 2
dfo[i] = np.sqrt(dfo[i])
if dfo[i] > 1:
outside_points += 1
print("Fraction of points outside circle: ", outside_points / npoints)
1 - 3.14/4
point = 5
dim = 2
apoint = np.random.rand(point, dim)
apoint
apoint * apoint
print(np.random.randint(1, 10, 5))
abc = np.arange(20).reshape(4,5)
print(abc)
print( abc * abc)
dfo = np.sum(abc)
print(dfo)
dfo = np.sum(abc, axis=1)
print(dfo)
print(np.sum(abc, axis= 0))
print(sum(abc * abc))
print(abc * abc)
print(sum(abc))
print(abc)
print(sum(abc, 1))
print(np.sum(abc, 1))
print(abc)
print(np.sum(abc, axis=1))
print(np.sqrt(np.sum(abc, 1)))
print(np.sqrt(10))
%%time
sp_points = points * points
print(sp_points)
print(points)
sp_points = points * points
print(sp_points)
points = np.random.rand(npoints, ndim)
print(points)
sp_points = points * points
print(sp_points)
sp_points = 0
sp_points = points * points
dfo = np.sqrt(np.sum(sp_points, axis=1))
outside_points = np.sum(dfo > 1)
print(outside_points/npoints)
import numpy as np
arr1 = np.arange(6)
arr1
arr1 = arr1.reshape((3, 2))
print(arr1)
arr2 = np.arange(6).reshape( 3, 2)
print(arr2)
np.reshape?
arr1 + arr2
arr2[0].reshape(1,2)
arr2[1].reshape(1,2)
arr1
arr1 + arr2[1]