0% found this document useful (0 votes)
3 views32 pages

Data Science Practical

This document details experiments using the Matplotlib and NumPy libraries for data visualization and numerical analysis. It covers various plot types including line, scatter, bar, histogram, and pie charts, along with their customization parameters, as well as key NumPy functions for array creation, manipulation, and mathematical operations. The report emphasizes the importance of understanding these tools for effective data representation and analysis.
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)
3 views32 pages

Data Science Practical

This document details experiments using the Matplotlib and NumPy libraries for data visualization and numerical analysis. It covers various plot types including line, scatter, bar, histogram, and pie charts, along with their customization parameters, as well as key NumPy functions for array creation, manipulation, and mathematical operations. The report emphasizes the importance of understanding these tools for effective data representation and analysis.
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

Experiment -1

Aim:- Study of Matplotlib library (charts, functions – bar chart, line chart, pie chart,
histogram, scatter plot with all specifications like labels, x–y functions).

Objective:- This report outlines the practical implementation of five fundamental plot types
in the Matplotlib library, demonstrating the use of their most critical customization
parameters for data visualization.

1. Line Plot ([Link])

Aim

To visualize the relationship and trend between two continuous variables, showing the
progression of one variable (Y) as the other variable (X, often time) changes.

Theory

The [Link]() function connects sequential data points with line segments. It is best used for
time series data, frequency response, or any data where the order of points is meaningful.
Customization is handled by specifying attributes of the Line2D object, such as line color,
style, and data point markers.

Code

import [Link] as plt


import numpy as np

# Data
x = [Link](0, 10, 50)
y = [Link](x)
y2 = [Link](x)

[Link](figsize=(10, 5))

# Plotting the first line with extensive parameters


[Link](
x, y,
color='darkgreen', # Line color
linestyle='--', # Line style (dashed)
linewidth=3.0, # Thickness of the line
marker='o', # Marker style (circle)
markersize=8, # Size of the marker
markeredgecolor='black',# Color of the marker border
markevery=5, # Show marker every 5 points
alpha=0.8, # Transparency
label='Sine Wave'
)
# Plotting the second line
[Link](
x, y2,
color='red',
linestyle=':', # Dotted line
linewidth=1.5,
label='Cosine Wave'
)

# Customizing Axes and Labels


[Link]('Line Plot: Trend Analysis with Custom Markers', fontsize=16)
[Link]('X-axis (Time Index)', fontsize=12)
[Link]('Y-axis (Amplitude)', fontsize=12)
[Link](loc='upper right', frameon=True, shadow=True)
[Link](True, linestyle='-', alpha=0.5)
[Link](0, 10)
[Link](-1.2, 1.2)
plt.tight_layout()
[Link]()

Conclusion

Line plots effectively convey trends over a continuous domain. Mastery of parameters like
linestyle, linewidth, and marker is crucial for distinguishing multiple data series and
enhancing plot readability.

2. Scatter Plot ([Link])

Aim

To visualize the distribution of data points for two numerical variables and identify
correlations, clusters, or outliers.
Theory

The [Link]() function plots individual data points as markers. Unlike [Link], it does not
assume sequential connection between points. Its primary strength lies in its ability to map
additional variables to visual properties like marker size (s) and color (c), enabling
visualization in up to four dimensions (X, Y, Size, Color).

Code

import [Link] as plt

import numpy as np

# Data generation (4 dimensions: X, Y, Size, Color)

[Link](42)

N = 100

x = [Link](N) * 100

y = [Link](N) * 100

sizes = [Link](N) * 1000 # 3rd Dimension: Marker Area (Magnitude)

colors = [Link](N) # 4th Dimension: Color Value (Z-value)

[Link](figsize=(8, 8))

# Key correction: marker='o' replaces marker='s'

scatter = [Link](

x, y,

s=sizes, # Marker size (Area, 3rd dimension)

c=colors, # Color values (4th dimension mapping to Z-value)

alpha=0.6, # Transparency (Mitigates overplotting)

marker='o', # CORRECT Marker shape: Circle (dotted form)

edgecolor='darkorange', # Boundary color

linewidths=1.5, # Boundary width


)

# Adding a colorbar for Z-value interpretation

cbar = [Link](scatter)

cbar.set_label('Feature Importance (Z-value)')

[Link]('Optimized Scatter Plot: Multi-dimensional Data Visualization (Dotted Form'),


fontsize=16)

[Link]('Feature X', fontsize=12)

