0% found this document useful (0 votes)
2 views21 pages

Practical File

The document outlines a series of experiments focused on Python programming and data analysis techniques, including operations in Python, data loading and preprocessing, statistical analysis, and machine learning applications such as classification and clustering. Each experiment includes aims, theoretical background, code examples, and expected outputs. The experiments cover various libraries like Pandas, Scikit-learn, and visualization tools to handle datasets and perform analyses.

Uploaded by

vangaurd60
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)
2 views21 pages

Practical File

The document outlines a series of experiments focused on Python programming and data analysis techniques, including operations in Python, data loading and preprocessing, statistical analysis, and machine learning applications such as classification and clustering. Each experiment includes aims, theoretical background, code examples, and expected outputs. The experiments cover various libraries like Pandas, Scikit-learn, and visualization tools to handle datasets and perform analyses.

Uploaded by

vangaurd60
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

TABLE OF CONTENT

S. No. Name of Experiment Date of Date of Signature


Experiment Submission

1 Introduction to Python, operations in


Python
2 Loading datasets a) Loading data from CSV
file b) Compute the basic statistics of given
data - shape, no. of columns, mean c)
Splitting a data frame on values of
categorical variables d) Indexing of the
data. e) Display top 10 rows of data
3 Write a python program to impute missing
values with various techniques on given
dataset. a) Remove rows/ attributes b)
Replace with mean or mode
4 Data Statistics and Data Visualization: Write
a Program to do them
5.1 Classification: 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
5.2 Write a program to perform Clustering using
K-Means Algorithm. Display Dendrogram.
6.1 Write a program to implement PCA example
using scikit-learn on Iris Data-set.
6.2 Write a program to implement Nearest
Neighbors classification on Iris Data-set and
plot the decision boundaries for each class.
6.3 Write a program to implement Nearest
Neighbors classification on Iris Data-set and
plot the decision boundaries for each class.
6.4 Write a program to implement Neural
network for classification on standard
dataset.
7 (a) Perform Data pre-processing. b) Apply
statistical analysis c) Perform Visual
analytics d) On final data, perform
Classification/ Clustering. e) Write a research
paper and communicate the same in a journal
or a conference.

Experiment – 1
Aim:
Introduction to Python & Operations in Python.
Theory:
Python is a high-level, interpreted programming language developed by Guido van Rossum in 1991. It is
simple, readable, and widely used for web development, data science, artificial intelligence, and
automation.
Python supports different types of operations:

1. Arithmetic Operations
Used to perform mathematical calculations. Operators:
• + Addition
• - Subtraction
• * Multiplication
• / Division
• % Modulus
• ** Exponent
• // Floor Division

2. Comparison Operations
Used to compare two values. Operators: ==, !=, >, <, >=, <=

3. Logical Operations
Used to combine conditional statements. Operators: and, or, not
Code:
a = 10
b = 5

