-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric_information.py
More file actions
169 lines (113 loc) · 5.79 KB
/
Copy pathnumeric_information.py
File metadata and controls
169 lines (113 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
def Describing_numeric_column(dataframe,column):
try:
print(column)
print(f"The Basic Description of the column is ", dataframe[column].describe())
print(f"The Median Value for the column is: ", dataframe[column].median())
mean = dataframe[column].mean()
stad_dev = np.std(dataframe[column])
return mean, stad_dev
except KeyError:
print(f"The Mentioned Column {column} does not exist in the Dataframe")
except Exception as e:
print(f"Unexcepted Error Occured: {e}")
finally:
print(f"The Numeric Column Described.")
def calculate_skew_and_plot_kdeplot(dataframe, column):
try:
# Computing Skew
skew_values = dataframe[column].skew()
# print(f"The Skew for the column is: ", skew_values)
if abs(skew_values) < 0.5:
verdict = "Almost Normal Distribution"
elif abs(skew_values) <= 1:
verdict = "⚠️ Moderately Skewed — can be accepted as Normal Distribution"
else:
verdict = "❌ Highly Skewed Distribution"
# Calculating the kdeplot
fig,ax = plt.subplots(figsize = (8,4))
sns.kdeplot(data = dataframe, x = column, color = "green", fill = True, ax= ax)
ax.set_title(f"Distribution of {column} is: ", fontsize = 14)
ax.set_xlabel(column)
ax.set_ylabel("Probability Density")
ax.grid(True, linestyle = "--", alpha = 0.6)
except NameError:
print(f"The Column {column} Does Not Exist in the DataFrame.")
except Exception as e:
print(f"Unexpected Error Occured: {e}")
finally:
print(f"The Numeric Column Described.")
return fig, skew_values, verdict
# CENTRAL FUNCTION
def missing_value_calculation(dataframe, column):
try:
null_values = dataframe[column].isna().sum()
null_values_percentage = round((null_values / dataframe[column].shape[0])*100,2)
if null_values_percentage == 0.0:
Message = "No Null Values Detected"
elif null_values_percentage <= 10.0:
Message = f"{null_values_percentage} percent null values detected. Suggested Treatment: Imputation "
elif null_values_percentage <=35.0:
Message = f"{null_values_percentage} percent null values detected. Suggested Treatment: Imputation Possible"
else:
Message = f"{null_values_percentage} percent null values detected. Suggested Treatment: Drop the Column"
except NameError:
print(f"The Column {column} Does Not Exist in the DataFrame.")
except Exception as e:
print(f"Unexpected Error Occured: {e}")
finally:
print(f"The Numeric Column Described.")
return null_values, null_values_percentage, Message
# CENTRAL FUNCTION --> add a check to identify whether the column is categorical or numeric and then provide the corresponsing missing value imputatio methods
def missing_value_treatment(dataframe, column, select_imputation_method):
# Dropping Missing Values
if select_imputation_method.lower() == "drop":
dataframe[column] = dataframe[column].dropna()
return "Missig Values Dropped"
# Mean Imputation
elif select_imputation_method.lower() == "mean":
dataframe[column] = dataframe[column].fillna(dataframe[column].mean())
return "Mean Imputation Complete"
# Median Imputation
elif select_imputation_method.lower() == "median":
dataframe[column] = dataframe[column].fillna(dataframe[column].median())
return "Median Imputation Complete"
# Mode imputation
elif select_imputation_method.lower() == "mode":
dataframe[column] = dataframe[column].fillna(dataframe[column].mode())
return "Mode Imputation Complete"
# Constant Imputation
elif select_imputation_method.lower() == "constant":
constant = int(input("Enter the constant value for imputation: "))
dataframe[column] = dataframe[column].fillna(constant)
return "Constant Imputation Complete"
# Missing Category Imputation
elif select_imputation_method.lower() == "missing":
dataframe[column] = dataframe[column].fillna("Missing")
return "Missing Imputation Complete"
def outlier_detection(dataframe, column, skew_value, mean, std_dev):
if skew_value <= 1:
Lower_Bound = mean - 3 *std_dev
Upper_Bound = mean + 3 *std_dev
print(Lower_Bound, Upper_Bound)
print("Total Outliers Detected: ",dataframe[(dataframe[column] < Lower_Bound) | (dataframe[column] > Upper_Bound)].shape[0])
Q1, Q2, Q3, IQR = 0.0, 0.0, 0.0, 0.0
new_df = dataframe[(dataframe[column] >= Lower_Bound) & (dataframe[column] <= Upper_Bound)]
else:
# Computing the quatiles
Q1 = np.nanquantile(dataframe[column],0.25)
Q2 = np.nanquantile(dataframe[column],0.5)
Q3 = np.nanquantile(dataframe[column],0.75)
IQR = Q3 - Q1
print(f"The Interquartile Range of the {column} is : ", IQR)
# computing the lower Bound and Upper Bound
Lower_Bound = Q1 - (1.5 * IQR)
Upper_Bound = Q3 + (1.5 * IQR)
print(f"The Lower_Bound of the {column} is : ", Lower_Bound)
print(f"The Upper_Bound of the {column} is : ", Upper_Bound)
print("Total Outliers Detected: ",dataframe[dataframe[column] > Upper_Bound].shape[0])
new_df = dataframe[(dataframe[column] >= Lower_Bound) & (dataframe[column] <= Upper_Bound)]
return Q1, Q2, Q3, IQR, Lower_Bound, Upper_Bound, new_df