{"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:41:44.014981Z","iopub.execute_input":"2024-12-05T13:41:44.016648Z","iopub.status.idle":"2024-12-05T13:41:45.306963Z","shell.execute_reply.started":"2024-12-05T13:41:44.016533Z","shell.execute_reply":"2024-12-05T13:41:45.305526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import cohen_kappa_score\nfrom sklearn.preprocessing import StandardScaler","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:45.308892Z","iopub.execute_input":"2024-12-05T13:41:45.309235Z","iopub.status.idle":"2024-12-05T13:41:45.315639Z","shell.execute_reply.started":"2024-12-05T13:41:45.309201Z","shell.execute_reply":"2024-12-05T13:41:45.314336Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\ntest = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:45.317217Z","iopub.execute_input":"2024-12-05T13:41:45.317579Z","iopub.status.idle":"2024-12-05T13:41:45.405352Z","shell.execute_reply.started":"2024-12-05T13:41:45.317546Z","shell.execute_reply":"2024-12-05T13:41:45.403951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train.info())\nprint(train.describe())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:45.408293Z","iopub.execute_input":"2024-12-05T13:41:45.408717Z","iopub.status.idle":"2024-12-05T13:41:45.574067Z","shell.execute_reply.started":"2024-12-05T13:41:45.408680Z","shell.execute_reply":"2024-12-05T13:41:45.572811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = ['Physical-BMI', 'Physical-Height', 'Physical-Weight']\ntarget = 'sii'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:45.575557Z","iopub.execute_input":"2024-12-05T13:41:45.575879Z","iopub.status.idle":"2024-12-05T13:41:45.581510Z","shell.execute_reply.started":"2024-12-05T13:41:45.575848Z","shell.execute_reply":"2024-12-05T13:41:45.580199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in features:\n    if col in train.columns:\n        train[col] = train[col].fillna(train[col].median())\n        if col in test.columns:\n            test[col] = test[col].fillna(test[col].median())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:45.582908Z","iopub.execute_input":"2024-12-05T13:41:45.583276Z","iopub.status.idle":"2024-12-05T13:41:45.598658Z","shell.execute_reply.started":"2024-12-05T13:41:45.583242Z","shell.execute_reply":"2024-12-05T13:41:45.597570Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[target] = train[target].fillna(0).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:45.600340Z","iopub.execute_input":"2024-12-05T13:41:45.600816Z","iopub.status.idle":"2024-12-05T13:41:45.609899Z","shell.execute_reply.started":"2024-12-05T13:41:45.600770Z","shell.execute_reply":"2024-12-05T13:41:45.608794Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scaler = StandardScaler()\nX = scaler.fit_transform(train[features])\ny = train[target].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:45.611578Z","iopub.execute_input":"2024-12-05T13:41:45.612047Z","iopub.status.idle":"2024-12-05T13:41:45.627327Z","shell.execute_reply.started":"2024-12-05T13:41:45.611988Z","shell.execute_reply":"2024-12-05T13:41:45.626186Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:45.629113Z","iopub.execute_input":"2024-12-05T13:41:45.629565Z","iopub.status.idle":"2024-12-05T13:41:45.637893Z","shell.execute_reply.started":"2024-12-05T13:41:45.629518Z","shell.execute_reply":"2024-12-05T13:41:45.636704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class SimplePerceptron:\n    def __init__(self, learning_rate=0.01, epochs=100, threshold=0):\n        self.learning_rate = learning_rate\n        self.epochs = epochs\n        self.threshold = threshold\n        self.weights = None\n        self.bias = None\n\n    def activation_function(self, z):\n        return 1 if z > self.threshold else 0\n\n    def fit(self, X, y):\n        n_samples, n_features = X.shape\n        self.weights = np.zeros(n_features)\n        self.bias = 0\n\n        for _ in range(self.epochs):\n            for idx, x_i in enumerate(X):\n                linear_output = np.dot(x_i, self.weights) + self.bias\n                y_predicted = self.activation_function(linear_output)\n\n                # Update rule\n                update = self.learning_rate * (y[idx] - y_predicted)\n                self.weights += update * x_i\n                self.bias += update\n\n    def predict(self, X):\n        linear_output = np.dot(X, self.weights) + self.bias\n        return np.array([self.activation_function(i) for i in linear_output])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:45.641644Z","iopub.execute_input":"2024-12-05T13:41:45.642033Z","iopub.status.idle":"2024-12-05T13:41:45.652987Z","shell.execute_reply.started":"2024-12-05T13:41:45.641999Z","shell.execute_reply":"2024-12-05T13:41:45.651569Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"perceptron = SimplePerceptron(learning_rate=0.01, epochs=200, threshold=0.5)\nperceptron.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:45.654541Z","iopub.execute_input":"2024-12-05T13:41:45.654970Z","iopub.status.idle":"2024-12-05T13:41:49.497103Z","shell.execute_reply.started":"2024-12-05T13:41:45.654930Z","shell.execute_reply":"2024-12-05T13:41:49.496156Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = perceptron.predict(X_val)\nscore = cohen_kappa_score(y_val, y_pred, weights='quadratic')\nprint(\"Quadratic Weighted Kappa Score:\", score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:49.498335Z","iopub.execute_input":"2024-12-05T13:41:49.498687Z","iopub.status.idle":"2024-12-05T13:41:49.509072Z","shell.execute_reply.started":"2024-12-05T13:41:49.498656Z","shell.execute_reply":"2024-12-05T13:41:49.507998Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = scaler.transform(test[features])\ntest['sii'] = perceptron.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:49.510537Z","iopub.execute_input":"2024-12-05T13:41:49.510946Z","iopub.status.idle":"2024-12-05T13:41:49.523358Z","shell.execute_reply.started":"2024-12-05T13:41:49.510909Z","shell.execute_reply":"2024-12-05T13:41:49.522089Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path='submission.csv'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:49.525016Z","iopub.execute_input":"2024-12-05T13:41:49.525505Z","iopub.status.idle":"2024-12-05T13:41:49.538032Z","shell.execute_reply.started":"2024-12-05T13:41:49.525454Z","shell.execute_reply":"2024-12-05T13:41:49.536549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = test[['id', 'sii']]\nsubmission.to_csv(file_path, index=False)\nprint(\"Submission file saved as 'submission.csv'\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T13:41:49.539644Z","iopub.execute_input":"2024-12-05T13:41:49.540034Z","iopub.status.idle":"2024-12-05T13:41:49.555630Z","shell.execute_reply.started":"2024-12-05T13:41:49.539996Z","shell.execute_reply":"2024-12-05T13:41:49.553901Z"}},"outputs":[],"execution_count":null}]}