Import Pandas As PD DF PD - Read - CSV ("Titanic - Train - CSV") DF - Head
Import Pandas As PD DF PD - Read - CSV ("Titanic - Train - CSV") DF - Head
Use
head(), tail(), info() commands in the imported data. Create a heat matrix and pairplot
for the imported data base.
Code:
import pandas as pd
df = pd.read_csv("titanic_train.csv")
df.head()
output:
df.tail()
df.info()
1
Heat matrix
import pandas as pd
import seaborn as sns
import matplotlib.pyplot 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
plt.figure(figsize=(12, 8))
heatmap_data = numeric_df.corr()
sns.heatmap(heatmap_data, annot=True, cmap='coolwarm', fmt=".2f")
plt.title('Heatmap for Titanic Dataset')
plt.show()
output
2
pair plots
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv("titanic_train.csv")
# Filling missing values in the 'Age' column with the mean value
df['Age'].fillna(df['Age'].mean(), inplace=True)
# Create a pairplot
sns.pairplot(df, hue='Survived', markers=["o", "s"])
3
output
4
Ques 2: Write a program to implement linear and logistic regression.
Code:
Linear regression
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.impute 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
model.fit(X_train, y_train)
# Making predictions on the test set
y_pred = model.predict(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
5
new_age = np.array([[25]]) # Replace 25 with the desired age
predicted_fare = model.predict(new_age)
print(f'Predicted Fare for Age {new_age[0, 0]}: {predicted_fare[0]}')
# Plotting the linear regression line
plt.scatter(X_test, y_test, color='blue', label='Actual Fare')
plt.plot(X_test, y_pred, color='red', linewidth=3, label='Linear Regression Line')
plt.scatter(new_age, predicted_fare, color='green', marker='*', s=200, label=f'Predicted Fare
for Age {new_age[0, 0]}')
plt.title('Linear Regression Model')
plt.xlabel('Age')
plt.ylabel('Fare')
plt.legend()
plt.show()
output:
6
Logistic Regression
Code:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, f1_score, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
# Split the data into features (X) and target variable (y)
X = df.drop('Survived', axis=1)
y = df['Survived']
7
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
8
output:
9
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 sklearn.metrics import accuracy_score, classification_report, confusion_matrix
df = pd.read_csv("titanic_train.csv")
10
# 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:
11
Ques 4: Write a program to implement k-nearest neighbors (KNN) and Support Vector
Machine (SVM) Algorithm for classification.
Code:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report
12
knn_model.fit(X_train, y_train)
# SVM algorithm
svm_model = SVC(kernel='linear')
svm_model.fit(X_train_scaled, y_train)
13
output:
14
Ques 5: Implement classification of a given dataset using random forest and decision
tree.
Code:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report
df = pd.read_csv("kyphosis.csv")
# Split the data into features (X) and target variable (y)
X = df.drop('Kyphosis', axis=1)
y = df['Kyphosis']
15
print("Classification Report:")
print(classification_report(y_test, dt_predictions))
16
output:
17
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
18
# Initialize weights and biases
weights_input_hidden = 2 * np.random.random((input_layer_size, hidden_layer_size)) - 1
weights_hidden_output = 2 * np.random.random((hidden_layer_size, output_layer_size)) - 1
# Training parameters
learning_rate = 0.5
epochs = 10000
# 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.T.dot(output_error_term) * learning_rate
weights_input_hidden += X.T.dot(hidden_error_term) * learning_rate
19
test_data = np.array([[0, 0],
[0, 1],
[1, 0],
[1, 1]])
predicted_output_test =
sigmoid(sigmoid(test_data.dot(weights_input_hidden)).dot(weights_hidden_output))
20