[Link]('Feature Y', fontsize=12)

plt.tight_layout()

Conclusion

The scatter plot is indispensable for bivariate analysis, and its parameters (s, c, cmap) allow it
to become a powerful tool for visualizing higher-dimensional datasets through aesthetic
mapping.
3. Bar Plot ([Link])

Aim

To compare the quantities or frequencies of different discrete, categorical groups.

Theory

The [Link]() function creates vertical rectangular bars, where the height of the bar is
proportional to the value it represents. It is ideal for showing frequency distributions or
comparing summary statistics across categories. Key parameters are used to control the visual
separation and stacking of the bars.

Code

import [Link] as plt


import numpy as np

# Data
categories = ['Q1', 'Q2', 'Q3', 'Q4']
sales_2023 = [350, 420, 390, 510]
errors = [20, 15, 25, 30] # Error data

[Link](figsize=(8, 6))

[Link](
categories,
sales_2023,
width=0.6, # Width of the bars (controlling spacing)
color=['#4C72B0', '#55A868', '#C44E52', '#8172B2'], # Custom colors
edgecolor='black', # Border color
linewidth=1.0, # Border width
alpha=0.9, # Transparency
align='center', # Alignment of bar with tick
# Parameter for error visualization
yerr=errors, # Error array
ecolor='darkred', # Error bar color
capsize=8 # Length of the error bar caps
)

[Link]('Bar Plot: Quarterly Sales with Error Bars', fontsize=16)


[Link]('Quarters', fontsize=12)
[Link]('Sales Revenue (K)', fontsize=12)
[Link](0, 600)
plt.tight_layout()
[Link]()

Conclusion
Bar plots offer a quick comparative overview of categorical data. The inclusion of width and
yerr/xerr (for error bars) allows for precise control over the visual presentation and statistical
rigor.
4. Histogram ([Link])
Aim
To display the underlying frequency distribution of a single numerical variable by grouping
data into bins and counting observations in each bin.
Theory
The [Link]() function calculates and draws a histogram. The shape of a histogram reveals
important information about the data's central tendency, spread, and modality. The most
critical parameter is bins, which determines the number or boundaries of the intervals (bins).
Code
import [Link] as plt
import numpy as np

# Data (1000 samples from a Normal Distribution)


[Link](42)
data = [Link](loc=100, scale=15, size=1000)
mean_val = [Link](data)

[Link](figsize=(8, 6))

[Link](
data,
bins=30, # Number of bins
density=True, # Normalize to form a probability density
color='teal', # Color of the bars
alpha=0.6, # Transparency
histtype='stepfilled', # Type: 'bar', 'barstacked', 'step', 'stepfilled'
edgecolor='gray', # Border color
range=(50, 150), # The range of the data to consider
label='Distribution'
)

# Adding a line for the mean