# Arithmetic Operations
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Modulus:", a % b)
print("Exponent:", a ** b)
print("Floor Division:", a // b)

# Comparison Operations
print("a is equal to b:", a == b)
print("a is greater than b:", a > b)
print("a is less than b:", a < b)

# Logical Operations
print("Logical AND:", a > 5 and b > 2)
print("Logical OR:", a < 5 or b > 2)
print("Logical NOT:", not(a > b))

Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Modulus: 0
Exponent: 100000
Floor Division: 2
a is equal to b: False
a is greater than b: True
a is less than b: False
Logical AND: True
Logical OR: True
Logical NOT: False
Experiment – 2
Aim:
Write a Python program to do operations on a loaded dataset.
Library Used: Pandas
Dataset: California Housing Dataset
The dataset contains housing-related information from California districts, commonly used for data
analysis and machine learning practice. It consists of 3000 records and 9 numerical features.
Code:
import pandas as pd

# (a) Loading data from CSV file


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

# (b) Compute basic statistics: shape, columns, and mean


print([Link])
print([Link])
print([Link](numeric_only=True))

# (c) Splitting a DataFrame based on categorical (string) variables


string_columns = []
for col in [Link]:
if df[col].dtype == 'object': # Check for categorical columns
string_columns.append(col)
if string_columns:
print("Columns with string values:")
for col_name in string_columns:
print(f"- {col_name}")
else:
print("No columns with string values found in the DataFrame.")

# (d) Indexing of the data


print([Link][0:5])

# (e) Display top 10 rows of data


print([Link](10))

Output:
Shape of Dataset : (3000, 9)
Columns in Dataset: Index(['longitude', 'latitude', 'housing_median_age',
'total_rooms',
'total_bedrooms', 'population', 'households', 'median_income',
'median_house_value'], dtype='object')

Mean of Features Containing Numerical Values :


longitude -119.589200
latitude 35.635390
housing_median_age 28.845333
total_rooms 2599.578667
total_bedrooms 529.950667
population 1402.798667
households 489.912000
median_income 3.807272
median_house_value 205846.275000
dtype: float64

No columns with string values found in the DataFrame.


Experiment – 3
Aim:
Data preprocessing – handling missing values: Write a python program to impute missing values with
various techniques on given dataset.
Code:
# a. Remove rows or attributes with missing values:
df_rows_dropped = retail_df.dropna(how='any')
print("Shape of DataFrame after dropping rows with any missing values:")
print(df_rows_dropped.shape)
print("\nFirst 5 rows of df_rows_dropped:")
display(df_rows_dropped.head())

# b. Replace with mean or mode:


# Mean Imputation (Numerical Attributes)
df_imputed_mean = retail_df.copy()
mean_price = retail_df['Price'].mean()
mean_rating = retail_df['Rating'].mean()
mean_discount = retail_df['Discount'].mean()
df_imputed_mean['Price'].fillna(mean_price, inplace=True)
df_imputed_mean['Rating'].fillna(mean_rating, inplace=True)
df_imputed_mean['Discount'].fillna(mean_discount, inplace=True)
print("Missing values after mean imputation:")
print(df_imputed_mean[['Price', 'Rating', 'Discount']].isnull().sum())
print("\nFirst 5 rows of df_imputed_mean after imputation:")
display(df_imputed_mean.head())

# Mode Imputation (Categorical Attributes):


df_imputed_mode = retail_df.copy()
mode_category = retail_df['Category'].mode()[0]
mode_stock = retail_df['Stock'].mode()[0]
df_imputed_mode['Category'].fillna(mode_category, inplace=True)
df_imputed_mode['Stock'].fillna(mode_stock, inplace=True)
print("Missing values after mode imputation:")
print(df_imputed_mode[['Category', 'Stock']].isnull().sum())
print("\nFirst 5 rows of df_imputed_mode after imputation:")
display(df_imputed_mode.head())

# c) Remove / replace missing values beyond a threshold value:


df_threshold_rows_dropped = retail_df.copy()
# Count missing values per row
missing_count_per_row = df_threshold_rows_dropped.isnull().sum(axis=1)
# Remove rows with more than 2 missing values
df_threshold_rows_dropped = df_threshold_rows_dropped[missing_count_per_row <= 2]
print("Shape of DataFrame after dropping rows with more than 2 missing values:")
print(df_threshold_rows_dropped.shape)
print("\nFirst 5 rows of df_threshold_rows_dropped:")
display(df_threshold_rows_dropped.head())

Output:
a)
b)

c)
Experiment – 4
Aim:
Data Statistics and Data Visualization – Library: Pandas
a) Display Frequency distribution; b) Normalization; c) Outlier detection; d) Correlation analysis; e)
Different plots
Code:
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
import kagglehub
from [Link] import MinMaxScaler

