{"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":"tpu1vmV38","dataSources":[{"sourceId":84253,"databundleVersionId":9421888,"sourceType":"competition"}],"dockerImageVersionId":30761,"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\nimport pandas as pd\nimport numpy as np\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.model_selection import cross_val_score, train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom scipy import stats\nfrom sklearn.linear_model import LinearRegression\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","execution":{"iopub.status.busy":"2024-08-25T14:57:01.146584Z","iopub.execute_input":"2024-08-25T14:57:01.147319Z","iopub.status.idle":"2024-08-25T14:57:01.171655Z","shell.execute_reply.started":"2024-08-25T14:57:01.147280Z","shell.execute_reply":"2024-08-25T14:57:01.170885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/first-steps-in-data-science-new/train (7).csv')\nsubmission = pd.read_csv('/kaggle/input/first-steps-in-data-science-new/sample.csv')\ntest=pd.read_csv('/kaggle/input/first-steps-in-data-science-new/test (4).csv')\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:01.172932Z","iopub.execute_input":"2024-08-25T14:57:01.173173Z","iopub.status.idle":"2024-08-25T14:57:02.670294Z","shell.execute_reply.started":"2024-08-25T14:57:01.173148Z","shell.execute_reply":"2024-08-25T14:57:02.669488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.target.hist(bins=120,figsize=(10,5))","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:02.671404Z","iopub.execute_input":"2024-08-25T14:57:02.671724Z","iopub.status.idle":"2024-08-25T14:57:03.161847Z","shell.execute_reply.started":"2024-08-25T14:57:02.671690Z","shell.execute_reply":"2024-08-25T14:57:03.161023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:03.163694Z","iopub.execute_input":"2024-08-25T14:57:03.163968Z","iopub.status.idle":"2024-08-25T14:57:03.168830Z","shell.execute_reply.started":"2024-08-25T14:57:03.163941Z","shell.execute_reply":"2024-08-25T14:57:03.168153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z_scores = np.abs((train - train.mean()) / train.std())\nthreshold = 4.5\ntrain_no_outliers = train[(z_scores < threshold).all(axis=1)]\na=train.shape[0]\nprint(a)\nb=train_no_outliers.shape[0]\nprint(b)\nc=a-b\nprint(c)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:03.169721Z","iopub.execute_input":"2024-08-25T14:57:03.169999Z","iopub.status.idle":"2024-08-25T14:57:03.216961Z","shell.execute_reply.started":"2024-08-25T14:57:03.169969Z","shell.execute_reply":"2024-08-25T14:57:03.216196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_multiple_mean_rows(df, n, prefix='Mean'):\n    # Create an explicit copy of the DataFrame\n    df_copy = df.copy()\n    \n    original_row_count = len(df_copy)\n    rows_per_group = original_row_count // n\n\n    new_rows = []\n    for i in range(n):\n        start_idx = i * rows_per_group\n        end_idx = (i + 1) * rows_per_group if i < n - 1 else None\n        \n        # Calculate mean for this group\n        group_mean = df_copy.iloc[start_idx:end_idx].mean()\n        \n        # Create a new row name\n        row_name = f\"{prefix}_{i+1}\"\n        \n        # Add the mean row to our list of new rows\n        new_rows.append(pd.DataFrame(group_mean).T.rename(index={0: row_name}))\n\n    # Concatenate the original DataFrame with all new rows at once\n    result = pd.concat([df_copy] + new_rows, axis=0)\n\n    return result\ntrain_no_outliers=add_multiple_mean_rows(train_no_outliers,c)\ntrain_no_outliers.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:03.217993Z","iopub.execute_input":"2024-08-25T14:57:03.218248Z","iopub.status.idle":"2024-08-25T14:57:03.266581Z","shell.execute_reply.started":"2024-08-25T14:57:03.218222Z","shell.execute_reply":"2024-08-25T14:57:03.265910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=train_no_outliers.drop(columns=['Unnamed: 0','target'])\ny=train_no_outliers['target']","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:03.267505Z","iopub.execute_input":"2024-08-25T14:57:03.267789Z","iopub.status.idle":"2024-08-25T14:57:03.275365Z","shell.execute_reply.started":"2024-08-25T14:57:03.267761Z","shell.execute_reply":"2024-08-25T14:57:03.274676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:03.276347Z","iopub.execute_input":"2024-08-25T14:57:03.276836Z","iopub.status.idle":"2024-08-25T14:57:03.288151Z","shell.execute_reply.started":"2024-08-25T14:57:03.276804Z","shell.execute_reply":"2024-08-25T14:57:03.287431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(train)\ndf.describe()","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:03.289058Z","iopub.execute_input":"2024-08-25T14:57:03.289300Z","iopub.status.idle":"2024-08-25T14:57:03.598020Z","shell.execute_reply.started":"2024-08-25T14:57:03.289273Z","shell.execute_reply":"2024-08-25T14:57:03.597407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=LinearRegression()\nmodel.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:03.600227Z","iopub.execute_input":"2024-08-25T14:57:03.600728Z","iopub.status.idle":"2024-08-25T14:57:03.692460Z","shell.execute_reply.started":"2024-08-25T14:57:03.600694Z","shell.execute_reply":"2024-08-25T14:57:03.691459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:03.693880Z","iopub.execute_input":"2024-08-25T14:57:03.694249Z","iopub.status.idle":"2024-08-25T14:57:03.711474Z","shell.execute_reply.started":"2024-08-25T14:57:03.694206Z","shell.execute_reply":"2024-08-25T14:57:03.710226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.sqrt(mean_squared_error(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:03.712966Z","iopub.execute_input":"2024-08-25T14:57:03.713949Z","iopub.status.idle":"2024-08-25T14:57:03.728560Z","shell.execute_reply.started":"2024-08-25T14:57:03.713899Z","shell.execute_reply":"2024-08-25T14:57:03.727394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=model.predict(test.drop(columns=['Unnamed: 0']))\nsubmission['target'] = preds\nsubmission.to_csv(\"submission.csv\", index = False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-25T14:57:03.730747Z","iopub.execute_input":"2024-08-25T14:57:03.732764Z","iopub.status.idle":"2024-08-25T14:57:03.918851Z","shell.execute_reply.started":"2024-08-25T14:57:03.732721Z","shell.execute_reply":"2024-08-25T14:57:03.918123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}