0% found this document useful (0 votes)
5 views

PR Code

Uploaded by

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

PR Code

Uploaded by

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

=========== PR 1 ===================

import numpy as np

import matplotlib.pyplot as plt

from scipy.stats import norm

# Generate synthetic data

np.random.seed(42)

class_0 = np.random.normal(loc=0, scale=1, size=100)

class_1 = np.random.normal(loc=3, scale=1, size=100)

# Prior probabilities

P_C0 = 0.5 # P(Class 0)

P_C1 = 0.5 # P(Class 1)

# Likelihoods

def likelihood(x, class_mean, class_std):

return norm.pdf(x, loc=class_mean, scale=class_std)

# Decision boundary

def decision_boundary(x):

likelihood_0 = likelihood(x, class_mean=0, class_std=1)

likelihood_1 = likelihood(x, class_mean=3, class_std=1)

P_x_C0 = likelihood_0 * P_C0

P_x_C1 = likelihood_1 * P_C1

return P_x_C0, P_x_C1

# Create a range of x values

x_values = np.linspace(-2, 5, 100)

P_x_C0_vals, P_x_C1_vals = decision_boundary(x_values)


# Make decisions based on posterior probabilities

decisions = np.where(P_x_C0_vals > P_x_C1_vals, 0, 1)

# Plotting

plt.figure(figsize=(10, 6))

plt.plot(x_values, P_x_C0_vals, label='P(x|C0) * P(C0)', color='blue')

plt.plot(x_values, P_x_C1_vals, label='P(x|C1) * P(C1)', color='orange')

plt.axvline(x=1.5, color='red', linestyle='--', label='Decision Boundary')

plt.title('Bayesian Decision Theory')

plt.xlabel('Feature Value')

plt.ylabel('Probability Density')

plt.legend()

plt.grid()

plt.show()

==================== Output ========================


============== PR 3 ===========================

class Parser:

def __init__(self, string):

self.string = string

self.position = 0

self.pattern = [] # To store the recognized pattern

def parse(self):

result = self.S()

return result

def S(self):

# Try to parse as A

pattern_a = self.A()

if pattern_a:

self.pattern.append(pattern_a)

return True

# Reset position for B

self.position = 0

pattern_b = self.B()

if pattern_b:

self.pattern.append(pattern_b)

return True

return False

def A(self):

if self.position < len(self.string) and self.string[self.position] == 'a':

start = self.position

while self.position < len(self.string) and self.string[self.position] == 'a':

self.position += 1

return f"A({self.string[start:self.position]})" # Return the pattern


return None

def B(self):

if self.position < len(self.string) and self.string[self.position] == 'b':

start = self.position

while self.position < len(self.string) and self.string[self.position] == 'b':

self.position += 1

return f"B({self.string[start:self.position]})" # Return the pattern

return None

# Example usage

input_string = "aaabbb" # Modify this string to test different patterns

parser = Parser(input_string)

if parser.parse():

print(f"The string '{input_string}' is recognized by the grammar.")

print("Generated Pattern:", " -> ".join(parser.pattern))

else:

print(f"The string '{input_string}' is NOT recognized by the grammar.")

================== output ======================


The string 'aaabbb' is recognized by the grammar.
Generated Pattern: A(aaa)
============== pr 4,5 ===============================

import numpy as np

import tensorflow as tf

from tensorflow import keras

import matplotlib.pyplot as plt

# Load MNIST dataset

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Normalize the images to a range of 0 to 1

x_train = x_train.astype('float32') / 255.0

x_test = x_test.astype('float32') / 255.0

# Reshape the data to add a channel dimension

x_train = np.expand_dims(x_train, axis=-1)

x_test = np.expand_dims(x_test, axis=-1)

# Build the CNN model

def create_model():

model = keras.Sequential([

keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),

keras.layers.MaxPooling2D((2, 2)),

keras.layers.Conv2D(64, (3, 3), activation='relu'),

keras.layers.MaxPooling2D((2, 2)),

keras.layers.Conv2D(64, (3, 3), activation='relu'),

keras.layers.Flatten(),

keras.layers.Dense(64, activation='relu'),

keras.layers.Dense(10, activation='softmax')

])

return model
model = create_model()

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the model

model.fit(x_train, y_train, epochs=5, batch_size=64, validation_split=0.2)

# Evaluate the model on test data

test_loss, test_accuracy = model.evaluate(x_test, y_test)

print(f'Test accuracy: {test_accuracy:.4f}')

# Predict on the test dataset

predictions = model.predict(x_test)

# Display some predictions

for i in range(5):

plt.imshow(x_test[i].reshape(28, 28), cmap='gray')

plt.title(f'Predicted: {np.argmax(predictions[i])}, Actual: {y_test[i]}')

plt.axis('off')

plt.show()

========== output ==========================


Epoch 1/5
750/750 [==============================] - 9s 11ms/step - loss: 0.2160 - accuracy: 0.9355 - val_loss:
0.0696 - val_accuracy: 0.9793
Epoch 2/5
750/750 [==============================] - 9s 12ms/step - loss: 0.0601 - accuracy: 0.9810 - val_loss:
0.0573 - val_accuracy: 0.9820
Epoch 3/5
750/750 [==============================] - 9s 12ms/step - loss: 0.0412 - accuracy: 0.9870 - val_loss:
0.0631 - val_accuracy: 0.9809
Epoch 4/5
750/750 [==============================] - 9s 12ms/step - loss: 0.0300 - accuracy: 0.9909 - val_loss:
0.0401 - val_accuracy: 0.9889
Epoch 5/5
750/750 [==============================] - 9s 11ms/step - loss: 0.0258 - accuracy: 0.9917 - val_loss:
0.0450 - val_accuracy: 0.9879
313/313 [==============================] - 2s 7ms/step - loss: 0.0325 - accuracy: 0.9896
Test accuracy: 0.9896

You might also like