path = kagglehub.dataset_download("nayonahmedlol/movies-metadata-dataset-tmdb-
style")
df = pd.read_csv(path+"/movies_metadata.csv")

language_distribution = df['original_language'].value_counts()
print(language_distribution)

scaler = MinMaxScaler()
df['runtime_normalized'] = scaler.fit_transform(df[['runtime']])
print(df[['runtime', 'runtime_normalized']].head())

Q1 = df['vote_average'].quantile(0.25)
Q3 = df['vote_average'].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
outliers = df[(df['vote_average'] < lower_bound) | (df['vote_average'] >
upper_bound)]
print("Outliers in 'vote_average' column using IQR method:")
print(outliers[['title', 'vote_average']])

numerical_cols = df.select_dtypes(include=['float64', 'int64']).columns


correlation_matrix = df[numerical_cols].corr()
[Link](figsize=(10, 8))
[Link](correlation_matrix, annot=True, cmap='coolwarm', fmt='.2f')
[Link]('Correlation Matrix of Numerical Features')

[Link](figsize=(12, 6))
[Link](x=language_distribution.head(10).index,
y=language_distribution.head(10).values)
[Link]('Top 10 Original Language Distribution')
[Link]('Original Language')
[Link]('Number of Movies')

[Link](figsize=(10, 6))
[Link](df['vote_average'], bins=10, kde=True)
[Link]('Distribution of Vote Average')
[Link]('Vote Average')
[Link]('Frequency')
[Link]()

Output:
original_language
en 32280
fr 2438
it 1529
ja 1350
de 1080
...
hy 1
lb 1
si 1
Name: count, Length: 92, dtype: int64

Runtime runtime_normalized
0 81.0 0.064490
1 104.0 0.082803
2 101.0 0.080414
3 127.0 0.101115
4 106.0 0.084395

Outliers in 'vote_average' column using IQR method:


title vote_average
83 Last Summer in the Hamptons 0.0
107 Headless Body in Topless Bar 0.0
126 Jupiter's Wife 0.0
132 Sonic Outlaws 0.0
137 Target 0.0
... ... ...
[3603 rows x 2 columns]
Experiment – 5
5.1 Aim:
Program to implement the Naïve Bayesian Classifier. Compute the accuracy of the classifier,
considering few test data sets.
Code:
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score
import numpy as np

df = pd.read_csv(file_path)
X = [Link][:, 1:]
y = [Link][:, 0]

print("Missing values in features (X):\n", [Link]().sum().sum())


print("Missing values in target (y):\n", [Link]().sum())

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


random_state=42)
print(f"Shape of X_train: {X_train.shape}")
print(f"Shape of X_test: {X_test.shape}")
print(f"Shape of y_train: {y_train.shape}")
print(f"Shape of y_test: {y_test.shape}")

model = GaussianNB()
[Link](X_train, y_train)
y_pred = [Link](X_test)

classifier = GaussianNB()
scores = cross_val_score(classifier, X, y, cv=5)
print(f"Cross-validation scores: {scores}")
print(f"Mean accuracy: {[Link](scores):.4f}")
print(f"Standard deviation of accuracy: {[Link](scores):.4f}")

Output:
Missing values in features (X): 0
Missing values in target (y): 0
Shape of X_train: (15999, 784)
Shape of X_test: (4000, 784)
Shape of y_train: (15999,)
Shape of y_test: (4000,)
Cross-validation scores: [0.55925 0.57375 0.58350 0.56650 0.56264]
Mean accuracy: 0.5691
Standard deviation of accuracy: 0.0084

5.2 Aim:
Clustering: Write a program to perform Clustering on a dataset using K-Means Algorithm. Display its
Dendrogram.
Code:
from [Link] import StandardScaler
from [Link] import dendrogram, linkage
from [Link] import PCA
from [Link] import KMeans
import [Link] as plt
import seaborn as sns
import pandas as pd

file_path = 'sample_data/california_housing_train.csv'
df = pd.read_csv(file_path)
missing_values = [Link]().sum()
print("Missing values per column:\n", missing_values)

numerical_cols = [Link]()
scaler = StandardScaler()
df_scaled = [Link](scaler.fit_transform(df[numerical_cols]),
columns=numerical_cols)

inertia = []
for k in range(1, 11):
kmeans = KMeans(n_clusters=k, random_state=42, n_init='auto')
[Link](df_scaled)
[Link](kmeans.inertia_)

