1
Practical -
1: To study and represent data from different sources and formats.
// Read the CSV data
import pandas as pd
df = pd.read_csv("/content/[Link]") [Link]()
// Read the text file
with open("/content/[Link]") as file:
txt_file = [Link]() print(txt_file)
REPRESENTING DATA IN DIFFERENT FORMATS
TABULAR REPRESENTATION df
= [Link](json_data)
print(df)
DM&ML Abbas Raza
2
2. DICTONARY REPRESENTATION
data_dict = df.to_dict() print(data_dict)
{'P003': {'name': 'Sneha Kulkarni', 'city': 'Pune', 'age': 22, 'gender': 'female', 'height': 1.6, 'weight': 45,
'bmi': 17.58, 'verdict': 'Underweight', 'id': nan}, 'P004': {'name': 'Arjun Verma', 'city': 'Mumbai', 'age':
40, 'gender': 'male', 'height': 1.8, 'weight': 90.0, 'bmi': 27.78, 'verdict': 'Normal', 'id': nan}, 'P005':
{'name': 'Palak', 'city': 'string', 'age': 1, 'gender': 'male', 'height': 1.0, 'weight': 1.0, 'bmi': 31.22,
'verdict': 'Obese', 'id': 'P005'}, 'P007': {'name': 'Kashish', 'city': 'Nagpur', 'age': 20, 'gender': 'male',
'height': 1.8, 'weight': 60.0, 'bmi': 18.52, 'verdict': 'Normal', 'id': nan}}
3. JSON REPRESENTATION
json_data = df.to_json() print(json_data)
{"P003":{"name":"Sneha
Kulkarni","city":"Pune","age":22,"gender":"female","height":1.6,"weight":45,"bmi":17.58,"verdict":"
Underweight","id":null},"P004":{"name":"Arjun
Verma","city":"Mumbai","age":40,"gender":"male","height":1.8,"weight":90.0,"bmi":27.78,"verdict"
:"Normal","id":null},"P005":{"name":"Palak","city":"string","age":1,"gender":"male","height":1.0,"w
eight":1.0,"bmi":31.22,"verdict":"Obese","id":"P005"},"P007":{"name":"Kashish","city":"Nagpur","a
ge":20,"gender":"male","height":1.8,"weight":60.0,"bmi":18.52,"verdict":"Normal","id":null}}
2: To preprocess data by handling missing values, noise removal,
normalization, and data integration.
Handling Missing values:
DM&ML Abbas Raza
3
Practical -
[Link]().sum()
import numpy as np columns_to_replace_zero = ['Glucose', 'BloodPressure',
'SkinThickness', 'Insulin', 'BMI'] df[columns_to_replace_zero] =
df[columns_to_replace_zero].replace(0, [Link]) print("Missing values after replacing 0
with NaN:") [Link]().sum() for column in columns_to_replace_zero:
df[column].fillna(df[column].mean(), inplace=True) print("Missing values after
imputation:")
[Link]().sum()
Noise Removal
from [Link] import StandardScaler
scaler = StandardScaler() X = [Link]('Outcome',
axis=1) y = df['Outcome']
X_scaled = scaler.fit_transform(X)
DM&ML Abbas Raza
4
X_scaled_df = [Link](X_scaled, columns=[Link])
X_scaled_df.head()
X_scaled_df.mean()
X_scaled_df.std()
3: To visualize datasets using graphs such as ba r charts, histograms,
scatter plots, box plots, and heatmaps.
Bar Chart:
import [Link] as plt
import seaborn as sns [Link](figsize=(6, 4))
[Link](x='Outcome', data=df)
DM&ML Abbas Raza
5
Practical -
[Link]('Distribution of Diabetes Outcome')
[Link]('Outcome (0: Non-Diabetic, 1: Diabetic)')
[Link]('Count')
[Link]()
Histogram:
print("\n#### Histograms of Numerical Features")
[Link](figsize=(18, 12)) for i, column in
enumerate([Link][:-1]): # Exclude 'Outcome'
[Link](3, 3, i + 1) # Adjust subplot grid as needed
[Link](df[column], kde=True) [Link](f'Distribution of
{column}') plt.tight_layout()
DM&ML Abbas Raza
6
[Link]()
Scatter Plot:
print("\n#### Scatter Plot: Glucose vs BMI by Outcome") [Link](figsize=(8,
6)) [Link](x='Glucose', y='BMI', hue='Outcome', data=df, palette='viridis', s=50,
alpha=0.7) [Link]('Glucose vs BMI colored by Outcome') [Link]('Glucose') [Link]('BMI')
[Link](title='Outcome')
DM&ML Abbas Raza
7
[Link]()
Box Plot:
print("\n#### Box Plots of Key Features")
[Link](figsize=(18, 10)) selected_columns = ['Pregnancies', 'Glucose',
'BloodPressure', 'BMI', 'Age'] for i, column in
enumerate(selected_columns):
[Link](2, 3, i + 1)
[Link](y=df[column])
[Link](f'Box Plot of {column}')
plt.tight_layout() [Link]()
Heatmap:
DM&ML Abbas Raza
8
print("\n#### Correlation Heatmap")
[Link](figsize=(10, 8)) [Link]([Link](), annot=True,
cmap='coolwarm', fmt=".2f") [Link]('Correlation Matrix of
Features') [Link]()
Practical 4: To implement frequent pattern mining on market basket datasets.
DM&ML Abbas Raza
9
-
import pandas as pd from [Link] import
TransactionEncoder from mlxtend.frequent_patterns import
apriori, association_rules
// Creating a sample market basket dataset dataset = [['Milk',
'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],
['Dill', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],
['Milk', 'Apple', 'Kidney Beans', 'Eggs'],
['Milk', 'Unicorn', 'Corn', 'Kidney Beans', 'Yogurt'],
['Corn', 'Onion', 'Onion', 'Kidney Beans', 'Ice cream', 'Eggs']]
print("Sample Market Basket Dataset:") for transaction in
dataset: print(transaction)
te = TransactionEncoder() te_ary =
[Link](dataset).transform(dataset) df_encoded =
[Link](te_ary, columns=te.columns_) print("One-
hot Encoded DataFrame:") display(df_encoded.head())
DM&ML Abbas Raza
10
//Apriori Algorithm frequent_itemsets = apriori(df_encoded, min_support=0.6,
use_colnames=True)
print("Frequent Itemsets (min_support=0.6):")
display(frequent_itemsets)
Practical 5: To apply feature selection techniques on available datasets
Corelation Analysis : - print("Correlation of features with the
'Outcome' variable:")
[Link]()['Outcome'].sort_values(ascending=False)
DM&ML Abbas Raza
11
CHI – SQUARE test: from sklearn.feature_selection import SelectKBest, chi2 import pandas as pd
df_binary = [Link]() for column in [Link]('Outcome'): # Exclude 'Outcome'
when creating feature dataframe if df[column].dtype in ['int64', 'float64']: median_val =
df[column].median() df_binary[f'{column}_high'] = (df[column] > median_val).astype(int)
X_chi2 = df_binary y_chi2 = df['Outcome'] selector = SelectKBest(chi2, k='all') # Select all features
to see scores [Link](X_chi2, y_chi2) chi_scores = [Link]({ 'Feature':
X_chi2.columns,
'P_Value': selector.pvalues_
})
display(chi_scores.sort_values(by='P_Value', ascending=True))
Recursive feature elimination (RFE):- from
sklearn.feature_selection import RFE from
DM&ML Abbas Raza
12
sklearn.linear_model import LogisticRegression
X_rfe = X_scaled_df y_rfe = y
estimator = LogisticRegression(solver='liblinear', random_state=42) # 'liblinear' is good for small datasets
rfe_selector = RFE(estimator=estimator, n_features_to_select=5, step=1) # step=1 removes one feature
at a time rfe_selector.fit(X_rfe, y_rfe) display(ranking.sort_values())
Practical 6: To implement classification algorithms for predictive analysis
from sklearn.model_selection import train_test_split from [Link] import
DecisionTreeClassifier from [Link] import accuracy_score,
classification_report, confusion_matrix import [Link] as plt import
seaborn as sns print("### Decision Tree Classifier Implementation\n") X =
X_scaled_df y = y
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
print(f"Testing set size: {X_test.shape[0]} samples")
dtree_classifier = DecisionTreeClassifier(random_state=42)
dtree_classifier.fit(X_train, y_train) print("Decision Tree
Classifier trained successfully.") y_pred =
DM&ML Abbas Raza
13
-
dtree_classifier.predict(X_test) print("Predictions made on
the test set.")
accuracy = accuracy_score(y_test, y_pred) print(f"Accuracy of
the Decision Tree Classifier: {accuracy:.4f}")
DM&ML Abbas Raza
14
Practical
- 7: To implement regression algorithms for continuous value prediction
Regression Algorithm – LINEAR REGRESSION:
from sklearn.model_selection import train_test_split from sklearn.linear_model
import LinearRegression from [Link] import mean_absolute_error,
mean_squared_error, r2_score import [Link] as plt import seaborn as
sns import numpy as np print("### Linear Regression Implementation\n") X_reg =
X_scaled_df.drop('BMI', axis=1) # Features y_reg = df['BMI']
# Split the dataset into training and testing sets
X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(X_reg, y_reg, test_size=0.2,
random_state=42) print(f"Training set size for regression: {X_train_reg.shape[0]} samples")
print(f"Testing set size for regression: {X_test_reg.shape[0]} samples")
# TRAINING THE MODEL linear_reg_model =
LinearRegression() linear_reg_model.fit(X_train_reg,
y_train_reg) print("Linear Regression model trained
successfully.") y_pred_reg =
linear_reg_model.predict(X_test_reg
print("Predictions made on the test set.")
DM&ML Abbas Raza
15
# Evaluate the model's performance mae =
mean_absolute_error(y_test_reg, y_pred_reg) mse =
mean_squared_error(y_test_reg, y_pred_reg) rmse =
[Link](mse) r2 = r2_score(y_test_reg, y_pred_reg)
print(f"Mean Absolute Error (MAE): {mae:.4f}")
print(f"Mean Squared Error (MSE): {mse:.4f}")
print(f"Root Mean Squared Error (RMSE): {rmse:.4f}")
print(f"R-squared (R2): {r2:.4f}")
- 8: To apply clustering algorithms for identifying patterns in data
K means Clustering :- from [Link] import
KMeans from [Link] import
StandardScaler import [Link] as plt
import seaborn as sns print("### K-Means
Clustering Implementation\n")
# For clustering, we'll use our scaled features (X_scaled_df)
DM&ML Kashish Kumar Singh
16
Practical
X_cluster = X_scaled_d
# --- Elbow Method to find optimal K ---
wcss = [] # Within-cluster sum of squares for
i in range(1, 11):
kmeans = KMeans(n_clusters=i, init='k-means++', random_state=42, n_init=10)
[Link](X_cluster) [Link](kmeans.inertia_)
print("\nBased on the elbow plot, choose an appropriate K where the rate of decrease in WCSS slows down
significantly For demonstration, let's assume K=3 for now.")
chosen_k = 3 # Example: choose K based on the Elbow Method kmeans_model =
KMeans(n_clusters=chosen_k, init='k-means++', random_state=42, n_init=10) clusters =
kmeans_model.fit_predict(X_cluster)
# Add the cluster assignments to our scaled DataFrame for visualization
X_cluster_with_labels = X_cluster.copy()
X_cluster_with_labels['Cluster'] = clusters
display(X_cluster_with_labels.head()) from [Link] import PCA pca =
PCA(n_components=2) X_pca = pca.fit_transform(X_cluster) pca_df = [Link](data = X_pca,
columns = ['principal component 1', 'principal component 2']) [Link](figsize=(10, 8))
[Link](x='principal component 1', y='principal component 2', hue='Cluster', data=pca_df,
palette=sns.color_palette('tab10', n_colors=chosen_k), s=100, alpha=0.7) [Link]('Principal
Component 1') [Link]('Principal Component 2') [Link](title='Cluster') [Link]()
DM&ML Abbas Raza
17
– 9: Detect Anomalies and Outliers
from [Link] import IsolationForest import
[Link] as plt import seaborn as sns print("### Anomaly
and Outlier Detection (Isolation Forest)\n" iso_forest =
IsolationForest(random_state=42, contamination=0.01) anomalies =
iso_forest.fit_predict(X_scaled_df)
X_scaled_df_with_anomalies = X_scaled_df.copy()
X_scaled_df_with_anomalies['Anomaly'] = anomalies
print("Number of anomalies detected:", list(anomalies).count(-1))
from [Link] import PCA pca =
PCA(n_components=2)
X_pca_anomalies = pca.fit_transform(X_scaled_df
DM&ML Kashish Kumar Singh
18
Practical
pca_df_anomalies = [Link](data = X_pca_anomalies, columns = ['principal component 1',
'principal component 2']) pca_df_anomalies['Anomaly']
= anomalies [Link](figsize=(10, 8)) [Link](
x='principal component 1', y='principal component 2',
hue='Anomaly', data=pca_df_anomalies,
palette={1: 'blue', -1: 'red'},
s=100, alpha=0.7
[Link]('Anomaly Detection using Isolation Forest (PCA Reduced)')
[Link]('Principal Component 1') [Link]('Principal
Component 2')
[Link](title='Anomaly (1: Inlier, -1: Outlier)')
[Link](True) [Link]() print("\nHead of
DataFrame with anomaly labels:")
display(X_scaled_df_with_anomalies.head())
DM&ML Abbas Raza
19
- 10: Ensemble Learning: Random Forest Classifier
from [Link] import RandomForestClassifier from [Link] import
accuracy_score, classification_report, confusion_matrix import [Link] as
plt import seaborn as sns print("### Random Forest Classifier Implementation\n")
rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)
# Train the model rf_classifier.fit(X_train, y_train) print("Random
Forest Classifier trained successfully.") y_pred_rf =
rf_classifier.predict(X_test) print("Predictions made on the test set.")
accuracy_rf = accuracy_score(y_test, y_pred_rf) print(f"\nAccuracy of
the Random Forest Classifier: {accuracy_rf:.4f}")
print("\nClassification Report:") print(classification_report(y_test,
y_pred_rf)) print("\nConfusion Matrix:") cm_rf =
confusion_matrix(y_test, y_pred_rf) [Link](figsize=(6, 5))
[Link](cm_rf, annot=True, fmt='d', cmap='Blues',
xticklabels=['Non-Diabetic', 'Diabetic'],
yticklabels=['Non-Diabetic', 'Diabetic'])
DM&ML Kashish Kumar Singh
20
Practical
[Link]('Predicted') [Link]('Actual')
[Link]('Confusion Matrix for Random Forest Classifier')
[Link]()
DM&ML Abbas Raza