{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:37:17.362778Z","iopub.execute_input":"2024-12-05T13:37:17.363200Z","iopub.status.idle":"2024-12-05T13:37:22.199395Z","shell.execute_reply.started":"2024-12-05T13:37:17.363161Z","shell.execute_reply":"2024-12-05T13:37:22.197929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\n\n# Dataset AND logic gate\ndata = pd.DataFrame({\n    'x1': [0, 0, 1, 1],\n    'x2': [0, 1, 0, 1],\n    't': [0, 0, 0, 1]  # Target\n})\n\n# Parameter initialization\nw = np.array([0.0, 0.0])  # Weights\nb = 0.0  # Bias\nalpha = 0.1  # Learning rate\ntheta = 0.0  # Threshold for activation function\nepochs = 10  # Number of epochs\n\n# Activation function (step function)\ndef step_function(y):\n    return 1 if y >= theta else 0\n\n# Training loop\nfor epoch in range(epochs):\n    print(f\"Epoch {epoch + 1}\")\n    for i in range(len(data)):\n        x = np.array([data.loc[i, 'x1'], data.loc[i, 'x2']])\n        target = data.loc[i, 't']\n        # Calculate weighted sum\n        y_in = np.dot(w, x) + b\n        # Activation\n        y = step_function(y_in)\n        # Update weights and bias\n        delta_w = alpha * (target - y) * x\n        delta_b = alpha * (target - y)\n        w += delta_w\n        b += delta_b\n        print(f\"Input: {x}, Target: {target}, Predicted: {y}, Weights: {w}, Bias: {b}\")\n\n# Predict using trained perceptron\npredictions = []\nprint(\"\\nPredictions:\")\nfor i in range(len(data)):\n    x = np.array([data.loc[i, 'x1'], data.loc[i, 'x2']])\n    y_in = np.dot(w, x) + b\n    y = step_function(y_in)\n    predictions.append({'id': i, 'predicted_sii': y})  # Menyimpan prediksi dengan id\n\n# Convert predictions to DataFrame\nsubmission = pd.DataFrame(predictions)\n\n# Save the submission file\noutput_path = 'submission.csv'\nsubmission.to_csv(output_path, index=False)\n\n# Check if file is saved correctly\nif os.path.exists(output_path):\n    print(f\"File saved successfully: {output_path}\")\nelse:\n    print(f\"Failed to save file: {output_path}\")\n\n# Tampilkan file submission untuk memastikan sudah disimpan dengan benar\nprint(submission)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:40:30.490876Z","iopub.execute_input":"2024-12-05T13:40:30.491518Z","iopub.status.idle":"2024-12-05T13:40:30.523050Z","shell.execute_reply.started":"2024-12-05T13:40:30.491443Z","shell.execute_reply":"2024-12-05T13:40:30.521381Z"}},"outputs":[],"execution_count":null}]}