[Link](figsize=(10, 6))
[Link](range(1, 11), inertia, marker='o')
[Link]('Elbow Method for Optimal K')
[Link]('Number of Clusters (K)')
[Link]('Inertia')
[Link](range(1, 11))
[Link](True)
[Link]()

kmeans = KMeans(n_clusters=3, random_state=42, n_init='auto')


[Link](df_scaled)
df_scaled['cluster_label'] = kmeans.labels_

pca = PCA(n_components=2)
pca_components = pca.fit_transform(df_scaled.drop('cluster_label', axis=1))
df_pca = [Link](data=pca_components, columns=['pca_component_1',
'pca_component_2'])
df_pca['cluster_label'] = df_scaled['cluster_label']

centroids_pca = df_pca.groupby('cluster_label')[['pca_component_1',
'pca_component_2']].mean()

[Link](figsize=(10, 8))
[Link](x='pca_component_1', y='pca_component_2', hue='cluster_label',
palette='viridis', data=df_pca, legend='full', alpha=0.6)
[Link](centroids_pca['pca_component_1'], centroids_pca['pca_component_2'],
marker='X', s=200, color='red', label='Centroids')
[Link]('K-Means Clusters (PCA-reduced)')
[Link]('PCA Component 1')
[Link]('PCA Component 2')
[Link]()
[Link](True)
[Link]()
df_subset = df_scaled.sample(n=1000, random_state=42).drop('cluster_label',
axis=1)
linked_data = linkage(df_subset, method='ward', metric='euclidean')
print("Shape of the linkage matrix:", linked_data.shape)

[Link](figsize=(20, 10))
dendrogram(linked_data)
[Link]('Hierarchical Clustering Dendrogram')
[Link]('Sample Index')
[Link]('Distance')
[Link]()

Output:
Missing values per column:
longitude 0
latitude 0
housing_median_age 0
total_rooms 0
total_bedrooms 0
population 0
households 0
median_income 0
median_house_value 0
dtype: int64

Shape of the linkage matrix: (999, 4)


Experiment – 6
6.1 Aim:
Write a program to implement PCA example using scikit-learn on Iris Dataset.
Code:
import numpy as np
import [Link] as plt
from sklearn import datasets
from [Link] import PCA

iris = datasets.load_iris()
X_iris, y_iris = [Link], [Link]

pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_iris)

print(f"Original shape: {X_iris.shape}, PCA shape: {X_pca.shape}")


print(f"Explained variance ratio: {pca.explained_variance_ratio_}")

[Link](figsize=(8, 6))
[Link](X_pca[:, 0], X_pca[:, 1], c=y_iris, cmap='viridis')
[Link]('Principal Component 1')
[Link]('Principal Component 2')
[Link]('PCA of Iris Dataset')
[Link](ticks=[0, 1, 2], format=[Link](lambda i, *args:
iris.target_names[int(i)]))
[Link]()

Output:

6.2 Aim:
Write a program to implement Nearest Neighbors classification on Iris Dataset and plot the decision
boundaries for each class.
Code:
from sklearn import datasets
from [Link] import KNeighborsClassifier
from [Link] import ListedColormap
import numpy as np
import [Link] as plt

iris = datasets.load_iris()
X_iris, y_iris = [Link], [Link]
X_knn = X_iris[:, :2]
y_knn = y_iris

clf = KNeighborsClassifier(n_neighbors=15)
[Link](X_knn, y_knn)

h = .02
x_min, x_max = X_knn[:, 0].min() - 1, X_knn[:, 0].max() + 1
y_min, y_max = X_knn[:, 1].min() - 1, X_knn[:, 1].max() + 1
xx, yy = [Link]([Link](x_min, x_max, h), [Link](y_min, y_max, h))
Z = [Link](np.c_[[Link](), [Link]()])
Z = [Link]([Link])

[Link](figsize=(8, 6))
[Link](xx, yy, Z, alpha=0.8, cmap=[Link])
[Link](X_knn[:, 0], X_knn[:, 1], c=y_knn, edgecolors='k',
cmap=[Link])
[Link]("Nearest Neighbors Decision Boundaries (k=15)")
[Link]('Sepal length')
[Link]('Sepal width')
[Link]()

