here is my code: # Import necessary libraries import pandas as pd import numpy as np…
- User
here is my code: # Import necessary libraries import pandas as pd import numpy as np from matplotlib import pyplot as plt from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler
Load the dataset
file_path = "garments_worker_productivity.csv" data = pd.read_csv(file_path)
Visualize the distribution of "actual_productivity"
plt.figure(figsize=(10, 6)) plt.hist(data["actual_productivity"], bins=50, alpha=0.7, color="blue") plt.title("Distribution of Actual Productivity") plt.xlabel("Actual Productivity") plt.ylabel("Frequency") plt.show()
Transform "actual_productivity" to a binary class
average_productivity = data["actual_productivity"].mean() data["productivity_class"] = (data["actual_productivity"] >= average_productivity).astype(int)
Visualizing the newly created binary distribution
plt.figure(figsize=(6, 4)) data["productivity_class"].value_counts().plot(kind="bar", color=["skyblue", "orange"]) plt.title("Distribution of Productivity Classes") plt.xlabel("Class (0: Unsatisfactory, 1: Satisfactory)") plt.ylabel("Frequency") plt.xticks(rotation=0) plt.show()
Adjustments Begin: Handling the date conversion
Check if the "date" column exists and convert it if present
if "date" in data.columns: # Convert "date" column to datetime format data["date"] = pd.to_datetime(data["date"])
# Extract year, month, and day as separate features data["year"] = data["date"].dt.year data["month"] = data["date"].dt.month data["day"] = data["date"].dt.day # After extraction, you can drop the original "date" column if it"s no longer needed data.drop(["date"], axis=1, inplace=True)Adjustments End
Assuming there are no other categorical or textual fields that need conversion
Drop or convert any such columns as needed here
Exclude target variable and any non-feature columns
features = data.drop(["actual_productivity", "productivity_class"], axis=1) target = data["productivity_class"]
Before scaling, ensure all features are numeric
features = features.apply(pd.to_numeric, errors="coerce")
scaler = StandardScaler() features_normalized = scaler.fit_transform(features)
Splitting the dataset into training and test sets
X_train, X_test, Y_train, Y_test = train_test_split(features_normalized, target, test_size=0.2, random_state=42)
Output the shapes to confirm
print("Training Features Shape:", X_train.shape) print("Training Labels Shape:", Y_train.shape) print("Test Features Shape:", X_test.shape) print("Test Labels Shape:", Y_test.shape)
and this is the error I am getting: PS C:\Users\AHM3D\Desktop\dataset> & C:/Users/AHM3D/AppData/Local/Programs/Python/Python312/python.exe c:/Users/AHM3D/Desktop/dataset/dataset.py C:\Users\AHM3D\AppData\Local\Programs\Python\Python312\Lib\site-packages\sklearn\utils\extmath.py:1137: RuntimeWarning: invalid value encountered in divide updated_mean = (last_sum + new_sum) / updated_sample_count C:\Users\AHM3D\AppData\Local\Programs\Python\Python312\Lib\site-packages\sklearn\utils\extmath.py:1142: RuntimeWarning: invalid value encountered in divide T = new_sum / new_sample_count C:\Users\AHM3D\AppData\Local\Programs\Python\Python312\Lib\site-packages\sklearn\utils\extmath.py:1162: RuntimeWarning: invalid value encountered in divide new_unnormalized_variance -= correction**2 / new_sample_count
- Assistant
The warnings you are seeing are likely related to operations involving divisions where there may be numeric instability, such as dividing by zero or very small numbers that lead to undefined or infinite values. In the context of your code, these warnings are being triggered during the process of data scaling with . This situation usually arises when there's an attempt to standardize a feature that has a constant value across all samples (i.e., zero variance) or when there are missing () values in your data that were not handled before scaling.