{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":82253,"databundleVersionId":8965849,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"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","execution":{"iopub.status.busy":"2024-06-28T13:10:22.678828Z","iopub.execute_input":"2024-06-28T13:10:22.679278Z","iopub.status.idle":"2024-06-28T13:10:23.16269Z","shell.execute_reply.started":"2024-06-28T13:10:22.679245Z","shell.execute_reply":"2024-06-28T13:10:23.160972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Replace 'filename.csv' with the actual CSV file name in the dataset\ntrain = '/kaggle/input/f1nalyze-datathon-ieeecsmuj/train.csv'\ntrain_ = pd.read_csv(train)\n\n# Display the first few rows of the DataFrame\ntrain_.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:10:23.165296Z","iopub.execute_input":"2024-06-28T13:10:23.165996Z","iopub.status.idle":"2024-06-28T13:11:10.329971Z","shell.execute_reply.started":"2024-06-28T13:10:23.165947Z","shell.execute_reply":"2024-06-28T13:11:10.328509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = '/kaggle/input/f1nalyze-datathon-ieeecsmuj/test.csv'\ntest_ = pd.read_csv(test)\n\n# Display the first few rows of the DataFrame\ntest_.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:11:10.331387Z","iopub.execute_input":"2024-06-28T13:11:10.3318Z","iopub.status.idle":"2024-06-28T13:11:15.600698Z","shell.execute_reply.started":"2024-06-28T13:11:10.331767Z","shell.execute_reply":"2024-06-28T13:11:15.599671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation = '/kaggle/input/f1nalyze-datathon-ieeecsmuj/test.csv'\nvalidation_ = pd.read_csv(validation)\n\n# Display the first few rows of the DataFrame\nvalidation_.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:11:15.60413Z","iopub.execute_input":"2024-06-28T13:11:15.604646Z","iopub.status.idle":"2024-06-28T13:11:19.584627Z","shell.execute_reply.started":"2024-06-28T13:11:15.604608Z","shell.execute_reply":"2024-06-28T13:11:19.583456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:11:19.586244Z","iopub.execute_input":"2024-06-28T13:11:19.586687Z","iopub.status.idle":"2024-06-28T13:11:19.592028Z","shell.execute_reply.started":"2024-06-28T13:11:19.586646Z","shell.execute_reply":"2024-06-28T13:11:19.590966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_linear_regression(X, y, epochs=100, learning_rate=0.01):\n    \"\"\"\n    Train a linear regression model using gradient descent.\n    \n    Parameters:\n    X (DataFrame): Training features.\n    y (Series): Training target.\n    epochs (int): Number of iterations for training.\n    learning_rate (float): Learning rate for gradient descent.\n    \n    Returns:\n    numpy array: Trained model weights.\n    \"\"\"\n    X = np.c_[np.ones(X.shape[0]), X]  # Add bias term\n    weights = np.zeros(X.shape[1])\n    for epoch in range(epochs):\n        predictions = np.dot(X, weights)\n        errors = predictions - y\n        gradient = np.dot(X.T, errors) / len(y)\n        weights -= learning_rate * gradient\n    return weights\n","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:11:19.593511Z","iopub.execute_input":"2024-06-28T13:11:19.593899Z","iopub.status.idle":"2024-06-28T13:11:19.605904Z","shell.execute_reply.started":"2024-06-28T13:11:19.593869Z","shell.execute_reply":"2024-06-28T13:11:19.604779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(X, weights):\n    \"\"\"\n    Make predictions using the trained linear regression model.\n    \n    Parameters:\n    X (DataFrame): Features for prediction.\n    weights (numpy array): Model weights.\n    \n    Returns:\n    numpy array: Predictions.\n    \"\"\"\n    X = np.c_[np.ones(X.shape[0]), X]  # Add bias term\n    return np.dot(X, weights)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:11:19.607378Z","iopub.execute_input":"2024-06-28T13:11:19.607784Z","iopub.status.idle":"2024-06-28T13:11:19.622487Z","shell.execute_reply.started":"2024-06-28T13:11:19.60775Z","shell.execute_reply":"2024-06-28T13:11:19.621246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Main script execution\ndef main():\n   \n    # Separate features and target variable\n    features = combined_data.drop(columns=['points'])\n    target = combined_data['points']\n    \n    data_train,data_test=train_,test_\n    \n    \n    # Split the data into training and testing sets\n    X_train, X_test, y_train, y_test = data_train,\n    \n    # Train the linear regression model\n    model_weights = train_linear_regression(X_train, y_train.values, epochs=100, learning_rate=0.01)\n    \n    # Evaluate the model on the test set\n    test_predictions = predict(X_test, model_weights)\n    test_loss = np.mean((test_predictions - y_test.values) ** 2)\n    print(f'Test Loss: {test_loss}')\n    \n    # Predict next year's standings\n    last_year_data = combined_data[combined_data['year'] == combined_data['year'].max()]\n    X_next_year = last_year_data.drop(columns=['points'])\n    \n    next_year_predictions = predict(X_next_year, model_weights)\n    last_year_data['predicted_points'] = next_year_predictions\n    \n    # Sort by predicted points to get the standings\n    standings = last_year_data.sort_values(by='predicted_points', ascending=False)\n    print(standings[['driverRef', 'predicted_points']])","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:11:19.624151Z","iopub.execute_input":"2024-06-28T13:11:19.625308Z","iopub.status.idle":"2024-06-28T13:11:19.63598Z","shell.execute_reply.started":"2024-06-28T13:11:19.625271Z","shell.execute_reply":"2024-06-28T13:11:19.634769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if __name__ == \"__main__\":\n    main()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:11:19.637506Z","iopub.execute_input":"2024-06-28T13:11:19.637906Z","iopub.status.idle":"2024-06-28T13:11:19.741574Z","shell.execute_reply.started":"2024-06-28T13:11:19.637868Z","shell.execute_reply":"2024-06-28T13:11:19.739773Z"},"trusted":true},"execution_count":null,"outputs":[]}]}