Output:

6.3 Aim:
Write a program to implement SVM for classification on standard dataset.
Code:
import numpy as np
from sklearn import datasets
from [Link] import SVC
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score

digits = datasets.load_digits()
X, y = [Link], [Link]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,


random_state=42)

svm_clf = SVC(kernel='linear')
svm_clf.fit(X_train, y_train)
y_pred_svm = svm_clf.predict(X_test)
print(f"SVM Accuracy on Digits Dataset: {accuracy_score(y_test,
y_pred_svm):.4f}")

Output:
SVM Accuracy on Digits Dataset: 0.9805

6.4 Aim:
Write a program to implement a Neural Network for classification on standard dataset.
Code:
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from sklearn.neural_network import MLPClassifier
from [Link] import accuracy_score

digits = datasets.load_digits()
X, y = [Link], [Link]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,


random_state=42)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = [Link](X_test)

mlp = MLPClassifier(hidden_layer_sizes=(50,), max_iter=1000, alpha=1e-4,


solver='sgd', random_state=1)
[Link](X_train_scaled, y_train)
y_pred_nn = [Link](X_test_scaled)
print(f"Neural Network Accuracy on Digits Dataset: {accuracy_score(y_test,
y_pred_nn):.4f}")

Output:
Neural Network Accuracy on Digits Dataset: 0.9704
Experiment – 7
7.1 Aim:
To perform Data Pre-processing on a given CSV dataset.
Code:
import pandas as pd
from [Link] import LabelEncoder

# Load dataset
df = pd.read_csv("[Link]")

# Clean column names


[Link] = [Link]()
print("Initial Data:\n", [Link]())

# Check missing values


print("\nMissing Values:\n", [Link]().sum())

# Remove duplicates
df = df.drop_duplicates()

# Handle missing values


for col in [Link]:
if df[col].dtype != 'object':
df[col].fillna(df[col].median(), inplace=True)
else:
df[col].fillna(df[col].mode()[0], inplace=True)

# Encode categorical columns


le = LabelEncoder()
for col in [Link]:
if df[col].dtype == 'object':
df[col] = le.fit_transform(df[col])

print("\nProcessed Data:\n", [Link]())


print("\nFinal Shape:", [Link])

Output:
7.2 Aim:
To perform Statistical Analysis on the dataset.
Code:
import pandas as pd

df = pd.read_csv("[Link]")
[Link] = [Link]()

# Descriptive statistics
print("Statistical Summary:\n", [Link]())

# Correlation matrix
print("\nCorrelation Matrix:\n", [Link](numeric_only=True))

# Class distribution (if class column exists)


if "class" in [Link]:
print("\nClass Distribution:\n", df["class"].value_counts())

Output:

7.3 Aim:
To perform Visual Analytics on the dataset.
Code:
import [Link] as plt

features = [Link][:-1]

# Histogram
[Link]()
[Link](df[features[0]], bins=30)
[Link]("Histogram")
[Link](features[0])
[Link]("Frequency")
[Link]()

# Boxplot
[Link]()
df[features].boxplot()
[Link]("Boxplot")
[Link](rotation=45)
[Link]()

# Scatter plot
if len(features) >= 2:
[Link]()
[Link](df[features[0]], df[features[1]], c=df[[Link][-1]])
[Link](features[0])
[Link](features[1])
[Link]("Scatter Plot")
[Link]()

Output:
7.4 Aim:
To perform Classification and Clustering on the final dataset.
Code:
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score

df = pd.read_csv("[Link]")
[Link] = [Link]()

X = [Link][:, :-1].values
y = [Link][:, -1].values

# Scaling
scaler = StandardScaler()
X = scaler.fit_transform(X)

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)

# Model
model = KNeighborsClassifier(n_neighbors=7)
[Link](X_train, y_train)

# Prediction
y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))

Output:

You might also like