0% found this document useful (0 votes)
7 views17 pages

Machine Learning Practical File

The document provides Python code for various data analysis and machine learning tasks using the Titanic dataset and the Iris dataset. It includes data extraction, visualization (heat matrix and scatter plots), linear and logistic regression, Naive Bayes classification, KNN, SVM, Random Forest classification, and building an Artificial Neural Network with backpropagation. Each section contains code snippets and descriptions of the processes involved in implementing these algorithms.

Uploaded by

Priyansh Kumar
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)
7 views17 pages

Machine Learning Practical File

The document provides Python code for various data analysis and machine learning tasks using the Titanic dataset and the Iris dataset. It includes data extraction, visualization (heat matrix and scatter plots), linear and logistic regression, Naive Bayes classification, KNN, SVM, Random Forest classification, and building an Artificial Neural Network with backpropagation. Each section contains code snippets and descriptions of the processes involved in implementing these algorithms.

Uploaded by

Priyansh Kumar
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

Ques 1: Write a program to Extract the data from the database using python.

Use
head(), tail(), info() commands in the imported data. Create a heat matrix and scatter
plot for the imported data base
Code:
import pandas as pd
df = pd.read_csv("titanic_train.csv")
[Link]()
output:

[Link]()

[Link]()
Heat matrix
import pandas as pd
import seaborn as sns
import [Link] as plt
df = pd.read_csv("titanic_train.csv")
# Exclude non-numeric columns
numeric_columns = df.select_dtypes(include=['number']).columns
numeric_df = df[numeric_columns]
# Create a heatmap
[Link](figsize=(12, 8))
heatmap_data = numeric_df.corr()
[Link](heatmap_data, annot=True, cmap='coolwarm', fmt=".2f")
[Link]('Heatmap for Titanic Dataset')
[Link]()
output
Scatter plots
import pandas as pd
import seaborn as sns
import [Link] as plt

df = pd.read_csv("titanic_train.csv")

# Scatter plot for Age and Fare


[Link](df['Age'], df['Fare'])
[Link]('Scatter Plot of Age vs Fare')
[Link]('Age')
[Link]('Fare')
[Link]()
output
Ques 2: Write a program to implement linear and logistic regression.
Code:
Linear regression
import pandas as pd
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error
from [Link] import SimpleImputer
import numpy as np

df = pd.read_csv("titanic_train.csv")
# Handling missing values in the 'Age' column using SimpleImputer
imputer = SimpleImputer(strategy='mean')
df['Age'] = imputer.fit_transform(df[['Age']])
# Selecting the features and target variable
X = df[['Age']].values
y = df['Fare'].values
# Splitting the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Creating a linear regression model
model = LinearRegression()
# Training the model
[Link](X_train, y_train)
# Making predictions on the test set
y_pred = [Link](X_test)
# Evaluating the model
mse = mean_squared_error(y_test, y_pred)
print(f'Mean Squared Error: {mse}')
# Predicting Fare for a new Age
new_age = [Link]([[25]]) # Replace 25 with the desired age
predicted_fare = [Link](new_age)
print(f'Predicted Fare for Age {new_age[0, 0]}: {predicted_fare[0]}')
# Plotting the linear regression line
[Link](X_test, y_test, color='blue', label='Actual Fare')
[Link](X_test, y_pred, color='red', linewidth=3, label='Linear Regression Line')
[Link](new_age, predicted_fare, color='green', marker='*', s=200, label=f'Predicted Fare
for Age {new_age[0, 0]}')
[Link]('Linear Regression Model')
[Link]('Age')
[Link]('Fare')
[Link]()
[Link]()
output:
Logistic Regression
Code:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score, precision_score, f1_score, confusion_matrix
import [Link] as plt
import seaborn as sns

# Read the CSV data


df = pd.read_csv("titanic_train.csv")

# Drop columns that are not needed for modeling


df = [Link](['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1)

# Convert categorical variables to numerical


df['Sex'] = df['Sex'].map({'male': 0, 'female': 1})
df['Embarked'] = df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2})

# Fill missing values in 'Age' with the median


df['Age'].fillna(df['Age'].median(), inplace=True)

# Fill missing values in 'Embarked' with the most common value


df['Embarked'].fillna(df['Embarked'].mode()[0], inplace=True)

# Split the data into features (X) and target variable (y)
X = [Link]('Survived', axis=1)
y = df['Survived']

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train the logistic regression model


model = LogisticRegression()
[Link](X_train, y_train)

# Make predictions on the test set


y_pred = [Link](X_test)

# Calculate evaluation metrics


accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)

# Print the metrics


print(f"Accuracy: {accuracy:.2f}")
print(f"Precision: {precision:.2f}")
print(f"F1 Score: {f1:.2f}")
print(f"Confusion Matrix:\n{conf_matrix}")

# Plot the confusion matrix


[Link](figsize=(6, 6))
[Link](conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False,
xticklabels=['Not Survived', 'Survived'],
yticklabels=['Not Survived', 'Survived'])
[Link]('Predicted')
[Link]('Actual')
[Link]('Confusion Matrix')
[Link]()
output:
Ques 3: Write a program to implement the naïve Bayesian classifier for a sample
training data set stored as a CSV file. Compute the accuracy of the classifier,
considering few test data sets.
Code:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score, classification_report, confusion_matrix

df = pd.read_csv("titanic_train.csv")

# Preprocess the data