[Link](mean_val, color='orange', linestyle='-', linewidth=2, label=f'Mean:
{mean_val:.2f}')

[Link]('Histogram: Distribution Analysis (Density Plot)', fontsize=16)


[Link]('Data Values (Scores)', fontsize=12)
[Link]('Probability Density', fontsize=12)
[Link]()
plt.tight_layout()
[Link]()
Conclusion

Histograms are the primary tool for understanding data distribution. The bins and density
parameters fundamentally change the plot's interpretation, making their appropriate use
essential for accurate statistical representation.

5. Pie Plot ([Link])

Aim

To represent data as slices of a pie, showing the proportion of each category relative to the
whole.

Theory

The [Link]() function draws a circular chart divided into proportional sectors. While often
criticized for poor quantitative comparison, it is effective for showcasing market share or
contribution to a total. Critical parameters include explode (to emphasize slices) and autopct
(to display percentages).

Code

import [Link] as plt


import numpy as np

# Data
sizes = [40, 30, 15, 15]
labels = ['North', 'South', 'East', 'West']
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']

# Explode the 'South' slice to emphasize it


explode = (0, 0.15, 0, 0)

[Link](figsize=(7, 7))

[Link](
sizes,
explode=explode,
labels=labels,
autopct='%1.2f%%', # Format percentage string with 2 decimal places
shadow=True, # Add shadow for 3D effect
startangle=140, # Rotates the start of the plot
colors=colors, # Custom colors
pctdistance=0.7, # Distance of percentage labels from the center
labeldistance=1.1, # Distance of category labels from the center
wedgeprops={'linewidth': 2, 'edgecolor': 'darkgray'} # Customize the wedges
)

[Link]('Pie Plot: Regional Market Share', fontsize=16)


# Ensures the pie is drawn as a circle, not an ellipse
[Link]('equal')
plt.tight_layout()
[Link]()

Conclusion

Pie plots are visually engaging for simple proportional data. The explode and autopct
parameters are key to making pie charts informative and aesthetically appealing by adding
emphasis and numerical context.
Experiment -2
Aim:-Study of Numpy Library

Objective:-

To understand the architecture and implement the 28 most extensively used functions and
concepts within the NumPy (Numerical Python) library, focusing on multi-dimensional array
creation, manipulation, advanced indexing, mathematical operations, and statistical
computations.

Theory

NumPy's core is the ndarray (N-dimensional array) object, which stores homogeneous data
and allows for fast, efficient vectorization of mathematical operations without explicit loops
(a process called vectorization). This report covers functions critical for data preprocessing
and numerical analysis, highlighting their essential parameters.

I. Array Creation Functions (5 Functions)

Key Parameters
Function Purpose
Covered
[Link]() Creates an array from a Python list/tuple. dtype, copy
[Link]() Creates an array with regularly spaced values. start, stop, step, dtype
Creates an array with a specified number of start, stop, num,
[Link]()
samples over an interval. endpoint, dtype
[Link]() /
Creates an array filled with zeros or ones. shape, dtype
[Link]()
Creates an array filled with a specific constant
[Link]() shape, fill_value, dtype
value.

Code: Array Creation

import numpy as np

print("--- 1. Array Creation ---")

# 1. [Link]()
arr1 = [Link]([1, 2, 3], dtype='float64', copy=True)
print(f"1. [Link] (float64): {arr1}")

# 2. [Link]()
arr2 = [Link](start=0, stop=10, step=2, dtype=np.int32)
print(f"2. [Link]: {arr2}")
# 3. [Link]()
arr3 = [Link](start=0, stop=1, num=5, endpoint=True)
print(f"3. [Link] (5 points): {arr3}")
# 4. [Link]() / [Link]()
arr4_z = [Link](shape=(2, 3), dtype=int)
arr4_o = [Link](shape=(2, 3), dtype=int)
print(f"4. [Link] (2x3): \n{arr4_z}")

# 5. [Link]()
arr5 = [Link](shape=(3, 3), fill_value=42.5, dtype=float)
print(f"5. [Link] (3x3, value 42.5): \n{arr5}")

II. Array Attributes and Manipulation (6 Functions/Concepts)

Function/Concept Purpose Key Parameters Covered


.shape, .dtype, .ndim Essential attributes for array structure. N/A (Read-only attributes)
Gives a new shape to an array without
.reshape() newshape, order ('C' or 'F')
changing its data.
Reverses the axes of an array (rows
.T (Transpose) N/A (Attribute)
become columns).
Joins a sequence of arrays along an axis (0 for rows, 1 for
[Link]()
existing axis. columns)
Splits an array into multiple sub-
[Link]() indices_or_sections, axis
arrays.
[Link]() / Stacks arrays vertically (row-wise) or N/A (Helper functions for
[Link]() horizontally (column-wise). concatenation)
Code: Array Manipulation

print("\n--- 2. Array Attributes and Manipulation ---")

arr_base = [Link](12) # 0 to 11

# Attributes
print(f"Array: {arr_base}")
print(f"Shape: {arr_base.shape}, Dtype: {arr_base.dtype}, Ndim: {arr_base.ndim}")

# .reshape()
arr_reshaped = arr_base.reshape((3, 4), order='C') # 'C' is C-style (row-major)
print(f"Reshaped (3x4): \n{arr_reshaped}")

# .T (Transpose)
arr_transposed = arr_reshaped.T
print(f"Transposed (4x3): \n{arr_transposed}")

# [Link]()
A = [Link]([[1, 2], [3, 4]])
B = [Link]([[5, 6], [7, 8]])
arr_concat_row = [Link]((A, B), axis=0)
arr_concat_col = [Link]((A, B), axis=1)
print(f"Concatenate (axis=0, Row-wise): \n{arr_concat_row}")
print(f"Concatenate (axis=1, Col-wise): \n{arr_concat_col}")

# [Link]() / [Link]()
arr_v = [Link]((A, B))
print(f"VStack (Vertical Stack): \n{arr_v}")

# [Link]()
arr_split = [Link](arr_concat_row, 2, axis=0) # Split into 2 equal parts
print(f"Split (into 2 parts, axis=0): {arr_split[0].shape}, {arr_split[1].shape}")
III. Mathematical and Linear Algebra Operations (6 Functions/Concepts)
Key Parameters
Function/Concept Purpose
Covered
Universal functions (ufuncs) for element-wise (Element-wise
[Link]() / [Link]()
addition/subtraction. operations)
Calculates the dot product (matrix multiplication
[Link]() / @ N/A
or inner product).
Calculates matrix product (preferred over [Link]
[Link]() N/A
for matrices).
Computes the (multiplicative) inverse of a
[Link]() (Input matrix)
matrix.
[Link]() Computes the determinant of a square matrix. (Input matrix)
[Link]() / [Link]() / Ufuncs for common element-wise operations
(Input array)
[Link]() (e.g., square root, exponential, log).
Code: Mathematical Operations

print("\n--- 3. Mathematical and Linear Algebra ---")

# Data
M1 = [Link]([[1, 2], [3, 4]])
M2 = [Link]([[5, 6], [7, 8]])

# [Link]() and [Link]()


arr_sum = [Link](M1, M2)
arr_diff = [Link](M1, M2)
print(f"M1 + M2 (Element-wise): \n{arr_sum}")

# [Link]() or @ (Matrix Multiplication)


arr_matmul = M1 @ M2
print(f"M1 @ M2 (Matrix Multiplication): \n{arr_matmul}")

# [Link]()
det_M1 = [Link](M1)
print(f"Determinant of M1: {det_M1:.2f}")

# [Link]()
inv_M1 = [Link](M1)
print(f"Inverse of M1: \n{inv_M1.round(2)}")

# [Link]()
arr_sqrt = [Link](M1)
print(f"Element-wise sqrt(M1): \n{arr_sqrt.round(2)}")

# [Link]() / [Link]()
arr_exp = [Link](M1)
print(f"Element-wise exp(M1, first row): {arr_exp[0].round(2)}")
IV. Statistical Operations (6 Functions)

Key Parameters
Function Purpose
Covered
[Link]() Computes the arithmetic mean. axis, dtype, keepdims
[Link]() Computes the median. axis, keepdims
[Link]() Computes the standard deviation. axis, ddof, keepdims
[Link]() / [Link]() Finds the maximum or minimum value. axis, keepdims
[Link]() /
Returns the indices of the max/min elements. axis
[Link]()
Returns elements chosen from x or y based
[Link]() condition, x, y
on condition.

Code: Statistical Operations

print("\n--- 4. Statistical Operations ---")

# Data (3x3 array)


stats_arr = [Link]([[10, 20, 30], [5, 15, 25], [40, 50, 60]])
print(f"Data Array: \n{stats_arr}")

# [Link]() / [Link]() / [Link]()


mean_row = [Link](stats_arr, axis=1, keepdims=True)
std_col = [Link](stats_arr, axis=0, ddof=1) # ddof=1 for sample STD
print(f"Mean across rows (keepdims=True): \n{mean_row.round(2)}")
print(f"Standard Deviation across columns: {std_col.round(2)}")

# [Link]() / [Link]()
max_val = [Link](stats_arr)
print(f"Global Maximum: {max_val}")

# [Link]() / [Link]()
argmax_row = [Link](stats_arr, axis=1)
print(f"Index of Max element per row: {argmax_row}") # e.g., 2 means 3rd element is max in
each row

# [Link]()
condition = stats_arr > 30
result = [Link](condition, 'High', 'Low') # Replace values based on condition
print(f"[Link] (Conditional Replacement): \n{result}")
V. Indexing and Broadcasting (5 Concepts)

Key Parameters
Concept Purpose
Covered
Slicing [::] Accessing a subset of the array based on ranges. start, stop, step
Boolean
Selecting elements based on a logical condition array. (Boolean mask)
Indexing
Selecting non-contiguous elements using a list/array of
Fancy Indexing (Array of indices)
indices.
Broadcasting Defines how NumPy handles arrays with different (Shape alignment
Concept shapes during arithmetic operations. rules)
Used to increase the dimension of an existing array
[Link] N/A
(e.g., turning a 1D array into a column/row vector).

Code: Indexing and Broadcasting

print("\n--- 5. Indexing and Broadcasting ---")

# Data
index_arr = [Link](27).reshape(3, 3, 3)
print(f"3D Array (3x3x3): \n{index_arr[0]}") # Showing only first slice

# Slicing [::]
slice1 = index_arr[1, 0:2, ::2] # Slice 1, rows 0-1, columns 0 and 2
print(f"Slicing [1, 0:2, ::2]: {slice1}")

# Boolean Indexing
bool_arr = index_arr[0]
mask = bool_arr % 2 == 0 # True for even numbers
even_numbers = bool_arr[mask]
print(f"Boolean Indexing (Even numbers from slice 0): {even_numbers}")

# Fancy Indexing
row_indices = [0, 2, 1]
col_indices = [1, 0, 2]
fancy_selection = index_arr[0, row_indices, col_indices]
print(f"Fancy Indexing (Specific elements): {fancy_selection}")

# Broadcasting Concept (Add a 1D array to each row of a 2D array)


A = [Link]([[1, 2, 3], [4, 5, 6]]) # Shape (2, 3)
B = [Link]([10, 20, 30]) # Shape (3,) -> Broadcasts to (2, 3)
C=A+B
print(f"Broadcasting A + B: \n{C}")

# [Link]
A_col_vector = A[:, [Link], 1] # Selects column 1, adds a new axis
print(f"[Link] shape: {A_col_vector.shape} (Makes it a column vector)")

Conclusion

NumPy's power lies in its vectorization capabilities, enabling high-performance operations


on the ndarray. Mastering functions like reshape, concatenate, mean with the axis parameter,
and advanced indexing methods (Boolean, Fancy, and the concept of Broadcasting) are
fundamental requirements for any data science role.
Experiment -3
Aim:- Study of Pandas Library

Objective:-

To study and practically implement the 28 most critical functions and methods in the Pandas
library, focusing on efficient data loading, cleaning, manipulation, aggregation, and analysis
using DataFrame and Series objects.

Theory

Pandas is built on NumPy and provides high-level data structures and tools designed to make
data analysis fast, easy, and expressive. The primary data structures are the DataFrame (a
2D labeled array with potentially heterogeneous columns) and the Series (a 1D labeled
array). Mastery of its methods is essential for data wrangling and preprocessing.

I. Data Structures, I/O, and Attributes (6 Concepts)

Concept/Function Purpose Key Parameters Covered


[Link]() Creates a DataFrame. data, index, columns
filepath_or_buffer, sep, header,
pd.read_csv() Loads data from a CSV file.
index_col
[Link]() / [Link]() Views the top or bottom N rows. n
[Link] / Attributes to check dimensions and data
N/A (Read-only attributes)
[Link] types.
Prints a summary of the DataFrame
[Link]() N/A
(non-null counts, types).
df.to_csv() Writes the DataFrame to a CSV file. path_or_buf, index

Code: Structures, I/O, Attributes

import pandas as pd
import numpy as np

# Create synthetic data


data = {
'EmployeeID': [Link](101, 111),
'Department': ['HR', 'IT', 'Finance', 'HR', 'IT', 'Sales', 'IT', 'Finance', 'HR', 'Sales'],
'Salary': [60000, 85000, 70000, 62000, 90000, 75000, 88000, 71000, 61000, 76000],
'YearsExp': [2, 5, 3, 2, 6, 4, 5, 3, 2, 4],
'Status': ['Active'] * 9 + [[Link]] # Added NaN for cleaning demo
}
df = [Link](data)

print("--- 1. Data Structures, I/O, and Attributes ---")


print(f"1. DataFrame created, Shape: {[Link]}")
print("2. [Link]:\n", [Link])
print("3. [Link]() summary:")
[Link](verbose=False, memory_usage=False)
# Note: For real I/O, uncomment the line below:
# df_loaded = pd.read_csv('[Link]', sep=',', index_col='EmployeeID')

II. Indexing, Selection, and Filtering (5 Concepts)


Function/Concept Purpose Key Parameters Covered
Label-based indexing (rows/cols by
[Link][] [row_label/condition, col_label]
name).
Integer-based indexing (rows/cols by [row_position_slice,
[Link][]
position). col_position_slice]
Column selection and Boolean
df[] ['col_name'], [condition]
masking.
[Link]() Filters data using a string expression. expr (the filtering condition string)
df.reset_index() Resets the index to default integers. drop, inplace

Code: Indexing, Selection, Filtering

print("\n--- 2. Indexing, Selection, and Filtering ---")


# 1. [Link][]: Selecting rows where Dept is IT and only Salary column
loc_data = [Link][df['Department'] == 'IT', ['EmployeeID', 'Salary']]
print(f"1. [Link][] (IT Salaries):\n{loc_data.head(3)}")

# 2. [Link][]: Selecting first 5 rows and columns 2 and 3


iloc_data = [Link][:5, [2, 3]]
print(f"2. [Link][] (First 5 rows, Salary/YearsExp):\n{iloc_data}")
# 3. [Link](): Filtering data using a string condition
query_data = [Link]('Salary > 80000 and YearsExp >= 5')
print(f"3. [Link]() (High Earners):\n{query_data}")

III. Data Cleaning and Handling Missing Values (5 Functions)

Function Purpose Key Parameters Covered


Returns a boolean DataFrame indicating
[Link]() N/A
missing values.
axis (0 or 1), how ('any' or
[Link]() Removes rows/columns with missing data.
'all'), inplace
Fills missing values with a specified value, method ('ffill'/'bfill'),
[Link]()
method or value. inplace
to_replace, value, regex,
[Link]() Replaces specific values in the DataFrame.
inplace
pd.to_datetime() Converts argument to datetime objects. arg, format, errors
Code: Data Cleaning

print("\n--- 3. Data Cleaning and Handling Missing Values ---")


print(f"Missing Values before: {df['Status'].isna().sum()}")

# 1. [Link](): Filling the single NaN value in 'Status' column


df['Status'].fillna(value='Inactive', inplace=True)
print(f"Missing Values after fillna: {df['Status'].isna().sum()}")

# 2. [Link](): Replacing a specific categorical value


df['Department'].replace(to_replace='HR', value='Human Resources', inplace=True)
print("Department replacement done.")

# 3. pd.to_datetime (Demonstration, assuming a date column)


dates = [Link](['2023-01-01', '2023-01-02', 'Invalid Date'])
cleaned_dates = pd.to_datetime(dates, format='mixed', errors='coerce')
print(f"4. pd.to_datetime (with errors='coerce'):\n{cleaned_dates}")

IV. Aggregation, Grouping, and Manipulation (11 Functions)

Function/Concept Purpose Key Parameters Covered


Groups data based on one or more
[Link]() by, axis, as_index
columns.
Performs multiple aggregation functions func (list or dict of
.agg()
simultaneously. functions)
.count(), .sum(),
Basic statistical aggregations. axis
.mean()
Joins DataFrames based on common right, how
[Link]()
columns. ('inner'/'left'/'right'), on
values, index, columns,
df.pivot_table() Creates a spreadsheet-style pivot table.
aggfunc
df.sort_values() Sorts by column values. by, axis, ascending, inplace
Applies a function along an axis
[Link]() func, axis
(row/column).
df['col'].unique() Returns unique values in a Series. N/A
[Link]() Removes rows or columns. labels, axis, inplace

Code: Aggregation, Grouping, Manipulation

print("\n--- 4. Aggregation, Grouping, and Manipulation ---")

# 1. [Link]() and .agg()


grouped_data = [Link]('Department').agg(
Avg_Salary=('Salary', 'mean'),
Total_Employees=('EmployeeID', 'count'),
Max_Exp=('YearsExp', 'max')
).round(2)
print(f"1. Groupby and Aggregation:\n{grouped_data}")

# 2. df.sort_values()
sorted_df = df.sort_values(by=['YearsExp', 'Salary'], ascending=[False, True])
print(f"2. Sort Values (Top 3 by Exp):\n{sorted_df[['YearsExp', 'Salary']].head(3)}")

# 3. [Link]()
def bonus_calc(row):
return row['Salary'] * 0.10 if row['YearsExp'] >= 5 else 0

df['Bonus'] = [Link](bonus_calc, axis=1)


print(f"3. [Link]() (Bonus Column added):\n{df[['Salary', 'YearsExp', 'Bonus']].head()}")

# 4. [Link]() - Demonstration (requires another DataFrame)


df_dept = [Link]({
'Department': ['IT', 'Human Resources', 'Sales'],
'Budget': [500000, 200000, 300000]
})

merged_df = [Link](df_dept, on='Department', how='left')


print(f"4. [Link]() (with Budget):\n{merged_df[['Department', 'Budget']].head(3)}")

# 5. df.pivot_table()
pivot_table = pd.pivot_table(
df,
values='Salary',
index='Department',
aggfunc='mean'
)
print(f"5. df.pivot_table (Avg Salary):\n{pivot_table}")

# 6. df['col'].unique()
unique_depts = df['Department'].unique()
print(f"6. Unique Departments: {unique_depts}")
Conclusion

The Pandas library provides essential tools for the initial stages of the data science workflow.
Proficiency in using methods like loc and iloc for precision indexing, understanding the
power of groupby and agg for summarization, and knowing how to handle data imperfections
using fillna and dropna are foundational. The parameters covered (e.g., axis, how, inplace,
by) are crucial for controlling the execution and output of these methods effectively.
Experiment -4
Aim:-Write a programme in Python to predict the class of the flower based on
available attributes

Outcome:
Must be able to predict the class (species) of a flower when unseen attribute values are
given.

Objectives:

 Understand the relationship between input features and class labels.

 Learn how to apply a classification algorithm to multiclass data.

 Evaluate model performance using appropriate metrics.

Nomenclature, Theory with Self-Assessment Questionnaire

 Nomenclature:

Symbol Description

X Input Features (attributes)

Y Response Variable (Class label)

A Bias term

b Weights associated with each input feature

 Solution:

Classification is a supervised learning technique used to categorize data into predefined


classes. For the Iris dataset:

 Features: Sepal length, sepal width, petal length, petal width.

 Target: Species (Setosa, Versicolor, Virginica).

How it works:
Logistic Regression models the probability that a given input point belongs to a particular
class using the logistic (sigmoid) function. For multiple classes, multinomial logistic
regression is used. The decision boundary is found that best separates the classes.

Scatterplots and pair plots help visualize class separability. Evaluation is done using
accuracy, confusion matrix, and classification reports.
 Assumptions:
 Data should have minimal multicollinearity.

 Classes are linearly separable (for logistic regression).

 Observations are independent.

 The relationship between the log-odds and predictors is linear.

 Dependencies:

 Numpy

 Pandas

 Matplotlib

 Seaborn

 Scikit-learn

Code/Pseudo Code

import numpy as np

import pandas as pd

import [Link] as plt

import seaborn as sns

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

from [Link] import accuracy_score, classification_report, confusion_matrix

# Load Iris dataset

iris = datasets.load_iris()

X = [Link]

y = [Link]

# Split data into train/test

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

# Create logistic regression model


model = LogisticRegression(max_iter=200)

# Train model

[Link](X_train, y_train)

# Predict on test data

y_pred = [Link](X_test)

# Evaluation

print("Accuracy:", accuracy_score(y_test, y_pred))

print("\nClassification Report:\n", classification_report(y_test, y_pred))

print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))

 Results
 Test Case:

Sepal Length Sepal Width Petal Length Petal Width Predicted Class

5.1 3.5 1.4 0.2 Setosa

6.5 3.0 5.2 2.0 Virginica


 Result Analysis:
 Advantages: Simple, interpretable, good for baseline models.

 Issues: Limited to linearly separable classes, may underperform on complex


datasets.

 References:
 Fisher, R.A. (1936). “The use of multiple measurements in taxonomic problems.”

 Scikit-learn Documentation: Logistic Regression

 UCI Machine Learning Repository: Iris Dataset

 Quiz & Viva Questions


 Quiz:
• Which of these is a classification algorithm?
(a) K-Means
(b) Logistic Regression
(c) Linear Regression
(d) None of the above

• Which metric is appropriate for evaluating a classification model?


(a) Mean Squared Error
(b) R-Squared
(c) Accuracy
(d) Sum of Squared Residuals

 Viva:
• What is classification?
• What is the difference between regression and classification?
• What is logistic regression used for?
• How do you interpret the confusion matrix?
Experiment -5
Aim:- Write a programme in Python to Loan Approval Prediction Using Logistic
Regression
Outcome
Must be able to predict whether a loan application will be approved or rejected based on
applicant data such as income, credit score, employment status, etc.

Objectives
 Understand the relationship between applicant attributes (independent variables) and
the loan approval decision (response variable).
 Learn how to apply Logistic Regression for binary classification.

Nomenclature, Theory with Self-Assessment Questionnaire

 Nomenclature
Symbol Description

Y Response Variable (0 = Not Approved, 1 = Approved)

X Input Data (features like income, credit score, etc.)

b₀ Bias/intercept

b₁, b₂, ... Weights associated with input features

 Solution
Logistic Regression is a statistical method used to model a binary outcome. Unlike Linear
Regression, Logistic Regression predicts the probability that a given input point belongs to a
particular class.
The logistic regression equation is:

 The output is always between 0 and 1, representing probability.


 If the predicted probability is greater than 0.5, the outcome is classified as Approved;
otherwise, Not Approved.
 The decision boundary can be adjusted based on business requirements.
A confusion matrix, accuracy, precision, recall, and ROC curve are used to evaluate
performance.
 Assumptions
 Binary outcome: Response variable must be binary.
 Independent observations: Samples must be independent.
 Little or no multicollinearity: Input features should not be highly correlated.
 Linear relationship: There should be a linear relationship between the log odds
and input features.
 Dependencies
 Numpy
 Pandas
 Matplotlib
 Sklearn

Code / Pseudo Code


import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score, classification_report
from [Link] import LabelEncoder, StandardScaler

# --- 1. Data Setup (Using Synthetic Data for a self-contained example) ---
[Link](42)
n_samples = 300
data = [Link]({
'Gender': [Link](['Male', 'Female', [Link]], n_samples, p=[0.45, 0.45,
0.1]),
'Married': [Link](['Yes', 'No'], n_samples),
'Credit_History': [Link]([1.0, 0.0, [Link]], n_samples, p=[0.7, 0.2, 0.1]),
'ApplicantIncome': [Link](1500, 60000, n_samples),
'LoanAmount': [Link](100, 700, n_samples),
'Loan_Status': [Link](['Y', 'N'], n_samples, p=[0.7, 0.3]) # Target
})

print("Data Loaded (Synthetic). Shape:", [Link])


# --- 2. Simple Preprocessing ---

# 2a. Handle Missing Values (Imputation)


# Fill categorical missing values with the mode
data['Gender'].fillna(data['Gender'].mode()[0], inplace=True)
# Fill numerical missing values (Credit_History) with the mode (as it's
binary/categorical)
data['Credit_History'].fillna(data['Credit_History'].mode()[0], inplace=True)
# Fill numerical missing values (LoanAmount) with the median
data['LoanAmount'].fillna(data['LoanAmount'].median(), inplace=True)

# 2b. Feature Encoding


# Convert categorical features to numeric using One-Hot Encoding (pd.get_dummies)
data_encoded=pd.get_dummies(data, columns=['Gender', 'Married'], drop_first=True)

# Convert Target variable to numeric ('Y'=1, 'N'=0)


data_encoded['Loan_Status'] =
LabelEncoder().fit_transform(data_encoded['Loan_Status'])

# 2c. Define Features (X) and Target (y)


X = data_encoded.drop('Loan_Status', axis=1)
y = data_encoded['Loan_Status']

# Optional but Recommended for Logistic Regression: Scaling Numerical Features


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
X = [Link](X_scaled, columns=[Link])

print("Preprocessing Complete (Missing values imputed, features encoded).")

# --- 3. Data Split ---


X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y)
# --- 4. Model Training ---

print("� Training Logistic Regression Model...")

model = LogisticRegression(random_state=42) # Default hyperparameters

[Link](X_train, y_train)

print("Training Complete.")

# --- 5. Prediction and Evaluation ---


y_pred = [Link](X_test)

print("\n Model Evaluation")


accuracy = accuracy_score(y_test, y_pred)
print(f"**Accuracy Score:** {accuracy:.4f}")

print("\n**Classification Report:**")
print(classification_report(y_test, y_pred, target_names=['N', 'Y']))
 Results
 Test Cases
Input (Income, Credit_Score, Employment_Status) Output (Loan Approved)

50,000, 750, 1 1

20,000, 600, 0 0

 Result Analysis
Advantages:
 Simple to implement and interpret.
 Good baseline for binary classification tasks.
 Issues:
 May underperform if data is not linearly separable.
 Sensitive to outliers and multicollinearity.
 References
 Scikit-learn Logistic Regression Documentation
 Machine Learning Mastery by Jason Brownlee
 Coursera: Machine Learning by Andrew Ng

Quiz & Viva Questions


 Quiz
 Which function is used as the activation function in Logistic Regression?
(a) Linear function
(b) Sigmoid function
(c) ReLU function
(d) Softmax function

 Which metric is NOT suitable for evaluating binary classification?


(a) Mean Squared Error
(b) Accuracy
(c) Precision
(d) Recall
 Viva
 What is Logistic Regression?
 How is Logistic Regression different from Linear Regression?
 What is the significance of the sigmoid function?
 What is a confusion matrix?

You might also like