layout | tags | comments | ||
---|---|---|---|---|
post |
|
true |
This tutorial is practice assignment from the course http://cs231n.github.io/
import sys
print(sys.version)
3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)
[GCC 7.3.0]
num = [22,44,66,11,45,56]
print(num)
[22, 44, 66, 11, 45, 56]
print(num[:3])
[22, 44, 66]
print(num[1:3])
[44, 66]
print(num[2:])
[66, 11, 45, 56]
First we create a list of numbers from 1-9 using range and then get the square as list , Note : list comprehension is applied on list and returns a list
[ x*x for x in list(range(1,10))]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[ x*x for x in list(range(1,10)) if x%2==0]
[4, 16, 36, 64]
import numpy as np
# Creating a rank 1 array
array1 = np.array([1,2,3,4,5,6])
print(type(array1)) ; print(array1.shape) ; print(array1[1])
<class 'numpy.ndarray'>
(6,)
2
# Creating a rank 2 array
array2 = np.array([[1,2,3] , [6,7,8]])
print(type(array2)) ; print(array2.shape) , print(array2[0])
<class 'numpy.ndarray'>
(2, 3)
[1 2 3]
np.zeros((3,3))
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
np.ones((2,2))
array([[1., 1.],
[1., 1.]])
np.full((3,3),2)
array([[2, 2, 2],
[2, 2, 2],
[2, 2, 2]])
np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
rand_np = np.random.random((6,4))
print(rand_np)
[[0.05080747 0.42425544 0.57862652 0.32096002]
[0.07626656 0.2110246 0.05251395 0.75752798]
[0.63088127 0.67261688 0.92382051 0.34417774]
[0.21743804 0.15609258 0.07513012 0.78520746]
[0.23994478 0.32181935 0.3711074 0.67729622]
[0.68537627 0.49100271 0.81613017 0.89442773]]
x = np.array([[1,2],[3,4]] ,dtype = np.float64)
x
array([[1., 2.],
[3., 4.]])
y = np.array([[5,6],[7,8]] , dtype = np.float64)
x + y
array([[ 6., 8.],
[10., 12.]])
v = np.array([9,10]) ; v
array([ 9, 10])
w = np.array([11, 12]) ; w
array([11, 12])
np.add(x,y)
array([[ 6., 8.],
[10., 12.]])
# Matrix / Vector product , produce rank 1 array
print(x.dot(v))
# or
print(np.dot(x,v))
[29\. 67.]
[29\. 67.]
#Matrix/matix product
print(x.dot(y))
# or
print(np.dot(x,y))
[[19\. 22.]
[43\. 50.]]
[[19\. 22.]
[43\. 50.]]
If the dimensions of two arrays are dissimilar, element-to-element operations are not possible. However, operations on arrays of non-similar shapes is still possible in NumPy, because of the broadcasting capability. The smaller array is broadcast to the size of the larger array so that they have compatible shapes.
a = np.array([[1,2] , [5,6], [3,5]])
b = np.array([30,40])
print(a.shape) ; print(b.shape)
(3, 2)
(2,)
np.dot(a,b)
array([110, 390, 290])
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 4 * np.pi, 0.1)
y = np.sin(x)
# Plot the points using matplotlib
plt.plot(x, y)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Plot the points using matplotlib
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
plt.show()