df = df[['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']]
df['Sex'] = df['Sex'].map({'male': 0, 'female': 1})
df['Embarked'] = df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2})
df['Age'].fillna(df['Age'].median(), inplace=True)
df['Embarked'].fillna(df['Embarked'].mode()[0], inplace=True)

# Split the data into features and target


X = [Link]('Survived', axis=1)
y = df['Survived']

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train the Naive Bayes classifier


nb_classifier = GaussianNB()
nb_classifier.fit(X_train, y_train)

# Make predictions on the test set


y_pred = nb_classifier.predict(X_test)
# Evaluate the classifier
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
classification_rep = classification_report(y_test, y_pred)

print(f'Accuracy: {accuracy}')
print(f'Confusion Matrix:\n{conf_matrix}')
print(f'Classification Report:\n{classification_rep}')

output:
Ques 4: Write a program to implement k-nearest neighbors (KNN) and Support Vector
Machine (SVM) Algorithm for classification.
Code:
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
from [Link] import SVC
from sklearn import datasets
from [Link] import accuracy_score, classification_report, confusion_matrix

iris = datasets.load_iris()
X = [Link]
y = [Link]

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# K-nearest neighbors (KNN) classifier


knn_classifier = KNeighborsClassifier(n_neighbors=3)
knn_classifier.fit(X_train, y_train)
knn_predictions = knn_classifier.predict(X_test)

# Support Vector Machine (SVM) classifier


svm_classifier = SVC(kernel='linear')
svm_classifier.fit(X_train, y_train)
svm_predictions = svm_classifier.predict(X_test)

# Evaluate KNN classifier


knn_accuracy = accuracy_score(y_test, knn_predictions)
knn_conf_matrix = confusion_matrix(y_test, knn_predictions)
knn_classification_rep = classification_report(y_test, knn_predictions)
print("K-nearest neighbors (KNN) Classifier:")
print(f'Accuracy: {knn_accuracy}')
print(f'Confusion Matrix:\n{knn_conf_matrix}')
print(f'Classification Report:\n{knn_classification_rep}\n')

# Evaluate SVM classifier


svm_accuracy = accuracy_score(y_test, svm_predictions)
svm_conf_matrix = confusion_matrix(y_test, svm_predictions)
svm_classification_rep = classification_report(y_test, svm_predictions)

print("Support Vector Machine (SVM) Classifier:")


print(f'Accuracy: {svm_accuracy}')
print(f'Confusion Matrix:\n{svm_conf_matrix}')
print(f'Classification Report:\n{svm_classification_rep}')
output:
Ques 5: Implement classification of a given dataset using random forest.
Code:
from sklearn.model_selection import train_test_split
from [Link] import RandomForestClassifier
from sklearn import datasets
from [Link] import accuracy_score, classification_report, confusion_matrix

iris = datasets.load_iris()
X = [Link]
y = [Link]

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Random Forest Classifier


rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)
rf_classifier.fit(X_train, y_train)
rf_predictions = rf_classifier.predict(X_test)

# Evaluate the Random Forest classifier


rf_accuracy = accuracy_score(y_test, rf_predictions)
rf_conf_matrix = confusion_matrix(y_test, rf_predictions)
rf_classification_rep = classification_report(y_test, rf_predictions)

print("Random Forest Classifier:")


print(f'Accuracy: {rf_accuracy}')
print(f'Confusion Matrix:\n{rf_conf_matrix}')
print(f'Classification Report:\n{rf_classification_rep}')
output:
Ques 6: Build an Artificial Neural Network (ANN) by implementing the Back
propagation algorithm and test the same using appropriate data sets.
Code:
import numpy as np

# Sigmoid activation function and its derivative


def sigmoid(x, derivative=False):
if derivative:
return x * (1 - x)
return 1 / (1 + [Link](-x))

# Input data for XOR problem


X = [Link]([[0, 0],
[0, 1],
[1, 0],
[1, 1]])

# Target labels for XOR


y = [Link]([[0],
[1],
[1],
[0]])

# Set random seed for reproducibility


[Link](42)

# Neural Network architecture


input_layer_size = 2
hidden_layer_size = 4
output_layer_size = 1
# Initialize weights and biases
weights_input_hidden = 2 * [Link]((input_layer_size, hidden_layer_size)) - 1
weights_hidden_output = 2 * [Link]((hidden_layer_size, output_layer_size)) - 1

# Training parameters
learning_rate = 0.5
epochs = 10000

# Training the Neural Network using backpropagation


for epoch in range(epochs):
# Forward pass
hidden_layer_input = [Link](X, weights_input_hidden)
hidden_layer_output = sigmoid(hidden_layer_input)

output_layer_input = [Link](hidden_layer_output, weights_hidden_output)


predicted_output = sigmoid(output_layer_input)

# Calculate the error


error = y - predicted_output

# Backpropagation
output_error_term = error * sigmoid(predicted_output, derivative=True)
hidden_error = output_error_term.dot(weights_hidden_output.T)
hidden_error_term = hidden_error * sigmoid(hidden_layer_output, derivative=True)

# Update weights
weights_hidden_output += hidden_layer_output.[Link](output_error_term) * learning_rate
weights_input_hidden += [Link](hidden_error_term) * learning_rate

# Test the trained Neural Network


test_data = [Link]([[0, 0],
[0, 1],
[1, 0],
[1, 1]])

predicted_output_test =
sigmoid(sigmoid(test_data.dot(weights_input_hidden)).dot(weights_hidden_output))

print("Predicted Output after Training:")


print(predicted_output_test)
output:

You might also like