0% found this document useful (0 votes)
12 views20 pages

1 3

The document contains various examples of lists, matrices, and arrays in Python, showcasing their structures and operations. It includes code snippets for creating and manipulating these data structures, as well as using libraries like NumPy for mathematical operations. Additionally, it discusses lambda functions and their applications in Python programming.

Uploaded by

123109015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views20 pages

1 3

The document contains various examples of lists, matrices, and arrays in Python, showcasing their structures and operations. It includes code snippets for creating and manipulating these data structures, as well as using libraries like NumPy for mathematical operations. Additionally, it discusses lambda functions and their applications in Python programming.

Uploaded by

123109015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

#Simple List

list1 = [1,5,6,2]

#List of Lists
list2 = [ [1,5,6,2], [3,2,1,3] ]

#List of Lists of Lists


List3 = [ [ [1,5,6,2],[3,2,1,3] ],
[ [5,2,1,2],[6,4,8,4] ],
[ [2,8,5,3],[1,1,9,4] ] ]

#Non-homologous List
#It may not be considered as an Array
list4 = [ [4,2,3], [5,1] ]
>>> X = [4, 5] #LIST
>>> print(type(X))
<class 'list'>
>>> Y = {4: 'four', 5: 'five'} #Dictionary
>>> print(type(Y))
<class 'dict'>

>>> numericalList = [4,5, 6]


>>> stringList = ['four', 'five', 'six']
>>> X = zip()
>>> XList = list(X)
>>> print(XList)
Z=[ ]
X=[2, 4, 6, 8]
for i in X:
... Z.append(X)
print(X,Z)

for i in range(len(X)):
... Z.append(X[i]+10)
print(X,Z)
import matplotlib.pyplot as plt
Z=[ ]
X=[0,1,2,4,5,6]
Y=[1,4,8,16,25,49]
fig,ax=plt.subplots( )
ax.plot(X,Y)
plt.show( )
for i in range(len(X)):
... Z.append(X[i]+10)
print(X,Z)
fig,ax=plt.subplots()
ax.plot(X,Z,"ob")
>>> X= zip(numericalList, stringList)
>>> XList = list(X)
>>> print(XList)
[(4, 'four'), (5, 'five'), (6, 'six')]
>>>

L = [1.5, "Hi", "Python", 2]


print (L[3:]);
print (L[0:2]);
print (L);
print (L + L);
print (L * 3);
import datetime
print(datetime.datetime.now())
2025-01-14 21:00:07.955515
import time
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Current Time =", current_time)
Current Time = 21:02:05
import random
import datetime
seed_value = int(datetime.datetime.now().timestamp())
print(seed_value)
random.seed(seed_value)# Seed the random number generator
random_number = random.randint(1, 100)# Generate a random number
print(random_number)
random.seed()
print(random.random())
#Matrix or a Rectangular Array
matrix1 = = [ [4,2],[5,1],[8,2] ]
# Shape is 3 rows and 2 columns

#3-dimenasional array
matrix2 = [ [ [1,5,6,2], [3,2,1,3] ], [ [5,2,1,2], [6,4,8,4] ], [ [2,8,5,3], [1,1,9,4] ] ]
# Shape is 3 x 2 x 4

#Note: A tensor object is an object that can be represented as an array.


inputs = [1, 2, 3, 2.5] #Input vector or the Feature Set
# shape = (4,)

# A batch of input
batch = [[1,5,6,2], [3,2,1,3],
[5,2,1,2], [6,4,8,4]
[2,8,5,3], [1,1,9,4],
[6,6,0,4], [8,7,6,4]
# shape (8,4) type: 2d array or a matrix
import numpy as np
ar = np.array(0)
print(ar)
print("Shape of the array:")
print(ar.shape)

import numpy as np

ar = np.array([[12,20] ,[13,15]])
print(ar)
print("Shape of the array:")
print(ar.shape)
a = [1,2,3]
b = [2,3,4]
dot_product = a[0]*b[0] + a[1]*b[1] + a[2]*b[2]
print(dot_product)
import numpy as np
inputs = [1.0, 2.0, 3.0, 2.5]
weights = [0.2, 0.8, -0.5, 1.0]
bias = 2.0
outputs = np.dot(weights, inputs) + bias
print(outputs)
import numpy as np
inputs = [1.0, 2.0, 3.0, 2.5]
weights = [[0.2, 0.8, -0.5, 1],
[0.5, -0.91, 0.26, -0.5],
[-0.26, -0.27, 0.17, 0.87]]
biases = [2.0, 3.0, 0.5]
layer_outputs = np.dot(weights, inputs) + biases
print(layer_outputs)
#Another homologous list or an array
#Also called feature set instances
# Also called Observations

a = [1, 2, 3]
np.array([a])

#or
#a = [1, 2, 3]
#np.expand_dims(np.array(a), axis=0)
# A row vector is a matrix whose first dimension’s size (the
number of rows) equals 1 and the second dimension’s size (the
number of columns) equals n i.e. the vector size.
In other words, it’s a 1×n array or array of shape (1, n)
● # A column vector is a matrix where the second dimension’s
size equals 1, in other words, it’s an array of shape (n, 1)
# Now
import numpy as np
a = [1, 2, 3]
b = [2, 3, 4]
#multiple A x B
# i.e. Multiply row element of A with Column value of B to obtain Output value
#So, we transpose B and multiply using Dot Product A x Transpose (B)
a = np.array([a])
b = np.array([b]).T
np.dot(a, b)
● We have achieved the same result as the dot product of two
vectors, but performed on matrices and returning a matrix —
exactly what we expected and wanted.
● It’s worth mentioning that NumPy does not have a dedicated
method for performing matrix product — the dot product and
matrix product are both implemented in a single method: np.dot().
import numpy as np
inputs = [[1.0, 2.0, 3.0, 2.5],
[2.0, 5.0, -1.0, 2.0],
[-1.5, 2.7, 3.3, -0.8]]
weights = [[0.2, 0.8, -0.5, 1.0],
[0.5, -0.91, 0.26, -0.5],
[-0.26, -0.27, 0.17, 0.87]]
biases = [2.0, 3.0, 0.5]
layer_outputs = np.dot(inputs, np.array(weights).T) + biases
print(layer_outputs)
● # A lambda function is a small anonymous function.
● # A lambda function can take any number of arguments, but can only have one expression.
● # Use lambda functions when an anonymous function is required for a short period of time.
● x = lambda a : a + 10
● print(x(5))
● x = lambda a, b : a * b
● print(x(5, 6))
● def myfunc(n):
● return lambda a : a * n
● def myfunc(n):
● return lambda a : a * n
● mydoubler = myfunc(2)
● print(mydoubler(11))
● def myfunc(n):
● return lambda a : a * n
● mydoubler = myfunc(2)
● mytripler = myfunc(3)
● print(mydoubler(11))
● print(mytripler(11))

You might also like