import numpy as np import seaborn as sns import pandas as pd from sklearn.metrics import…
- User
import numpy as np import seaborn as sns import pandas as pd from sklearn.metrics import accuracy_score, confusion_matrix, classification_report from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler import matplotlib.pyplot as plt
Load the dataset
csv_file_path = "garments_worker_productivity.csv" df = pd.read_csv(csv_file_path)
Inspect the distribution
print(df["actual_productivity"].describe())
Transform actual_productivity
Transform ‘actual_productivity’ to a binary variable
threshold = 0.5 df["productivity_class"] = (df["actual_productivity"] >= threshold).astype(int)
Check the transformation
print(df["productivity_class"].value_counts())
Visualize the distribution of classes
sns.countplot(x="productivity_class", data=df) plt.title("Distribution of Productivity Classes") plt.show()
Normalize Features (Excluding the Target Variable)
Define features and target
X = df.drop(["actual_productivity", "productivity_class"], axis=1) y = df["productivity_class"]
Convert categorical variables into dummies
X = pd.get_dummies(X)
Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Normalize - Fit StandardScaler on X_train and transform both X_train and X_test
scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test)
If needed, visualize the normalized training data (optional)
print(pd.DataFrame(X_train_scaled, columns=X_train.columns).head())
Confirm the splitting
print(f"Training Set Size: {len(X_train)}") print(f"Test Set Size: {len(X_test)}") def sigmoid(x): return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x): return x * (1 - x)
class NeuralNetwork: def init(self, x, y, hidden_neurons=20): self.input = x self.weights1 = np.random.rand(self.input.shape[1], hidden_neurons) * 0.01 self.weights2 = np.random.rand(hidden_neurons, 1) * 0.01 self.y = y self.output = np.zeros(y.shape)
def feedforward(self): self.layer1 = sigmoid(np.dot(self.input, self.weights1)) self.output = sigmoid(np.dot(self.layer1, self.weights2)) def backprop(self): error = self.y - self.output d_weights2 = np.dot(self.layer1.T, (2 * error * sigmoid_derivative(self.output))) d_weights1 = np.dot(self.input.T, (np.dot(2 * error * sigmoid_derivative(self.output), self.weights2.T) * sigmoid_derivative(self.layer1))) self.weights1 += d_weights1 self.weights2 += d_weights2 def train(self, epochs=1000): for epoch in range(epochs): self.feedforward() self.backprop() def predict(self, X): self.input = X self.feedforward() return self.output - Assistant
- User
can you help me fix the errors: Traceback (most recent call last): File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 113, in nn = create_and_train_nn(X_train_scaled, y_train, neurons, epochs=1500) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 96, in create_and_train_nn nn = NeuralNetwork(X_train, y_train, hidden_neurons=hidden_neurons) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 63, in init self.y = y.reshape(-1, 1) ^^^^^^^^^ File "C:\Users\AHM3D\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\generic.py", line 6299, in getattr return object.getattribute(self, name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Series' object has no attribute 'reshape'. Did you mean: 'shape'?
- Assistant
- User
can you help with these errors: File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 114, in nn = create_and_train_nn(X_train_scaled, y_train, neurons, epochs=1500) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 97, in create_and_train_nn nn = NeuralNetwork(X_train, y_train_np, hidden_neurons=hidden_neurons) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 63, in init self.y = y.to_numpy().reshape(-1, 1) # Converts to a NumPy array and then reshapes ^^^^^^^^^^ AttributeError: 'numpy.ndarray' object has no attribute 'to_numpy' PS C:\Users\AHM3D\Desktop\dataset>
- Assistant
- User
can you fix these errors: File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 115, in nn = create_and_train_nn(X_train_scaled, y_train, neurons, epochs=1500) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 98, in create_and_train_nn nn.train(epochs=epochs) File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 87, in train self.backprop() File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 82, in backprop self.bias2 += d_bias2 ValueError: non-broadcastable output operand with shape (1,1) doesn't match the broadcast shape (957,1)
- Assistant
- User
can you fix these errors: Traceback (most recent call last): File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 120, in nn = create_and_train_nn(X_train_scaled, y_train, neurons, epochs=1500) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\AHM3D\Desktop\dataset\ANN.py", line 103, in create_and_train_nn nn.train(epochs=epochs) ^^^^^^^^ AttributeError: 'NeuralNetwork' object has no attribute 'train'
- Assistant
- User
- Assistant