{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2022-07-14T11:12:37.350569Z","iopub.execute_input":"2022-07-14T11:12:37.351186Z","iopub.status.idle":"2022-07-14T11:12:37.370251Z","shell.execute_reply.started":"2022-07-14T11:12:37.351134Z","shell.execute_reply":"2022-07-14T11:12:37.367985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/spaceship-titanic/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:37.432566Z","iopub.execute_input":"2022-07-14T11:12:37.434235Z","iopub.status.idle":"2022-07-14T11:12:37.485462Z","shell.execute_reply.started":"2022-07-14T11:12:37.434134Z","shell.execute_reply":"2022-07-14T11:12:37.484012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##訓練データの中身　上だけ\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:37.518813Z","iopub.execute_input":"2022-07-14T11:12:37.519399Z","iopub.status.idle":"2022-07-14T11:12:37.548746Z","shell.execute_reply.started":"2022-07-14T11:12:37.519361Z","shell.execute_reply":"2022-07-14T11:12:37.546994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##訓練データの中身　ちょい詳しめ\ntrain.describe","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:37.573729Z","iopub.execute_input":"2022-07-14T11:12:37.574612Z","iopub.status.idle":"2022-07-14T11:12:37.599260Z","shell.execute_reply.started":"2022-07-14T11:12:37.574563Z","shell.execute_reply":"2022-07-14T11:12:37.597530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##数値の相関を見る\ntrain.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:37.635382Z","iopub.execute_input":"2022-07-14T11:12:37.636031Z","iopub.status.idle":"2022-07-14T11:12:37.659149Z","shell.execute_reply.started":"2022-07-14T11:12:37.635992Z","shell.execute_reply":"2022-07-14T11:12:37.657585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\ntest = pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')\nsub = pd.read_csv('/kaggle/input/spaceship-titanic/sample_submission.csv')\n\n##外れ値の個数を求める\ntrain.isnull().sum()\n##補完する必要あり","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:37.682755Z","iopub.execute_input":"2022-07-14T11:12:37.683183Z","iopub.status.idle":"2022-07-14T11:12:37.732958Z","shell.execute_reply.started":"2022-07-14T11:12:37.683131Z","shell.execute_reply":"2022-07-14T11:12:37.731499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn\nseaborn.heatmap(train.isnull())","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:37.736496Z","iopub.execute_input":"2022-07-14T11:12:37.736907Z","iopub.status.idle":"2022-07-14T11:12:38.413285Z","shell.execute_reply.started":"2022-07-14T11:12:37.736860Z","shell.execute_reply":"2022-07-14T11:12:38.411670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##数値化されている物の分布確認\nfig=plt.figure()\n\nfig.add_subplot(3,2,1)\nplt.hist(train[\"Age\"],bins=100)\n\nfig.add_subplot(3,2,2)\nplt.hist(train[\"RoomService\"],bins=100,range=(0,5000))\n\nfig.add_subplot(3,2,3)\nplt.hist(train[\"FoodCourt\"],bins=100,range=(0,10000))\n\nfig.add_subplot(3,2,4)\nplt.hist(train[\"ShoppingMall\"],bins=100,range=(0,5000))\n\nfig.add_subplot(3,2,5)\nplt.hist(train[\"Spa\"],bins=100,range=(0,10000))\n\nfig.add_subplot(3,2,6)\nplt.hist(train[\"VRDeck\"],bins=100,range=(0,10000))\n\n##年齢以外は平均値ではなく中央値を外れ値に入れるのが妥当か？","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:38.416492Z","iopub.execute_input":"2022-07-14T11:12:38.417844Z","iopub.status.idle":"2022-07-14T11:12:41.429485Z","shell.execute_reply.started":"2022-07-14T11:12:38.417801Z","shell.execute_reply":"2022-07-14T11:12:41.427673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##↓2行は年齢の外れ値を平均値にするか中央値にするかの検討","metadata":{}},{"cell_type":"code","source":"train[\"Age\"].mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.433007Z","iopub.execute_input":"2022-07-14T11:12:41.434395Z","iopub.status.idle":"2022-07-14T11:12:41.447206Z","shell.execute_reply.started":"2022-07-14T11:12:41.434276Z","shell.execute_reply":"2022-07-14T11:12:41.445319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Age\"].median()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.451352Z","iopub.execute_input":"2022-07-14T11:12:41.452137Z","iopub.status.idle":"2022-07-14T11:12:41.464467Z","shell.execute_reply.started":"2022-07-14T11:12:41.452095Z","shell.execute_reply":"2022-07-14T11:12:41.462660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##外れ値はすべて中央値にする\ntrain['RoomService'].fillna(train['RoomService'].median(skipna=True), inplace=True)\ntrain['FoodCourt'].fillna(train['FoodCourt'].median(skipna=True), inplace=True)\ntrain['ShoppingMall'].fillna(train['ShoppingMall'].median(skipna=True), inplace=True)\ntrain['Spa'].fillna(train['Spa'].median(skipna=True), inplace=True)\ntrain['VRDeck'].fillna(train['VRDeck'].median(skipna=True), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.466531Z","iopub.execute_input":"2022-07-14T11:12:41.467375Z","iopub.status.idle":"2022-07-14T11:12:41.486262Z","shell.execute_reply.started":"2022-07-14T11:12:41.467322Z","shell.execute_reply":"2022-07-14T11:12:41.484101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"FoodCourt\"].mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.488479Z","iopub.execute_input":"2022-07-14T11:12:41.489341Z","iopub.status.idle":"2022-07-14T11:12:41.499733Z","shell.execute_reply.started":"2022-07-14T11:12:41.489301Z","shell.execute_reply":"2022-07-14T11:12:41.497892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##上で年齢の外れ値を補完し忘れてたのでここで追加、欠損値の個数再度確認\ntrain['Age'].fillna(train['Age'].median(skipna=True), inplace=True)\ntrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.502329Z","iopub.execute_input":"2022-07-14T11:12:41.503321Z","iopub.status.idle":"2022-07-14T11:12:41.528036Z","shell.execute_reply.started":"2022-07-14T11:12:41.503271Z","shell.execute_reply":"2022-07-14T11:12:41.526184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##欠損値補完後の相関を見る\ntrain.corr()\n##船内サービスに費やした金額の総和を行えば相関が強くなるか？","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.530008Z","iopub.execute_input":"2022-07-14T11:12:41.530863Z","iopub.status.idle":"2022-07-14T11:12:41.555862Z","shell.execute_reply.started":"2022-07-14T11:12:41.530822Z","shell.execute_reply":"2022-07-14T11:12:41.554292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##\"OnboardService\"として船内サービス全体の金額を説明変数に追加\ntrain[\"OnboardService\"] = train[\"RoomService\"]+train[\"FoodCourt\"]+train[\"ShoppingMall\"]+train[\"Spa\"]+train[\"VRDeck\"]\n##実際に追加されたか確認\ntrain.describe","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.558320Z","iopub.execute_input":"2022-07-14T11:12:41.558891Z","iopub.status.idle":"2022-07-14T11:12:41.593555Z","shell.execute_reply.started":"2022-07-14T11:12:41.558851Z","shell.execute_reply":"2022-07-14T11:12:41.591630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##説明変数追加後の相関\ntrain.corr()\n##あまり意味がなさそう？","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.599494Z","iopub.execute_input":"2022-07-14T11:12:41.600064Z","iopub.status.idle":"2022-07-14T11:12:41.631755Z","shell.execute_reply.started":"2022-07-14T11:12:41.600026Z","shell.execute_reply":"2022-07-14T11:12:41.629148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##乗客番号、出身、船室、名前は訓練データに関係ないとして、消去する\ntrain.drop(['PassengerId','HomePlanet','Cabin','Name'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.634942Z","iopub.execute_input":"2022-07-14T11:12:41.635552Z","iopub.status.idle":"2022-07-14T11:12:41.647480Z","shell.execute_reply.started":"2022-07-14T11:12:41.635512Z","shell.execute_reply":"2022-07-14T11:12:41.645953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##コールドスリープ、目的地、VIPの欠損値の補完は難しいので、それらの欠損値を含む行を消去\ntrain.dropna(how='any')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.649081Z","iopub.execute_input":"2022-07-14T11:12:41.649676Z","iopub.status.idle":"2022-07-14T11:12:41.700971Z","shell.execute_reply.started":"2022-07-14T11:12:41.649637Z","shell.execute_reply":"2022-07-14T11:12:41.699571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.703136Z","iopub.execute_input":"2022-07-14T11:12:41.703570Z","iopub.status.idle":"2022-07-14T11:12:41.728145Z","shell.execute_reply.started":"2022-07-14T11:12:41.703537Z","shell.execute_reply":"2022-07-14T11:12:41.726589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##コールドスリープとVIP、目的地のダミー化\ncategory = ['CryoSleep','VIP','Destination','Transported']\ntraining = pd.get_dummies(train, columns=category,drop_first=True)\ntraining","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.730078Z","iopub.execute_input":"2022-07-14T11:12:41.730494Z","iopub.status.idle":"2022-07-14T11:12:41.771877Z","shell.execute_reply.started":"2022-07-14T11:12:41.730461Z","shell.execute_reply":"2022-07-14T11:12:41.770442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##訓練データの相関を見る\ntraining.corr()\n##コールドスリープと目的値への到着は正の相関を強く\n##船室サービスと目的地への到着は弱いながら負の相関がある","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.773234Z","iopub.execute_input":"2022-07-14T11:12:41.773588Z","iopub.status.idle":"2022-07-14T11:12:41.803243Z","shell.execute_reply.started":"2022-07-14T11:12:41.773557Z","shell.execute_reply":"2022-07-14T11:12:41.802186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##相関が低いもの、不必要と思われる説明変数を消去する\ntraining.drop(['Age','FoodCourt','ShoppingMall','Spa','OnboardService','VIP_True','Destination_PSO J318.5-22','Destination_TRAPPIST-1e'],axis=1,inplace=True)\ntraining.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.805139Z","iopub.execute_input":"2022-07-14T11:12:41.806501Z","iopub.status.idle":"2022-07-14T11:12:41.828366Z","shell.execute_reply.started":"2022-07-14T11:12:41.806445Z","shell.execute_reply":"2022-07-14T11:12:41.827031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\ntraining_standard = StandardScaler()\ntraining_standard.fit(training[['RoomService','VRDeck']])\ntraining_std = pd.DataFrame(training_standard.transform(training[['RoomService','VRDeck']]))\ntraining [['RoomService','VRDeck']] = training_std\ntraining","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.830205Z","iopub.execute_input":"2022-07-14T11:12:41.831025Z","iopub.status.idle":"2022-07-14T11:12:41.865259Z","shell.execute_reply.started":"2022-07-14T11:12:41.830967Z","shell.execute_reply":"2022-07-14T11:12:41.862863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\ncols = ['RoomService','VRDeck','CryoSleep_True'] \nX = training[cols]\ny = training['Transported_True']\n# Build a logreg and compute the feature importances\nmodel = LogisticRegression()\n# create the RFE model and select 8 attributes\nmodel.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.867719Z","iopub.execute_input":"2022-07-14T11:12:41.868242Z","iopub.status.idle":"2022-07-14T11:12:41.903458Z","shell.execute_reply.started":"2022-07-14T11:12:41.868202Z","shell.execute_reply":"2022-07-14T11:12:41.901504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\ntrain_predicted = model.predict(X)\naccuracy_score(train_predicted, y)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.905822Z","iopub.execute_input":"2022-07-14T11:12:41.906793Z","iopub.status.idle":"2022-07-14T11:12:41.930067Z","shell.execute_reply.started":"2022-07-14T11:12:41.906747Z","shell.execute_reply":"2022-07-14T11:12:41.928232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')\ntest.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.933037Z","iopub.execute_input":"2022-07-14T11:12:41.934181Z","iopub.status.idle":"2022-07-14T11:12:41.995336Z","shell.execute_reply.started":"2022-07-14T11:12:41.934079Z","shell.execute_reply":"2022-07-14T11:12:41.993250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:41.997991Z","iopub.execute_input":"2022-07-14T11:12:41.999994Z","iopub.status.idle":"2022-07-14T11:12:42.057789Z","shell.execute_reply.started":"2022-07-14T11:12:41.999923Z","shell.execute_reply":"2022-07-14T11:12:42.056249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop(['PassengerId','HomePlanet','Cabin','Destination','Age','VIP','FoodCourt','ShoppingMall','Spa','Name'],axis=1,inplace=True)\ntest['RoomService'].fillna(train['RoomService'].median(skipna=True), inplace=True)\ntest['VRDeck'].fillna(train['VRDeck'].median(skipna=True), inplace=True)\ncategory = ['CryoSleep']\ntesting = pd.get_dummies(test, columns=category,drop_first=True)\ntesting.dropna(how='any')\ntest_copied = testing.copy()\ntest_std = training_standard.transform(test_copied[['RoomService','VRDeck']])\ntest_std\ntesting[['RoomService','VRDeck']] = test_std","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:42.060011Z","iopub.execute_input":"2022-07-14T11:12:42.060943Z","iopub.status.idle":"2022-07-14T11:12:42.091725Z","shell.execute_reply.started":"2022-07-14T11:12:42.060887Z","shell.execute_reply":"2022-07-14T11:12:42.089988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = ['RoomService','VRDeck','CryoSleep_True'] \nX_test=testing[cols]\nprint(X_test.dtypes)\ntest_predicted = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:12:42.093452Z","iopub.execute_input":"2022-07-14T11:12:42.093874Z","iopub.status.idle":"2022-07-14T11:12:42.113368Z","shell.execute_reply.started":"2022-07-14T11:12:42.093840Z","shell.execute_reply":"2022-07-14T11:12:42.110798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/spaceship-titanic/sample_submission.csv')\nsub","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:16:28.532189Z","iopub.execute_input":"2022-07-14T11:16:28.532595Z","iopub.status.idle":"2022-07-14T11:16:28.556081Z","shell.execute_reply.started":"2022-07-14T11:16:28.532564Z","shell.execute_reply":"2022-07-14T11:16:28.554587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['Transported'] = list(map(bool, test_predicted))\nsub.to_csv('submission.csv', index=False)\nsub","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:44:04.825829Z","iopub.execute_input":"2022-07-14T11:44:04.826521Z","iopub.status.idle":"2022-07-14T11:44:04.856071Z","shell.execute_reply.started":"2022-07-14T11:44:04.826487Z","shell.execute_reply":"2022-07-14T11:44:04.855018Z"},"trusted":true},"execution_count":null,"outputs":[]}]}