import pandas as pd
import numpy as np
import seaborn as sns
import [Link] as plt
from [Link] import StandardScaler, LabelEncoder
from sklearn.model_selection import train_test_split
from [Link] import RandomForestClassifier
from [Link] import classification_report, confusion_matrix, accuracy_score
import warnings
[Link]('ignore')
[Link]['[Link]']=[15,15]
df = pd.read_csv("/content/bike_sales_data_world_2013_2023 (1).csv")
pd.set_option('display.max_columns', None)
[Link]()
# ================= CELL SEPARATOR =================
[Link]()
# ================= CELL SEPARATOR =================
[Link]
# ================= CELL SEPARATOR =================
[Link]().T
# ================= CELL SEPARATOR =================
[Link]().sum()
# ================= CELL SEPARATOR =================
[Link]().sum()
# ================= CELL SEPARATOR =================
[Link]()
# ================= CELL SEPARATOR =================
df.drop_duplicates(inplace=True) # removing duplicates
# ================= CELL SEPARATOR =================
for i in [Link]:
print(i)
print(df[i].dtype)
print("------------------")
print(df[i].unique())
print(df[i].nunique())
print("-------------------------------------------------------------------")
# ================= CELL SEPARATOR =================
df["Insurance"] = df["Insurance"].fillna("Unknown")
# ================= CELL SEPARATOR =================
df["Insurance"].value_counts()
# ================= CELL SEPARATOR =================
df['Eco_Friendly'] = df['Eco_Friendly'].astype(int)
# ================= CELL SEPARATOR =================
[Link]('Date',axis=1, inplace=True)
# ================= CELL SEPARATOR =================
df['Rating_Class'] = ['High' if x in (4,5) else 'Low' for x in df['Rating']] #
TARGET VARIABLE
# ================= CELL SEPARATOR =================
[Link]("Rating",axis=1,inplace=True)
# ================= CELL SEPARATOR =================
num = df.select_dtypes(include=[Link]).[Link]()
t=1
for i in num:
[Link](6,3,t)
[Link](x=df[i])
[Link](f'Boxplot for {i}')
t += 1
plt.tight_layout()
[Link]()
# ================= CELL SEPARATOR =================
t=1
for i in num:
[Link](6,3,t)
[Link](df[i],kde=True)
[Link](f'histplot for {i}')
t += 1
plt.tight_layout()
[Link]()
# ================= CELL SEPARATOR =================
import pandas as pd
import [Link] as plt
import numpy as np
# 1. Define the updated list of columns (12 total)
pie_chart_columns = [
'Customer_Gender',
'Age_Group',
'Product_Category',
'Size',
'Color',
'Material',
'Warranty',
'Manufacturer',
'Eco_Friendly',
'Shipping_Company',
'Shipping_Type',
'Return_Policy'
]
# 2. Setup Subplot Layout
n_cols = 3
n_rows = 4 # Changed to 4 rows for 12 plots (4 * 3 = 12)
fig, axes = [Link](n_rows, n_cols, figsize=(20, 20))
axes = [Link]() # Flatten the 2D array of axes for single loop iteration
# 3. Loop and Plot
for i, col in enumerate(pie_chart_columns):
try:
ax = axes[i] # Get the current axis
# Calculate value counts for the column
counts = df[col].value_counts(dropna=False)
# Plotting the pie chart on the current axis
wedges, texts, autotexts = [Link](
counts,
labels=[Link](str),
autopct='%1.1f%%',
startangle=90,
pctdistance=0.7,
wedgeprops={'edgecolor': 'black', 'linewidth': 0.5}
)
ax.set_title(f'{col}', fontsize=14, pad=15)
[Link]('equal') # Ensures pie is circular
# Adjust percentage text size for better fit in subplots
for autotext in autotexts:
autotext.set_fontsize(8)
except Exception as e:
# This block handles any columns that might cause an issue during plotting
print(f"Skipping plot for '{col}' due to error: {e}")
axes[i].axis('off')
# 4. Final Layout and Display
# Since n_rows * n_cols == len(pie_chart_columns), no axes need to be turned off.
[Link]('Univariate Distribution (Pie Charts) of Categorical Columns',
fontsize=24, y=1.01)
plt.tight_layout(rect=[0, 0, 1, 0.98]) # Adjust layout to make room for suptitle
[Link]()
# ================= CELL SEPARATOR =================
cat = df.select_dtypes(include=object).[Link]()
for col in cat:
[Link](figsize=(8,4))
df[col].value_counts().plot(kind="bar")
[Link](f"{col} Countplot")
[Link]()
# ================= CELL SEPARATOR =================
num1 = df.select_dtypes(include=[Link]).[Link]()
# ================= CELL SEPARATOR =================
t=1
for i in num1:
[Link](5,4,t)
[Link](x=df[i],y = df['Rating_Class'])
[Link](f'violin plot for {i}')
[Link](i)
[Link]('Rating class')
t += 1
plt.tight_layout()
[Link]()
# ================= CELL SEPARATOR =================
# ================= CELL SEPARATOR =================
for i in cat:
print(f'Relation between {i} and Rating class (Target)')
print([Link](df[i],df['Rating_Class']))
print('*******************')
# ================= CELL SEPARATOR =================
t=1
[Link](figsize=(20, 10))
for i in
['Age_Group','Customer_Gender','Country','Product_Category','Sub_Category','Shippin
g_Type']:
[Link](3,3, t)
[Link](x=df[i], hue=df['Rating_Class'])
[Link](f'Countplot of {i} vs Rating_Class')
[Link](rotation=45)
t += 1
plt.tight_layout()
[Link]()
# ================= CELL SEPARATOR =================
[Link]([Link](numeric_only=True), cmap='coolwarm', annot=True)
# ================= CELL SEPARATOR =================
df['Rating_Class'].value_counts(normalize = True)*100
# ================= CELL SEPARATOR =================
[Link](x='Rating_Class', data=df, palette='viridis')
[Link]("Target Variable Distribution (Imbalance Check)")
[Link]()
# ================= CELL SEPARATOR =================
iqr= df[num].quantile(0.75)-df[num].quantile(0.25)
upper_whis= df[num].quantile(0.75)+(iqr*1.5)
lower_whis= df[num].quantile(0.25)-(iqr*1.5)
outliers= df[((df[num]< lower_whis)|(df[num]> upper_whis)).any(axis=1)]
outliers
# ================= CELL SEPARATOR =================
outlier_count = [Link][0]
# ================= CELL SEPARATOR =================
outlier_count
# ================= CELL SEPARATOR =================
outlier_percent = (outlier_count / len(df)) * 100
outlier_percent
# ================= CELL SEPARATOR =================
import pandas as pd
import numpy as np
from scipy import stats
import warnings
[Link]('ignore')
rating_map = {
'Low': 0,
'High': 1
}
df['Rating_Class'] = df['Rating_Class'].map(rating_map)
df['Rating_Class'] = df['Rating_Class'].astype(int)
# ================= CELL SEPARATOR =================
target = 'Rating_Class'
num_cols = df.select_dtypes(include=['int64', 'float64']).[Link]()
num_cols = [col for col in num_cols if col != target]
cat_cols = df.select_dtypes(include=['object', 'bool']).[Link]()
cat_cols = [col for col in cat_cols if col != target]
print("Numeric columns:", num_cols)
print("Categorical columns:", cat_cols)
# ================= CELL SEPARATOR =================
results_num = []
for col in num_cols:
group0 = df[df[target] == 0][col].dropna()
group1 = df[df[target] == 1][col].dropna()
stat0, p_shapiro0 = [Link]([Link](min(5000, len(group0)),
random_state=42))
stat1, p_shapiro1 = [Link]([Link](min(5000, len(group1)),
random_state=42))
normal = (p_shapiro0 > 0.05) and (p_shapiro1 > 0.05)
stat_levene, p_levene = [Link](group0, group1)
equal_var = p_levene > 0.05
if normal and equal_var:
test_name = 'Independent t-test'
stat, p_value = stats.ttest_ind(group0, group1, equal_var=True)
else:
test_name = 'Mann-Whitney U'
stat, p_value = [Link](group0, group1)
results_num.append({
'Feature': col,
'Normality p (G0)': p_shapiro0,
'Normality p (G1)': p_shapiro1,
'Levene p': p_levene,
'Test Used': test_name,
'p-value': p_value,
'Significant? (p<0.05)': '✅ Yes' if p_value < 0.05 else '❌ No'
})
results_num_df = [Link](results_num)
results_num_df
# ================= CELL SEPARATOR =================
results_cat = []
for col in cat_cols:
contingency_table = [Link](df[col], df[target])
if contingency_table.shape[0] > 1 and contingency_table.shape[1] > 1:
chi2, p, dof, expected = stats.chi2_contingency(contingency_table)
results_cat.append({
'Feature': col,
'Chi-Square Stat': round(chi2, 3),
'p-value': round(p, 4),
'Significant? (p<0.05)': '✅ Yes' if p < 0.05 else '❌ No'
})
results_cat_df = [Link](results_cat)
results_cat_df
# ================= CELL SEPARATOR =================
from [Link].outliers_influence import variance_inflation_factor
from [Link] import add_constant
import pandas as pd
X = df.select_dtypes(include=['number'])
X_const = add_constant(X)
vif = [Link]()
vif["Feature"] = X_const.columns
vif["VIF"] = [variance_inflation_factor(X_const.values, i) for i in
range(X_const.shape[1])]
vif = vif.sort_values(by="VIF", ascending=False).reset_index(drop=True)
vif
# ================= CELL SEPARATOR =================
[Link]("Revenue",axis=1,inplace=True)
# ================= CELL SEPARATOR =================
X = df.select_dtypes(include=['number'])
X_const = add_constant(X)
vif = [Link]()
vif["Feature"] = X_const.columns
vif["VIF"] = [variance_inflation_factor(X_const.values, i) for i in
range(X_const.shape[1])]
vif = vif.sort_values(by="VIF", ascending=False).reset_index(drop=True)
vif
# ================= CELL SEPARATOR =================
[Link]("Unit_Cost",axis=1,inplace=True)
# ================= CELL SEPARATOR =================
X = df.select_dtypes(include=['number'])
X_const = add_constant(X)
vif = [Link]()
vif["Feature"] = X_const.columns
vif["VIF"] = [variance_inflation_factor(X_const.values, i) for i in
range(X_const.shape[1])]
vif = vif.sort_values(by="VIF", ascending=False).reset_index(drop=True)
vif
# ================= CELL SEPARATOR =================
[Link]()
# ================= CELL SEPARATOR =================
[Link](["Day","Year","Customer_Age","Customer_Gender","Product","Cost","Unit_Price
","Delivery_Time","Shipping_Cost","Shipping_Company","Shipping_Type","Insurance"],a
xis=1,inplace=True)
# ================= CELL SEPARATOR =================
[Link](columns=['Shipping_Weight'], inplace=True)
# ================= CELL SEPARATOR =================
cat1 = df.select_dtypes(include=object).[Link]()
# ================= CELL SEPARATOR =================
for i in cat1:
print(i)
print(df[i].unique())
print('****************')
# ================= CELL SEPARATOR =================
month_map = {
'January': 1, 'February': 2, 'March': 3, 'April': 4,
'May': 5, 'June': 6, 'July': 7, 'August': 8,
'September': 9, 'October': 10, 'November': 11, 'December': 12
}
df['Month'] = df['Month'].map(month_map)
df["Month"] = df["Month"].astype(int)
df['Month_sin'] = [Link](2 * [Link] * df['Month'] / 12)
df['Month_cos'] = [Link](2 * [Link] * df['Month'] / 12)
[Link](columns=['Month'], inplace=True)
# ================= CELL SEPARATOR =================
le = LabelEncoder()
df["Manufacturer"] = le.fit_transform(df["Manufacturer"])
# ================= CELL SEPARATOR =================
from [Link] import OrdinalEncoder
encoder = OrdinalEncoder(categories=[['Small', 'Medium', 'Large', 'Extra Large']])
df['Size'] = encoder.fit_transform(df[['Size']])
# ================= CELL SEPARATOR =================
encoder = OrdinalEncoder(categories=[['1 Year', '2 Years', '3 Years', 'Lifetime']])
df['Warranty'] = encoder.fit_transform(df[['Warranty']])
df['Warranty'] = df['Warranty'].astype(int)
# ================= CELL SEPARATOR =================
encoder = OrdinalEncoder(categories=[[
'Youth (<25)',
'Young Adults (25-34)',
'Adults (35-64)',
'Seniors (64+)'
]])
df['Age_Group'] = encoder.fit_transform(df[['Age_Group']])
df['Age_Group'] = df['Age_Group'].astype(int)
# ================= CELL SEPARATOR =================
from [Link] import LabelEncoder,OrdinalEncoder,OneHotEncoder
# ================= CELL SEPARATOR =================
encoder = LabelEncoder()
# ================= CELL SEPARATOR =================
freq_country = df['Country'].value_counts(normalize=True)
df['Country'] = df['Country'].map(freq_country)
freq_state = df['State'].value_counts(normalize=True)
df['State'] = df['State'].map(freq_state)
df = pd.get_dummies(df, columns=['Sub_Category'], prefix='SubCat',
drop_first=True,dtype=int)
# ================= CELL SEPARATOR =================
df = pd.get_dummies(df, columns=['Color'], drop_first=True,dtype=int)
# ================= CELL SEPARATOR =================
df = pd.get_dummies(df, columns=['Material','Product_Category','Return_Policy'],
drop_first=True,dtype=int)
# ================= CELL SEPARATOR =================
[Link]()
# ================= CELL SEPARATOR =================
num_cols = df.select_dtypes(include=['int64', 'float64']).columns
num_cols = [col for col in num_cols if col != 'Rating_Class']
print("Numeric columns for transformation check:\n", num_cols)
# ================= CELL SEPARATOR =================
skew_values = df[num_cols].skew().sort_values(ascending=False)
skew_values
# ================= CELL SEPARATOR =================
df
# ================= CELL SEPARATOR =================
import numpy as np
df['Profit'].replace([[Link], -[Link]], [Link], inplace=True)
min_profit = df['Profit'].min()
df['Profit'] = np.log1p(df['Profit'] - min_profit + 1)
df['Profit'].fillna(df['Profit'].median(), inplace=True)
print(df['Profit'].describe())
# ================= CELL SEPARATOR =================
skew_values = df[num_cols].skew().sort_values(ascending=False)
skew_values
# ================= CELL SEPARATOR =================
from sklearn.model_selection import train_test_split
x = [Link]('Rating_Class', axis=1)
y = df['Rating_Class']
xtrain, xtest, ytrain, ytest = train_test_split(
x, y, test_size=0.2, random_state=42, stratify=y
)
# ================= CELL SEPARATOR =================
[Link](["min","max"]).T
# ================= CELL SEPARATOR =================
from [Link] import MinMaxScaler
scale_cols = ['Order_Quantity', 'Discount', 'Profit']
scaler = MinMaxScaler()
xtrain[scale_cols] = scaler.fit_transform(xtrain[scale_cols])
xtest[scale_cols] = [Link](xtest[scale_cols])
# ================= CELL SEPARATOR =================
import numpy as np
import pandas as pd
from sklearn import metrics
from [Link] import DecisionTreeClassifier
from xgboost import XGBClassifier
import warnings
[Link]('ignore')
score_card = [Link](columns=[
'Model Name', 'Train Accuracy', 'Test Accuracy',
'Precision', 'Recall (Sensitivity)', 'Specificity',
'F1-Weighted', 'AUC-ROC'
])
def update_score_card(model_name, model, xtrain, xtest, ytrain, ytest):
global score_card
y_pred_train = [Link](xtrain)
y_pred_test = [Link](xtest)
train_acc = metrics.accuracy_score(ytrain, y_pred_train)
test_acc = metrics.accuracy_score(ytest, y_pred_test)
f1_weighted = metrics.f1_score(ytest, y_pred_test, average='weighted')
precision = metrics.precision_score(ytest, y_pred_test)
recall = metrics.recall_score(ytest, y_pred_test)
cm = metrics.confusion_matrix(ytest, y_pred_test)
if [Link] == (2, 2):
tn, fp, fn, tp = [Link]()
specificity = tn / (tn + fp)
else:
specificity = [Link]
try:
y_prob_test = model.predict_proba(xtest)[:, 1]
auc_roc_score = metrics.roc_auc_score(ytest, y_prob_test)
except Exception:
auc_roc_score = [Link]
new_row = [Link]({
'Model Name': [model_name],
'Train Accuracy': [train_acc],
'Test Accuracy': [test_acc],
'Precision': [precision],
'Recall (Sensitivity)': [recall],
'Specificity': [specificity],
'F1-Weighted': [f1_weighted],
'AUC-ROC': [auc_roc_score]
})
score_card = [Link]([score_card, new_row], ignore_index=True)
return score_card
# ================= CELL SEPARATOR =================
xgb_model = XGBClassifier(
random_state=42,
eval_metric='logloss',
use_label_encoder=False
)
xgb_model.fit(xtrain, ytrain)
# ================= CELL SEPARATOR =================
update_score_card("XGBOOST", xgb_model, xtrain, xtest, ytrain, ytest)
# ================= CELL SEPARATOR =================
# ================= CELL SEPARATOR =================
# ================= CELL SEPARATOR =================
# ================= CELL SEPARATOR =================