DL & AI - Lab Manual
DL & AI - Lab Manual
NO: 1
DECISION TREE CLASSIFICATION USING SCIKIT LEARN
DATE:
AIM:
To write the python program to implement and evaluate a decision tree classification
ALGORITHM:
prompt/terminal.
STEP 3: Load your dataset using Pandas. For example, if your dataset is a CSV file
STEP 4: Preprocess the Data.Check for missing values and handle them appropriately
STEP 8: Use the trained model to make predictions on the test set
STEP 10: Visualize the decision tree using Matplotlib and Scikit-learn's plot_tree function
PROGRAM:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn import tree
data = pd.read_csv(r'D:\Dataset\Decision_Tree_ Dataset.csv',sep= ',', header= 0)
print ("Dataset Lenght:: "), len(data)
print ("Dataset Shape:: "), data.shape
print ("Dataset:: ")
data.head()
X = data.values[:, 1:5]
Y = data.values[:,0]
X_train, X_test, y_train, y_test = train_test_split( X, Y, test_size = 0.3, random_state =
100)
clf_entropy = DecisionTreeClassifier(criterion = "entropy", random_state = 100,
max_depth=3, min_samples_leaf=5)
clf_entropy.fit(X_train, y_train)
y_pred = clf_entropy.predict(X_test)
y_pred
print ("Accuracy is "), accuracy_score(y_test,y_pred)*100
OUTPUT:
RESULT:
Thus, the Decision Tree Classification Experiment has been executed successfully.
The model was trained and evaluated on the given dataset
EXP.NO: 2
THEANO FOR COMPUTING A LOGISTIC FUNCTION
DATE:
AIM:
To compute the logistic (or sigmoid) function using Theano and evaluate its output for a
given input value.
ALGORITHM:
STEP 1: Ensure Theano is installed in your Python environment
STEP 4: Implement the logistic (sigmoid) function using Theano’s tensor operations.
STEP 6: Pass an input value to the compiled function and retrieve the result
STEP 7: Output the result to see the computed logistic function value
PROGRAM:
def logistic_function(x):
return tf.math.sigmoid(x)
OUTPUT:
RESULT:
Thus, the logistic (or sigmoid) function using Theano Experiment has been executed
successfully. The model was trained and evaluated on the given dataset.
EXP.NO: 3
CALCULATE DATA LOSS USING TENSOR
DATE:
AIM:
To write a Python program to calculate data loss using TensorFlow for a neural network
model on a given dataset.
ALGORITHM:
STEP 1: Install TensorFlow
STEP 5: Compile the model with an optimizer, loss function, and metrics
STEP 7: Evaluate the model's performance on the training data to calculate loss and
accuracy
STEP 8: Use the trained model to make predictions on the training data
PROGRAM:
import tensorflow as tf
import numpy as np
X_train = np.random.rand(10, 5)
y_train = np.random.randint(0, 2, size=(10, 1))
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu', input_shape=(5,)), # Hidden layer
tf.keras.layers.Dense(1, activation='sigmoid') # Output layer
])
RESULT:
Thus, the logistic function computation using Theano has been executed successfully.
The function was defined symbolically and compiled
EXP.NO: 4
CLASSIFY HANDWRITTEN DIGITS USING MNIST DATASET
DATE:
AIM:
To write a Python program to classify handwritten digits using the MNIST dataset with
TensorFlow and Keras, implementing a neural network model for this task.
ALGORITHM:
STEP 1: Install TensorFlow
STEP 7: Use the trained model to make predictions on the test data
STEP 8: Visualize some sample predictions along with their true labels
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import layers, models
model = models.Sequential([
layers.Flatten(input_shape=(28, 28)),
layers.Dense(128, activation='relu'),
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
predictions = model.predict(x_test)
plt.figure(figsize=(5, 5))
plt.imshow(x_test[0], cmap=plt.cm.binary)
plt.title(f'Predicted Label: {np.argmax(predictions[0])}')
plt.show()
OUTPUT:
RESULT:
Thus, the classification of handwritten digits using the MNIST dataset has been executed
successfully.
EXP.NO: 5
IMAGE MANIPULATION USING SCIPY.
DATE:
AIM:
To write a Python program to perform image manipulation using the SciPy library
including reading image and performing various operations.
ALGORITHM:
STEP 1: Install TensorFlow
STEP 3: Load the image using imageio.imread(or use any other image)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')
plt.show()
from scipy.ndimage import rotate
RESULT:
Thus, the image manipulation tasks have been successfully executed using SciPy
EXP.NO: 6
PREDICT THE COLOR RED OR WHITE USING KERAS
DATE:
AIM:
To predict whether a color is red or white based on its RGB values using a neural network
model built with Keras.
ALGORITHM:
STEP 1: Install TensorFlow
STEP 3: Set a random seed for reproducibility and generate synthetic data for colors
STEP 4: Create binary labels where a color is labeled as 'Red' (1) if the red component
STEP 8: Predict the color of a new sample using the trained model
PROGRAM:
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import matplotlib.pyplot as plt
np.random.seed(42)
num_samples = 1000
colors = np.random.rand(num_samples, 3)
labels = (colors[:, 0] > 0.5).astype(int)
split_ratio = 0.8
split_index = int(num_samples * split_ratio)
train_colors, test_colors = colors[:split_index], colors[split_index:]
train_labels, test_labels = labels[:split_index], labels[split_index:]
model = Sequential([
Dense(64, activation='relu', input_shape=(3,)),
Dense(32, activation='relu'),
Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_colors, train_labels, epochs=100, batch_size=32, validation_split=0.2)
loss, accuracy = model.evaluate(test_colors, test_labels)
print("Test accuracy:", accuracy)
RESULT:
Thus, the color classification model using Keras has been executed successfully. The
neural network was trained to classify colors as either red or white based on their RGB
values.
EXP.NO: 6
SPEECH TO TEXT AND TEXT TO SPEECH USING IBM API KEYS
DATE:
AIM:
To implement speech-to-text and text-to-speech functionalities using IBM’s Watson
API services.
PROCEDURE:
STEP 1: Install the ibm-watson library to interact with IBM Watson APIs
STEP 2: Import Necessary Packages
STEP 3: Set your IBM Watson API keys and endpoints
STEP 4: Authenticate and create service instances
STEP 5: Convert speech to text from an audio file
STEP 6: Test the functionalities with appropriate audio and text inputs to ensure they
work as expected
STEP 7: The results demonstrate the practical application of IBM Watson's AI services
for converting between speech and text, enabling effective interaction with audio and
textual data.
PROGRAM:
api_key = 'dFpr6elI-0sSQWJyXTa_gi7-Mi9NFZ9u80KwySh9WHFO'
url = 'https://api.eu-gb.speech-to-text.watson.cloud.ibm.com/instances/81676a4c-9a69-
420a-b684-4d4a0cf6f752'
authenticator = IAMAuthenticator(api_key)
speech_to_text = SpeechToTextV1(authenticator=authenticator)
speech_to_text.set_service_url(url)
def speech_to_text_conversion(audio_file_path):
with open('D:\\Technologies\\Deep Learning Models & AI Analyst\\Lab
Excersizes\\harvard.wav', 'rb') as audio_file:
response = speech_to_text.recognize(
audio=audio_file,
content_type='audio/wav', # Adjust if using a different audio format
model='en-US_BroadbandModel'
).get_result()
transcript = response['results'][0]['alternatives'][0]['transcript']
return transcript
audio_file_path = r'D:\Technologies\Deep Learning Models & AI Analyst\Lab
Excersizes\harvard.wav' # Replace with your audio file path
transcript = speech_to_text_conversion(audio_file_path)
print(f'Transcript: {transcript}')
OUTPUT:
RESULT:
Thus, the speech-to-text and text-to-speech functionalities using IBM Watson APIs have
been implemented successfully.
EXP.NO: 9
IMPLEMENTING LINEAR REGRESSION USING PYTHON
DATE:
AIM
To implement linear regression using Python to predict house prices based on various
features of the houses.
ALGORITHM:
STEP 2: Perform data cleaning, handling missing values, and encoding categorical
variables if necessary.
STEP 4: Visualize the data to understand the relationships between features and the target
STEP 5: Use plots like scatter plots, histograms, and heatmaps to identify patterns and
correlations.
STEP 6: Identify and select the most relevant features that influence house prices.
STEP 9: Evaluate the model performance using metrics such as Mean Absolute Error
(MAE).
PROGRAM:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
data = pd.read_csv(r'D:\Technologies\Deep Learning Models & AI Analyst\Lab
Excersizes\housing.csv')
data_cleaned = data.dropna()
X = data_cleaned[['longitude', 'latitude', 'housing_median_age', 'total_rooms', 'total_bedrooms',
'population', 'households', 'median_income', 'ocean_proximity']]
y = data_cleaned['median_house_value']
preprocessor = ColumnTransformer(
transformers=[
('cat', OneHotEncoder(), ['ocean_proximity'])
],
remainder='passthrough'
)
X_processed = preprocessor.fit_transform(X)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_processed)
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
plt.figure(figsize=(8, 6))
plt.scatter(y_test, y_pred, color='blue', alpha=0.5)
plt.xlabel('Actual House Prices')
plt.ylabel('Predicted House Prices')
plt.title('Actual vs. Predicted House Prices')
plt.show()
OUTPUT:
RESULT
Thus, a linear regression model was successfully implemented to predict house prices
based on various features.
EXP.NO: 10
EVALUATING LOGISTIC REGRESSION
DATE:
AIM
The aim of this experiment is to develop a predictive model using logistic regression to
identify customers in the telecom industry who are likely to churn.
ALGORITHM:
STEP 3: Convert categorical variables into numerical values using techniques like one-hot
encoding
STEP 4: Visualize the distribution of features and the relationship between features and
STEP 8: Evaluate the model on the testing dataset using metrics such as accuracy.
PROGRAM:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
X = data.drop('Churn_Yes', axis=1)
y = data['Churn_Yes']
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
monthly_charges = np.linspace(data['MonthlyCharges'].min(),
data['MonthlyCharges'].max(), 300).reshape(-1, 1)
avg_values = {col: data[col].mean() for col in X.columns if col != 'MonthlyCharges'}
template_data = pd.DataFrame(avg_values, index=range(len(monthly_charges)))
template_data['MonthlyCharges'] = monthly_charges.flatten()
template_data = template_data[X.columns]
monthly_charges_probs = model.predict_proba(template_data)[:, 1]
plt.figure(figsize=(10, 6))
plt.scatter(data['MonthlyCharges'], y, color='blue', label='Data points')
plt.plot(monthly_charges, monthly_charges_probs, color='red', linewidth=2,
label='Sigmoid curve')
plt.xlabel('Monthly Charges')
plt.ylabel('Probability of Churn')
plt.title('Logistic Regression Sigmoid Curve')
plt.legend()
plt.show()
OUTPUT:
RESULT:
Thus, logistic regression model successfully predicts customer churn with an acceptable
level of accuracy and other performance metrics.
EXP.NO: 11
PERFORMANCE EVALUATION USING K-MEANS
DATE: CLUSTERING
AIM:
The aim of this analysis is to perform K-Means clustering on a dataset of countries, based
ALGORITHM:
STEP 1: Read the dataset from a CSV file containing information about countries
STEP 2: Convert the categorical 'Language' column into numerical values using one-hot
encoding.
STEP 4: Apply the K-Means algorithm with a predefined number of clusters (K=2).
STEP 7: Plot the countries on a scatter plot using latitude and longitude. Different clusters
encoder = OneHotEncoder(sparse=False)
encoded_languages = encoder.fit_transform(df[['Language']])
encoded_df = pd.DataFrame(encoded_languages,
columns=encoder.get_feature_names_out())
df['Cluster'] = clusters
silhouette_avg = silhouette_score(scaled_features, clusters)
print(f'Silhouette Score: {silhouette_avg:.2f}')
plt.figure(figsize=(10, 6))
plt.scatter(df['Longitude'], df['Latitude'], c=df['Cluster'], cmap='viridis', marker='o', s=100)
centers = kmeans.cluster_centers_
centers = scaler.inverse_transform(np.hstack((centers[:, :2], np.zeros((centers.shape[0],
encoded_languages.shape[1])))))[:, :2]
plt.scatter(centers[:, 1], centers[:, 0], c='red', marker='x', s=200, label='Centroids')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('K-Means Clustering')
plt.legend()
plt.grid(False)
plt.show()
OUTPUT:
RESULT:
Thus, scatter plot displays the countries in different clusters, with distinct colors
representing different clusters.
EXP.NO: 12
WEATHER PREDICTION USING NAIVE BAYES
DATE: CLASSIFICATION
AIM:
The aim of this analysis is to predict weather conditions based on given features such as
temperature, humidity, and wind speed using Naive Bayes classification.
ALGORITHM:
STEP 2: Convert the categorical 'Language' column into numerical values using one-hot
encoding.
STEP 4: Train a Naive Bayes classifier using the training data. Gaussian Naive Bayes is
STEP 6: Use the trained model to make predictions on the test data.
STEP 7: Visualize the results to understand how well the model performs.
PROGRAM:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv(r'D:\Technologies\Deep Learning Models & AI Analyst\Lab
Excersizes\Weather_data.csv')
X = df.drop('Weather', axis=1)
y = df['Weather']
categorical_features = ['WindDirection'] # Example categorical feature
numeric_features = ['Temperature', 'Humidity'] # Example numeric features
preprocessor = ColumnTransformer(
transformers=[
('num', StandardScaler(), numeric_features),
('cat', OneHotEncoder(), categorical_features)
])
X_processed = preprocessor.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X_processed, y, test_size=0.3,
random_state=0)
model = GaussianNB()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
print('Classification Report:')
print(classification_report(y_test, y_pred))
print('Confusion Matrix:')
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=model.classes_,
yticklabels=model.classes_)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
plt.show()
OUTPUT:
RESULT:
Thus, the Naïve Bayes Classification was successfully predicted with the help of Machine
learning algorithm.