{ "cells": [ { "cell_type": "code", "execution_count": 118, "id": "97bb26ef", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 128, "id": "d76d80a5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS
3411Futrelle, Mrs. Jacques Heath (Lily May Peel)female35.01011380353.1000C123S
4503Allen, Mr. William Henrymale35.0003734508.0500NaNS
\n", "
" ], "text/plain": [ " PassengerId Survived Pclass \\\n", "0 1 0 3 \n", "1 2 1 1 \n", "2 3 1 3 \n", "3 4 1 1 \n", "4 5 0 3 \n", "\n", " Name Sex Age SibSp \\\n", "0 Braund, Mr. Owen Harris male 22.0 1 \n", "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n", "2 Heikkinen, Miss. Laina female 26.0 0 \n", "3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 \n", "4 Allen, Mr. William Henry male 35.0 0 \n", "\n", " Parch Ticket Fare Cabin Embarked \n", "0 0 A/5 21171 7.2500 NaN S \n", "1 0 PC 17599 71.2833 C85 C \n", "2 0 STON/O2. 3101282 7.9250 NaN S \n", "3 0 113803 53.1000 C123 S \n", "4 0 373450 8.0500 NaN S " ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_train = pd.read_csv(\"train.csv\")\n", "df_test = pd.read_csv(\"test.csv\")\n", "\n", "df_train.head()" ] }, { "cell_type": "code", "execution_count": 129, "id": "59c76c5d", "metadata": {}, "outputs": [], "source": [ "features = [\"Pclass\", \"Sex\", \"Age\", \"SibSp\", \"Parch\", 'Fare']" ] }, { "cell_type": "code", "execution_count": 130, "id": "6db8cdcf", "metadata": {}, "outputs": [], "source": [ "y_train = df_train[\"Survived\"]\n", "X_train = pd.get_dummies(df_train[features])\n", "X_test = pd.get_dummies(df_test[features])\n", "\n", "X_test = X_test.fillna(0)\n", "X_train = X_train.fillna(0)" ] }, { "cell_type": "code", "execution_count": 131, "id": "74a3fc72", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
RandomForestClassifier(max_depth=5, random_state=1)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ], "text/plain": [ "RandomForestClassifier(max_depth=5, random_state=1)" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.ensemble import RandomForestClassifier\n", "\n", "#random_state = 1\n", "model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)\n", "model.fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": 132, "id": "5c4e2279", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8451178451178452" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.metrics import accuracy_score\n", "y_pred = model.predict(X_train)\n", "\n", "accuracy_score(y_train, y_pred)" ] }, { "cell_type": "code", "execution_count": 133, "id": "d645db9e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Submission Saved!!\n" ] } ], "source": [ "yTest_pred = model.predict(X_test)\n", "output = pd.DataFrame({'PassengerId': df_test.PassengerId, 'Survived': yTest_pred})\n", "output.to_csv('submissionSimple.csv', index=False)\n", "print(\"Submission Saved!!\")" ] }, { "cell_type": "code", "execution_count": null, "id": "495dd26e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }