sppu-mlpracticals 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sppu_mlpracticals-0.1.0/PKG-INFO +9 -0
- sppu_mlpracticals-0.1.0/README.md +0 -0
- sppu_mlpracticals-0.1.0/pyproject.toml +18 -0
- sppu_mlpracticals-0.1.0/setup.cfg +4 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/__init__.py +5 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/core.py +42 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/__init__.py +0 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/dmv1.py +96 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/dmv2.py +105 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/dmv3.py +163 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/dmv4.py +153 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/dmv5.py +148 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/dmv6.py +141 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/dmv7.py +161 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml1A.py +98 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml1B.py +105 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml2A.py +231 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml2B.py +206 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml3A.py +107 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml3B.py +117 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml4A.py +87 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml4B.py +104 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml5A.py +95 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml5B.py +136 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml6A.py +164 -0
- sppu_mlpracticals-0.1.0/src/mlpracticals/practicals/ml6B.py +115 -0
- sppu_mlpracticals-0.1.0/src/sppu_mlpracticals.egg-info/PKG-INFO +9 -0
- sppu_mlpracticals-0.1.0/src/sppu_mlpracticals.egg-info/SOURCES.txt +28 -0
- sppu_mlpracticals-0.1.0/src/sppu_mlpracticals.egg-info/dependency_links.txt +1 -0
- sppu_mlpracticals-0.1.0/src/sppu_mlpracticals.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sppu-mlpracticals
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python practicals for SPPU AI and Data Science students
|
|
5
|
+
Author: Abhijeet Upadhye
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: machine-learning,python,SPPU,AI,data-science
|
|
8
|
+
Requires-Python: >=3.9
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sppu-mlpracticals"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Python practicals for SPPU AI and Data Science students"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Abhijeet Upadhye" }
|
|
14
|
+
]
|
|
15
|
+
keywords = ["machine-learning", "python", "SPPU", "AI", "data-science"]
|
|
16
|
+
|
|
17
|
+
[tool.setuptools.packages.find]
|
|
18
|
+
where = ["src"]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from importlib import import_module
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
PRACTICALS = {
|
|
5
|
+
"ml1a": "PCA on Wine Dataset",
|
|
6
|
+
"ml1b": "LDA on Iris Dataset",
|
|
7
|
+
"ml2a": "Uber Fare Regression",
|
|
8
|
+
"ml2b": "Diabetes Regression Analysis",
|
|
9
|
+
"ml3a": "SVM on Handwritten Digits",
|
|
10
|
+
"ml3b": "KNN on Social Network Ads",
|
|
11
|
+
"ml4a": "K-Means Clustering on Iris Dataset",
|
|
12
|
+
"ml4b": "K-Medoids Clustering on Credit Card Dataset",
|
|
13
|
+
"ml5a": "Random Forest on Car Safety Dataset",
|
|
14
|
+
"ml5b": "Ensemble Learning on Iris Dataset",
|
|
15
|
+
"ml6a": "Maze Reinforcement Learning",
|
|
16
|
+
"ml6b": "Taxi Reinforcement Learning",
|
|
17
|
+
"dmv1": "Data Loading, Storage and File Formats",
|
|
18
|
+
"dmv2": "Weather Data Analysis using OpenWeatherMap API",
|
|
19
|
+
"dmv3": "Data Cleaning and Preparation",
|
|
20
|
+
"dmv4": "Data Wrangling on Real Estate Market",
|
|
21
|
+
"dmv5": "Data Visualization using Matplotlib",
|
|
22
|
+
"dmv6": "Data Aggregation on Retail Sales Data",
|
|
23
|
+
"dmv7": "Time Series Data Analysis on Stock Market Data",
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get(practical_id):
|
|
28
|
+
key = str(practical_id).strip().lower()
|
|
29
|
+
|
|
30
|
+
if key not in PRACTICALS:
|
|
31
|
+
raise ValueError(
|
|
32
|
+
f"Practical '{practical_id}' not found. "
|
|
33
|
+
f"Available practicals: {', '.join(PRACTICALS.keys())}"
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
module = import_module(f".practicals.{key[0:3]}{key[3].upper()}", package="mlpracticals")
|
|
37
|
+
|
|
38
|
+
return module.PRACTICAL
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def list_practicals():
|
|
42
|
+
return PRACTICALS.copy()
|
|
File without changes
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
PRACTICAL = {
|
|
2
|
+
"id": "dmv1",
|
|
3
|
+
"title": "Data Integration, Cleaning, Analysis and Visualization",
|
|
4
|
+
|
|
5
|
+
"aim": """
|
|
6
|
+
To integrate data from CSV, Excel and JSON files,
|
|
7
|
+
perform data cleaning and analysis, and visualize
|
|
8
|
+
category-wise sales using Python.
|
|
9
|
+
""",
|
|
10
|
+
|
|
11
|
+
"theory": """
|
|
12
|
+
Data Integration is the process of combining data
|
|
13
|
+
collected from different sources into a single dataset.
|
|
14
|
+
Python Pandas provides functions to read data from
|
|
15
|
+
different file formats such as CSV, Excel and JSON.
|
|
16
|
+
|
|
17
|
+
Data Cleaning is performed to improve data quality
|
|
18
|
+
by removing missing values and duplicate records.
|
|
19
|
+
Pandas provides dropna() to remove missing values
|
|
20
|
+
and drop_duplicates() to remove duplicate records.
|
|
21
|
+
|
|
22
|
+
Data Transformation involves modifying existing data
|
|
23
|
+
or creating new columns for further analysis. In this
|
|
24
|
+
practical, a TotalSales column is created by multiplying
|
|
25
|
+
Quantity and Price.
|
|
26
|
+
|
|
27
|
+
Data Analysis is used to understand the dataset and
|
|
28
|
+
extract useful information. Functions such as head(),
|
|
29
|
+
describe() and sum() help display records, statistical
|
|
30
|
+
information and total sales.
|
|
31
|
+
|
|
32
|
+
Grouping is performed using the groupby() function.
|
|
33
|
+
It groups records based on a particular column and
|
|
34
|
+
calculates the total sales for each category.
|
|
35
|
+
|
|
36
|
+
Data Visualization is the graphical representation
|
|
37
|
+
of data. Matplotlib is used to create a bar chart
|
|
38
|
+
that displays sales for different categories.
|
|
39
|
+
This makes it easier to compare category-wise sales.
|
|
40
|
+
""",
|
|
41
|
+
|
|
42
|
+
"formula": """
|
|
43
|
+
Total Sales:
|
|
44
|
+
|
|
45
|
+
TotalSales = Quantity × Price
|
|
46
|
+
|
|
47
|
+
Category-wise Sales:
|
|
48
|
+
|
|
49
|
+
Category Sales = Σ TotalSales
|
|
50
|
+
|
|
51
|
+
Overall Sales:
|
|
52
|
+
|
|
53
|
+
Total Sales = Σ TotalSales
|
|
54
|
+
""",
|
|
55
|
+
|
|
56
|
+
"algorithm": [
|
|
57
|
+
"Import Pandas and Matplotlib libraries.",
|
|
58
|
+
"Load CSV, Excel and JSON files.",
|
|
59
|
+
"Combine all datasets using the concat() function.",
|
|
60
|
+
"Remove missing values and duplicate records.",
|
|
61
|
+
"Create a TotalSales column using Quantity and Price.",
|
|
62
|
+
"Display the first five records using head().",
|
|
63
|
+
"Display statistical information using describe().",
|
|
64
|
+
"Calculate the overall sales using sum().",
|
|
65
|
+
"Group sales records by Category using groupby().",
|
|
66
|
+
"Create a bar chart to visualize category-wise sales.",
|
|
67
|
+
"Display the bar chart."
|
|
68
|
+
],
|
|
69
|
+
|
|
70
|
+
"code": '''
|
|
71
|
+
import pandas as pd
|
|
72
|
+
import matplotlib.pyplot as plt
|
|
73
|
+
|
|
74
|
+
csv = pd.read_csv("sales.csv")
|
|
75
|
+
excel = pd.read_excel("sales.xlsx")
|
|
76
|
+
json = pd.read_json("sales.json")
|
|
77
|
+
|
|
78
|
+
sales = pd.concat([csv, excel, json], ignore_index=True)
|
|
79
|
+
|
|
80
|
+
sales = sales.dropna().drop_duplicates()
|
|
81
|
+
|
|
82
|
+
sales["TotalSales"] = sales["Quantity"] * sales["Price"]
|
|
83
|
+
|
|
84
|
+
print(sales.head())
|
|
85
|
+
print(sales.describe())
|
|
86
|
+
|
|
87
|
+
print("Total Sales:", sales["TotalSales"].sum())
|
|
88
|
+
|
|
89
|
+
category_sales = sales.groupby("Category")["TotalSales"].sum()
|
|
90
|
+
print(category_sales)
|
|
91
|
+
|
|
92
|
+
category_sales.plot(kind="bar", title="Sales by Category")
|
|
93
|
+
plt.ylabel("Total Sales")
|
|
94
|
+
plt.show()
|
|
95
|
+
'''
|
|
96
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
PRACTICAL = {
|
|
2
|
+
"id": "dmv2",
|
|
3
|
+
"title": "Weather Data Analysis using OpenWeatherMap API",
|
|
4
|
+
|
|
5
|
+
"aim": """
|
|
6
|
+
To retrieve weather data from the OpenWeatherMap API,
|
|
7
|
+
perform data processing and analyze weather conditions
|
|
8
|
+
using Python and visualize the results.
|
|
9
|
+
""",
|
|
10
|
+
|
|
11
|
+
"theory": """
|
|
12
|
+
A Web API (Application Programming Interface) allows
|
|
13
|
+
applications to communicate and exchange data over
|
|
14
|
+
the internet.
|
|
15
|
+
|
|
16
|
+
OpenWeatherMap is a weather service that provides
|
|
17
|
+
real-time weather information through its API.
|
|
18
|
+
It provides information such as temperature, humidity,
|
|
19
|
+
wind speed and weather conditions.
|
|
20
|
+
|
|
21
|
+
The Requests library is used to send HTTP requests
|
|
22
|
+
to the API and retrieve weather data in JSON format.
|
|
23
|
+
|
|
24
|
+
The JSON response is converted into a Python dictionary
|
|
25
|
+
and relevant weather attributes are extracted.
|
|
26
|
+
|
|
27
|
+
Pandas is used to organize and process the retrieved
|
|
28
|
+
weather information. Data analysis is performed to
|
|
29
|
+
calculate temperature statistics and examine weather
|
|
30
|
+
conditions.
|
|
31
|
+
|
|
32
|
+
Matplotlib is used to visualize weather attributes
|
|
33
|
+
through bar charts. These visualizations make it easier
|
|
34
|
+
to understand and compare weather conditions.
|
|
35
|
+
""",
|
|
36
|
+
|
|
37
|
+
"formula": """
|
|
38
|
+
Average Temperature:
|
|
39
|
+
|
|
40
|
+
Average = Sum of Temperatures / Number of Records
|
|
41
|
+
|
|
42
|
+
Temperature Conversion:
|
|
43
|
+
|
|
44
|
+
Celsius = Kelvin - 273.15
|
|
45
|
+
""",
|
|
46
|
+
|
|
47
|
+
"algorithm": [
|
|
48
|
+
"Import Requests, Pandas and Matplotlib libraries.",
|
|
49
|
+
"Register on OpenWeatherMap and obtain an API key.",
|
|
50
|
+
"Enter the city name and API key.",
|
|
51
|
+
"Send a request to the OpenWeatherMap API.",
|
|
52
|
+
"Retrieve weather information in JSON format.",
|
|
53
|
+
"Extract temperature, humidity and wind speed.",
|
|
54
|
+
"Convert temperature from Kelvin to Celsius.",
|
|
55
|
+
"Store the weather information in a DataFrame.",
|
|
56
|
+
"Calculate average temperature and display weather details.",
|
|
57
|
+
"Create bar charts to visualize weather attributes."
|
|
58
|
+
],
|
|
59
|
+
|
|
60
|
+
"code": '''
|
|
61
|
+
import requests
|
|
62
|
+
import pandas as pd
|
|
63
|
+
import matplotlib.pyplot as plt
|
|
64
|
+
|
|
65
|
+
api_key = "YOUR_API_KEY"
|
|
66
|
+
city = "Pune"
|
|
67
|
+
|
|
68
|
+
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
|
|
69
|
+
|
|
70
|
+
response = requests.get(url)
|
|
71
|
+
data = response.json()
|
|
72
|
+
|
|
73
|
+
if response.status_code == 200:
|
|
74
|
+
temperature = data["main"]["temp"] - 273.15
|
|
75
|
+
humidity = data["main"]["humidity"]
|
|
76
|
+
wind_speed = data["wind"]["speed"]
|
|
77
|
+
weather = data["weather"][0]["description"]
|
|
78
|
+
|
|
79
|
+
df = pd.DataFrame({
|
|
80
|
+
"City": [city],
|
|
81
|
+
"Temperature": [temperature],
|
|
82
|
+
"Humidity": [humidity],
|
|
83
|
+
"Wind Speed": [wind_speed],
|
|
84
|
+
"Weather": [weather]
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
print(df)
|
|
88
|
+
|
|
89
|
+
print("Average Temperature:", df["Temperature"].mean())
|
|
90
|
+
print("Maximum Temperature:", df["Temperature"].max())
|
|
91
|
+
print("Minimum Temperature:", df["Temperature"].min())
|
|
92
|
+
|
|
93
|
+
df[["Temperature", "Humidity", "Wind Speed"]].plot(
|
|
94
|
+
kind="bar",
|
|
95
|
+
figsize=(8, 5),
|
|
96
|
+
title="Weather Analysis"
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
plt.ylabel("Values")
|
|
100
|
+
plt.show()
|
|
101
|
+
|
|
102
|
+
else:
|
|
103
|
+
print("Error:", data["message"])
|
|
104
|
+
'''
|
|
105
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
PRACTICAL = {
|
|
2
|
+
"id": "dmv3",
|
|
3
|
+
"title": "Data Cleaning and Preparation on Customer Churn Dataset",
|
|
4
|
+
|
|
5
|
+
"aim": """
|
|
6
|
+
To clean and prepare the Telecom Customer Churn dataset
|
|
7
|
+
by handling missing values, removing duplicates, treating
|
|
8
|
+
outliers, performing feature engineering, scaling data
|
|
9
|
+
and splitting it into training and testing datasets.
|
|
10
|
+
""",
|
|
11
|
+
|
|
12
|
+
"theory": """
|
|
13
|
+
Data Cleaning is the process of identifying and correcting
|
|
14
|
+
errors, missing values, duplicate records and inconsistencies
|
|
15
|
+
in a dataset. It improves data quality and makes the data
|
|
16
|
+
suitable for further analysis.
|
|
17
|
+
|
|
18
|
+
The Telecom Customer Churn dataset contains customer
|
|
19
|
+
information such as gender, tenure, monthly charges,
|
|
20
|
+
contract type and churn status. Churn indicates whether
|
|
21
|
+
a customer has discontinued the company's services.
|
|
22
|
+
|
|
23
|
+
Data Exploration is performed using functions such as
|
|
24
|
+
head(), info() and isnull().sum() to understand the
|
|
25
|
+
structure and identify missing values.
|
|
26
|
+
|
|
27
|
+
Missing values can be handled using mean, median or mode
|
|
28
|
+
imputation. Duplicate records are removed using the
|
|
29
|
+
drop_duplicates() function.
|
|
30
|
+
|
|
31
|
+
Data Transformation involves converting columns into
|
|
32
|
+
appropriate data types and standardizing inconsistent
|
|
33
|
+
categorical values.
|
|
34
|
+
|
|
35
|
+
Outlier Detection identifies unusually high or low values.
|
|
36
|
+
The Interquartile Range (IQR) method can be used to detect
|
|
37
|
+
outliers in numerical columns.
|
|
38
|
+
|
|
39
|
+
Feature Engineering is the process of creating new
|
|
40
|
+
features from existing columns. In this practical, a
|
|
41
|
+
TenureGroup column is created based on customer tenure.
|
|
42
|
+
|
|
43
|
+
Data Encoding converts categorical values into numerical
|
|
44
|
+
representations using one-hot encoding.
|
|
45
|
+
|
|
46
|
+
Feature Scaling brings numerical features to a similar
|
|
47
|
+
scale using StandardScaler.
|
|
48
|
+
|
|
49
|
+
Finally, the dataset is divided into training and testing
|
|
50
|
+
sets using train_test_split(). The cleaned dataset is
|
|
51
|
+
also exported as a CSV file for future analysis.
|
|
52
|
+
""",
|
|
53
|
+
|
|
54
|
+
"formula": """
|
|
55
|
+
Interquartile Range:
|
|
56
|
+
|
|
57
|
+
IQR = Q3 - Q1
|
|
58
|
+
|
|
59
|
+
Lower Bound:
|
|
60
|
+
|
|
61
|
+
Q1 - 1.5 × IQR
|
|
62
|
+
|
|
63
|
+
Upper Bound:
|
|
64
|
+
|
|
65
|
+
Q3 + 1.5 × IQR
|
|
66
|
+
|
|
67
|
+
Standardization:
|
|
68
|
+
|
|
69
|
+
Z = (X - μ) / σ
|
|
70
|
+
|
|
71
|
+
Where:
|
|
72
|
+
X = Original value
|
|
73
|
+
μ = Mean
|
|
74
|
+
σ = Standard deviation
|
|
75
|
+
""",
|
|
76
|
+
|
|
77
|
+
"algorithm": [
|
|
78
|
+
"Import the required Python libraries.",
|
|
79
|
+
"Load the Telecom_Customer_Churn.csv dataset.",
|
|
80
|
+
"Explore the dataset using head(), info() and describe().",
|
|
81
|
+
"Identify and handle missing values.",
|
|
82
|
+
"Remove duplicate records.",
|
|
83
|
+
"Convert columns into appropriate data types.",
|
|
84
|
+
"Standardize categorical values.",
|
|
85
|
+
"Detect and handle outliers using the IQR method.",
|
|
86
|
+
"Create a new feature called TenureGroup.",
|
|
87
|
+
"Encode categorical columns using one-hot encoding.",
|
|
88
|
+
"Scale numerical features using StandardScaler.",
|
|
89
|
+
"Split the dataset into training and testing sets.",
|
|
90
|
+
"Export the cleaned dataset as a CSV file."
|
|
91
|
+
],
|
|
92
|
+
|
|
93
|
+
"code": '''
|
|
94
|
+
import pandas as pd
|
|
95
|
+
from sklearn.preprocessing import StandardScaler
|
|
96
|
+
from sklearn.model_selection import train_test_split
|
|
97
|
+
|
|
98
|
+
df = pd.read_csv("Telecom_Customer_Churn.csv")
|
|
99
|
+
|
|
100
|
+
print(df.head())
|
|
101
|
+
print(df.info())
|
|
102
|
+
print(df.isnull().sum())
|
|
103
|
+
|
|
104
|
+
df = df.drop_duplicates()
|
|
105
|
+
|
|
106
|
+
df = df.replace(" ", pd.NA)
|
|
107
|
+
|
|
108
|
+
df["TotalCharges"] = pd.to_numeric(
|
|
109
|
+
df["TotalCharges"], errors="coerce"
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
df["TotalCharges"] = df["TotalCharges"].fillna(
|
|
113
|
+
df["TotalCharges"].median()
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
df = df.fillna(df.mode().iloc[0])
|
|
117
|
+
|
|
118
|
+
for col in df.select_dtypes(include="object").columns:
|
|
119
|
+
df[col] = df[col].str.strip().str.lower()
|
|
120
|
+
|
|
121
|
+
for col in ["tenure", "MonthlyCharges", "TotalCharges"]:
|
|
122
|
+
Q1 = df[col].quantile(0.25)
|
|
123
|
+
Q3 = df[col].quantile(0.75)
|
|
124
|
+
IQR = Q3 - Q1
|
|
125
|
+
|
|
126
|
+
df[col] = df[col].clip(
|
|
127
|
+
lower=Q1 - 1.5 * IQR,
|
|
128
|
+
upper=Q3 + 1.5 * IQR
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
df["TenureGroup"] = pd.cut(
|
|
132
|
+
df["tenure"],
|
|
133
|
+
bins=[-1, 12, 36, 72],
|
|
134
|
+
labels=["New", "Medium", "Long"]
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
df["Churn"] = df["Churn"].map({"no": 0, "yes": 1})
|
|
138
|
+
|
|
139
|
+
df = df.drop(columns=["customerID"], errors="ignore")
|
|
140
|
+
|
|
141
|
+
df = pd.get_dummies(df, drop_first=True, dtype=int)
|
|
142
|
+
|
|
143
|
+
X = df.drop("Churn", axis=1)
|
|
144
|
+
y = df["Churn"]
|
|
145
|
+
|
|
146
|
+
scaler = StandardScaler()
|
|
147
|
+
X_scaled = scaler.fit_transform(X)
|
|
148
|
+
|
|
149
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
150
|
+
X_scaled, y, test_size=0.2, random_state=42,
|
|
151
|
+
stratify=y
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
cleaned_data = pd.DataFrame(X_scaled, columns=X.columns)
|
|
155
|
+
cleaned_data["Churn"] = y.values
|
|
156
|
+
|
|
157
|
+
cleaned_data.to_csv("Cleaned_Customer_Churn.csv", index=False)
|
|
158
|
+
|
|
159
|
+
print("Training Data:", X_train.shape)
|
|
160
|
+
print("Testing Data:", X_test.shape)
|
|
161
|
+
print("Cleaned dataset saved successfully.")
|
|
162
|
+
'''
|
|
163
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
PRACTICAL = {
|
|
2
|
+
"id": "dmv4",
|
|
3
|
+
"title": "Data Wrangling on Real Estate Market",
|
|
4
|
+
|
|
5
|
+
"aim": """
|
|
6
|
+
To perform data wrangling on real estate data by cleaning
|
|
7
|
+
column names, handling missing values, filtering records,
|
|
8
|
+
encoding categorical variables, aggregating data and
|
|
9
|
+
identifying outliers using Python.
|
|
10
|
+
""",
|
|
11
|
+
|
|
12
|
+
"theory": """
|
|
13
|
+
Data Wrangling is the process of cleaning, transforming
|
|
14
|
+
and organizing raw data into a structured format suitable
|
|
15
|
+
for analysis and further processing.
|
|
16
|
+
|
|
17
|
+
The Real Estate dataset contains information about
|
|
18
|
+
properties, including location, property type, area,
|
|
19
|
+
number of bedrooms and sale prices.
|
|
20
|
+
|
|
21
|
+
Data Importing is performed using Pandas read_csv()
|
|
22
|
+
to load the dataset into a DataFrame.
|
|
23
|
+
|
|
24
|
+
Column Cleaning involves removing unnecessary spaces
|
|
25
|
+
and special characters from column names to make them
|
|
26
|
+
consistent and easy to use.
|
|
27
|
+
|
|
28
|
+
Missing values are handled using methods such as
|
|
29
|
+
dropna() and fillna(). Numerical missing values can
|
|
30
|
+
be replaced with the median, while categorical missing
|
|
31
|
+
values can be replaced with the mode.
|
|
32
|
+
|
|
33
|
+
Data Filtering is used to select records based on
|
|
34
|
+
specific conditions such as property type, location
|
|
35
|
+
or sale price.
|
|
36
|
+
|
|
37
|
+
Categorical Encoding converts text-based values into
|
|
38
|
+
numerical values. One-hot encoding creates separate
|
|
39
|
+
columns for different categories.
|
|
40
|
+
|
|
41
|
+
Data Aggregation groups records using groupby() and
|
|
42
|
+
calculates summary statistics such as average property
|
|
43
|
+
prices for different locations.
|
|
44
|
+
|
|
45
|
+
Outlier Detection identifies unusually high or low
|
|
46
|
+
property prices using the Interquartile Range (IQR)
|
|
47
|
+
method.
|
|
48
|
+
|
|
49
|
+
These operations help prepare real estate data for
|
|
50
|
+
accurate analysis and future machine learning models.
|
|
51
|
+
""",
|
|
52
|
+
|
|
53
|
+
"formula": """
|
|
54
|
+
Interquartile Range:
|
|
55
|
+
|
|
56
|
+
IQR = Q3 - Q1
|
|
57
|
+
|
|
58
|
+
Lower Bound:
|
|
59
|
+
|
|
60
|
+
Q1 - 1.5 × IQR
|
|
61
|
+
|
|
62
|
+
Upper Bound:
|
|
63
|
+
|
|
64
|
+
Q3 + 1.5 × IQR
|
|
65
|
+
|
|
66
|
+
Average Property Price:
|
|
67
|
+
|
|
68
|
+
Average = Total Property Prices / Number of Properties
|
|
69
|
+
""",
|
|
70
|
+
|
|
71
|
+
"algorithm": [
|
|
72
|
+
"Import the required Python libraries.",
|
|
73
|
+
"Load the RealEstate_Prices.csv dataset.",
|
|
74
|
+
"Display the first five records and dataset information.",
|
|
75
|
+
"Clean column names by removing spaces and special characters.",
|
|
76
|
+
"Identify and handle missing values.",
|
|
77
|
+
"Remove duplicate records.",
|
|
78
|
+
"Filter properties based on specific conditions.",
|
|
79
|
+
"Encode categorical variables using one-hot encoding.",
|
|
80
|
+
"Group properties by neighborhood and calculate average prices.",
|
|
81
|
+
"Identify and handle outliers using the IQR method.",
|
|
82
|
+
"Display the processed dataset and aggregated results."
|
|
83
|
+
],
|
|
84
|
+
|
|
85
|
+
"code": '''
|
|
86
|
+
import pandas as pd
|
|
87
|
+
|
|
88
|
+
df = pd.read_csv("RealEstate_Prices.csv")
|
|
89
|
+
|
|
90
|
+
print(df.head())
|
|
91
|
+
print(df.info())
|
|
92
|
+
print(df.isnull().sum())
|
|
93
|
+
|
|
94
|
+
df.columns = (
|
|
95
|
+
df.columns.str.strip()
|
|
96
|
+
.str.replace(" ", "_")
|
|
97
|
+
.str.replace(r"[^a-zA-Z0-9_]", "", regex=True)
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
df = df.drop_duplicates()
|
|
101
|
+
|
|
102
|
+
for col in df.select_dtypes(include="number").columns:
|
|
103
|
+
df[col] = df[col].fillna(df[col].median())
|
|
104
|
+
|
|
105
|
+
for col in df.select_dtypes(include="object").columns:
|
|
106
|
+
df[col] = df[col].fillna(df[col].mode()[0])
|
|
107
|
+
|
|
108
|
+
print("Cleaned Data:")
|
|
109
|
+
print(df.head())
|
|
110
|
+
|
|
111
|
+
print("Filtered Properties:")
|
|
112
|
+
filtered = df[df["Sale_Price"] > 100000]
|
|
113
|
+
print(filtered.head())
|
|
114
|
+
|
|
115
|
+
df = pd.get_dummies(df, drop_first=True, dtype=int)
|
|
116
|
+
|
|
117
|
+
print("Encoded Data:")
|
|
118
|
+
print(df.head())
|
|
119
|
+
|
|
120
|
+
original = pd.read_csv("RealEstate_Prices.csv")
|
|
121
|
+
original.columns = (
|
|
122
|
+
original.columns.str.strip()
|
|
123
|
+
.str.replace(" ", "_")
|
|
124
|
+
.str.replace(r"[^a-zA-Z0-9_]", "", regex=True)
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
original["Sale_Price"] = pd.to_numeric(
|
|
128
|
+
original["Sale_Price"], errors="coerce"
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
print("Average Price by Neighborhood:")
|
|
132
|
+
print(original.groupby("Neighborhood")["Sale_Price"].mean())
|
|
133
|
+
|
|
134
|
+
Q1 = original["Sale_Price"].quantile(0.25)
|
|
135
|
+
Q3 = original["Sale_Price"].quantile(0.75)
|
|
136
|
+
IQR = Q3 - Q1
|
|
137
|
+
|
|
138
|
+
lower = Q1 - 1.5 * IQR
|
|
139
|
+
upper = Q3 + 1.5 * IQR
|
|
140
|
+
|
|
141
|
+
df_clean = original[
|
|
142
|
+
(original["Sale_Price"] >= lower) &
|
|
143
|
+
(original["Sale_Price"] <= upper)
|
|
144
|
+
]
|
|
145
|
+
|
|
146
|
+
print("Data After Removing Outliers:")
|
|
147
|
+
print(df_clean.head())
|
|
148
|
+
|
|
149
|
+
df_clean.to_csv("Cleaned_RealEstate.csv", index=False)
|
|
150
|
+
|
|
151
|
+
print("Cleaned dataset saved successfully.")
|
|
152
|
+
'''
|
|
153
|
+
}
|