{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":81933,"databundleVersionId":9643020,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\n# Import required libraries\nimport numpy as np\nimport pandas as pd\nfrom sklearn.metrics import accuracy_score\n\n# Define the step function as the activation function\ndef step_function(x):\n    return 1 if x >= 0 else 0\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T07:17:32.213581Z","iopub.execute_input":"2024-12-05T07:17:32.214009Z","iopub.status.idle":"2024-12-05T07:17:33.203197Z","shell.execute_reply.started":"2024-12-05T07:17:32.213971Z","shell.execute_reply":"2024-12-05T07:17:33.202240Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Define the dataset for AND logic gate\ndata = {\n    \"x1\": [0, 0, 1, 1],\n    \"x2\": [0, 1, 0, 1],\n    \"t\":  [0, 0, 0, 1]  # Target output\n}\ndf = pd.DataFrame(data)\nprint(\"Dataset:\", df)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T07:17:56.541519Z","iopub.execute_input":"2024-12-05T07:17:56.542087Z","iopub.status.idle":"2024-12-05T07:17:56.559043Z","shell.execute_reply.started":"2024-12-05T07:17:56.542049Z","shell.execute_reply":"2024-12-05T07:17:56.557950Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Initialize parameters\nweights = np.array([0.0, 0.0])  # [w1, w2]\nbias = 0.0\nlearning_rate = 0.1\nepochs = 10  # Number of training iterations\n\nprint(\"Initial weights:\", weights)\nprint(\"Initial bias:\", bias)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T07:18:33.290717Z","iopub.execute_input":"2024-12-05T07:18:33.291123Z","iopub.status.idle":"2024-12-05T07:18:33.297973Z","shell.execute_reply.started":"2024-12-05T07:18:33.291088Z","shell.execute_reply":"2024-12-05T07:18:33.296858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Train the Simple Perceptron\nfor epoch in range(epochs):\n    print(f\"Epoch {epoch + 1}/{epochs}\")\n    for index, row in df.iterrows():\n        x = np.array([row['x1'], row['x2']])  # Input features\n        target = row['t']  # Target output\n\n        # Calculate weighted sum\n        weighted_sum = np.dot(weights, x) + bias\n\n        # Apply step function\n        output = step_function(weighted_sum)\n\n        # Update weights and bias if prediction is wrong\n        error = target - output\n        weights += learning_rate * error * x\n        bias += learning_rate * error\n\n        print(f\"Input: {x}, Target: {target}, Predicted: {output}, Error: {error}\")\n        print(f\"Updated weights: {weights}, Updated bias: {bias}\")\n    print(\"-\" * 30)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T07:18:45.973490Z","iopub.execute_input":"2024-12-05T07:18:45.973928Z","iopub.status.idle":"2024-12-05T07:18:46.000220Z","shell.execute_reply.started":"2024-12-05T07:18:45.973888Z","shell.execute_reply":"2024-12-05T07:18:45.999034Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Test the trained model on the same dataset\npredictions = []\nfor index, row in df.iterrows():\n    x = np.array([row['x1'], row['x2']])\n    weighted_sum = np.dot(weights, x) + bias\n    output = step_function(weighted_sum)\n    predictions.append(output)\n\ndf['Prediction'] = predictions\nprint(\"Results:\", df)\n\n# Calculate accuracy\naccuracy = accuracy_score(df['t'], df['Prediction'])\nprint(\"Accuracy:\", accuracy)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T07:19:09.942031Z","iopub.execute_input":"2024-12-05T07:19:09.942430Z","iopub.status.idle":"2024-12-05T07:19:09.953845Z","shell.execute_reply.started":"2024-12-05T07:19:09.942392Z","shell.execute_reply":"2024-12-05T07:19:09.952764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Simulasi dataset uji\ntest_data = {\n    \"x1\": [0, 0, 1, 1],\n    \"x2\": [0, 1, 0, 1]\n}\ntest_df = pd.DataFrame(test_data)\n\n# Prediksi menggunakan model\ntest_predictions = []\nfor index, row in test_df.iterrows():\n    x = np.array([row['x1'], row['x2']])\n    weighted_sum = np.dot(weights, x) + bias\n    output = step_function(weighted_sum)\n    test_predictions.append(output)\n\ntest_df['Prediction'] = test_predictions\n\n# Membuat submission file\nsubmission = pd.DataFrame({\n    \"id\": range(len(test_df)),\n    \"Prediction\": test_df[\"Prediction\"]\n})\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"Submission file saved as submission.csv\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T07:29:01.157802Z","iopub.execute_input":"2024-12-05T07:29:01.158283Z","iopub.status.idle":"2024-12-05T07:29:01.176864Z","shell.execute_reply.started":"2024-12-05T07:29:01.158243Z","shell.execute_reply":"2024-12-05T07:29:01.175552Z"}},"outputs":[],"execution_count":null}]}