{"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-16T02:41:28.733991Z","iopub.execute_input":"2022-07-16T02:41:28.734402Z","iopub.status.idle":"2022-07-16T02:41:28.744934Z","shell.execute_reply.started":"2022-07-16T02:41:28.734371Z","shell.execute_reply":"2022-07-16T02:41:28.743306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(\"../input/titanic/train.csv\")\ntest=pd.read_csv(\"../input/titanic/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:11.104469Z","iopub.execute_input":"2022-07-16T02:44:11.104863Z","iopub.status.idle":"2022-07-16T02:44:11.120470Z","shell.execute_reply.started":"2022-07-16T02:44:11.104830Z","shell.execute_reply":"2022-07-16T02:44:11.119488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:12.178638Z","iopub.execute_input":"2022-07-16T02:44:12.178977Z","iopub.status.idle":"2022-07-16T02:44:12.194280Z","shell.execute_reply.started":"2022-07-16T02:44:12.178948Z","shell.execute_reply":"2022-07-16T02:44:12.193425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.loc[train.Age.isnull()==True,\"Age\"]=train.Age.mean()\ntrain=train.drop(\"Cabin\",axis=1)\ntrain=train.drop([\"Ticket\",\"Name\",\"PassengerId\",\"SibSp\",\"Parch\",\"Age\"],axis=1)\ntrain=train.drop(61,axis=0)\ntrain=train.drop(829,axis=0)\ntrain.loc[train[\"Sex\"]==\"male\",\"Sex\"]=0\ntrain.loc[train[\"Sex\"]==\"female\",\"Sex\"]=1\ntrain.loc[train.Embarked==\"C\",\"Embarked\"]=0\ntrain.loc[train.Embarked==\"S\",\"Embarked\"]=1\ntrain.loc[train.Embarked==\"Q\",\"Embarked\"]=2\n#train.Age=round(train.Age)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:13.230732Z","iopub.execute_input":"2022-07-16T02:44:13.231362Z","iopub.status.idle":"2022-07-16T02:44:13.246652Z","shell.execute_reply.started":"2022-07-16T02:44:13.231328Z","shell.execute_reply":"2022-07-16T02:44:13.245598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#trainデータをtr_train : tr_test = 7 : 3に分割\nfrom sklearn.model_selection import train_test_split\ntr_train,tr_test=train_test_split(train,test_size=0.3,random_state=1234)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:16.627610Z","iopub.execute_input":"2022-07-16T02:44:16.628027Z","iopub.status.idle":"2022-07-16T02:44:16.635534Z","shell.execute_reply.started":"2022-07-16T02:44:16.627992Z","shell.execute_reply":"2022-07-16T02:44:16.634126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr_train_X=tr_train[train.columns[1:]]\ntr_train_Y=tr_train.Survived\ntr_test_X=tr_test[train.columns[1:]]\ntr_test_Y=tr_test[train.columns[0]]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:17.481280Z","iopub.execute_input":"2022-07-16T02:44:17.481910Z","iopub.status.idle":"2022-07-16T02:44:17.489008Z","shell.execute_reply.started":"2022-07-16T02:44:17.481861Z","shell.execute_reply":"2022-07-16T02:44:17.487914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#決定木\nfrom sklearn import tree\nmodel=tree.DecisionTreeClassifier()\nmodel.fit(tr_train_X,tr_train_Y)\npredict=model.predict(tr_test_X)\n\nfrom sklearn import metrics\nprint(\"判別率:\",metrics.accuracy_score(predict,tr_test_Y))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:18.401257Z","iopub.execute_input":"2022-07-16T02:44:18.401920Z","iopub.status.idle":"2022-07-16T02:44:18.412978Z","shell.execute_reply.started":"2022-07-16T02:44:18.401872Z","shell.execute_reply":"2022-07-16T02:44:18.411876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.model_selection import KFold,cross_val_score,cross_val_predict\n#kf=KFold(n_splits=5,random_state=30,shuffle=True)\n#x=train[train.columns[1:]]\n#y=train[\"Survived\"]\n#cv_result=cross_val_score(model,x,y,cv=kf)\n#print(cv_result)\n#print(\"平均精度:{}\".format(cv_result.mean()))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T01:02:39.097382Z","iopub.execute_input":"2022-07-16T01:02:39.098573Z","iopub.status.idle":"2022-07-16T01:02:39.137413Z","shell.execute_reply.started":"2022-07-16T01:02:39.098525Z","shell.execute_reply":"2022-07-16T01:02:39.136423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Random Forest\n#from sklearn.ensemble import RandomForestClassifier\n#model=RandomForestClassifier(n_estimators=50,max_depth=5)\n#model.fit(tr_train_X,tr_train_Y)\n#predict=model.predict(tr_test_X)\n#\n#from sklearn import metrics\n#print(\"判別率:\",metrics.accuracy_score(predict,tr_test_Y))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:52:11.607690Z","iopub.execute_input":"2022-07-16T02:52:11.608094Z","iopub.status.idle":"2022-07-16T02:52:11.698732Z","shell.execute_reply.started":"2022-07-16T02:52:11.608061Z","shell.execute_reply":"2022-07-16T02:52:11.697574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.model_selection import KFold,cross_val_score,cross_val_predict\n#kf=KFold(n_splits=5,random_state=30,shuffle=True)\n#x=train[train.columns[1:]]\n#y=train[\"Survived\"]\n#cv_result=cross_val_score(model,x,y,cv=kf)\n#print(cv_result)\n#print(\"平均精度:{}\".format(cv_result.mean()))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:52:12.728732Z","iopub.execute_input":"2022-07-16T02:52:12.729085Z","iopub.status.idle":"2022-07-16T02:52:13.129851Z","shell.execute_reply.started":"2022-07-16T02:52:12.729054Z","shell.execute_reply":"2022-07-16T02:52:13.128728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.model_selection import GridSearchCV\n#param={\"n_estimators\":range(100,1000,100)}\n#GS_rf=GridSearchCV(estimator=RandomForestClassifier(random_state=0),param_grid=param,verbose=True,cv=5)\n#GS_rf.fit(x,y)\n#print(\"best score:{}\".format(GS_rf.best_score_))\n#print(\"最適なパラメータ:{}\".format(GS_rf.best_estimator_))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T01:34:37.570375Z","iopub.execute_input":"2022-07-16T01:34:37.570741Z","iopub.status.idle":"2022-07-16T01:35:15.419334Z","shell.execute_reply.started":"2022-07-16T01:34:37.570713Z","shell.execute_reply":"2022-07-16T01:35:15.418217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.loc[test.Age.isnull()==True,\"Age\"]=test.Age.mean()\ntest=test.drop(\"Cabin\",axis=1)\ntest=test.drop([\"Ticket\",\"Name\",\"PassengerId\",\"SibSp\",\"Parch\",\"Age\"],axis=1)\ntest.loc[test[\"Sex\"]==\"male\",\"Sex\"]=0\ntest.loc[test[\"Sex\"]==\"female\",\"Sex\"]=1\ntest.loc[test.Embarked==\"C\",\"Embarked\"]=0\ntest.loc[test.Embarked==\"S\",\"Embarked\"]=1\ntest.loc[test.Embarked==\"Q\",\"Embarked\"]=2\n#test.Age=round(test.Age)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:40.230337Z","iopub.execute_input":"2022-07-16T02:44:40.230704Z","iopub.status.idle":"2022-07-16T02:44:40.243408Z","shell.execute_reply.started":"2022-07-16T02:44:40.230675Z","shell.execute_reply":"2022-07-16T02:44:40.242578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.loc[test.Fare.isnull()==True,\"Fare\"]=test.Fare.median()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:45.849038Z","iopub.execute_input":"2022-07-16T02:44:45.849970Z","iopub.status.idle":"2022-07-16T02:44:45.856792Z","shell.execute_reply.started":"2022-07-16T02:44:45.849925Z","shell.execute_reply":"2022-07-16T02:44:45.855961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model=GS_rf.best_estimator_\nmodel.fit(train[train.columns[1:]],train[train.columns[0]])\ntest_prediction=model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:46.822928Z","iopub.execute_input":"2022-07-16T02:44:46.823650Z","iopub.status.idle":"2022-07-16T02:44:46.837470Z","shell.execute_reply.started":"2022-07-16T02:44:46.823601Z","shell.execute_reply":"2022-07-16T02:44:46.836262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"passenger_id=np.arange(892,1310)\ntest_result=pd.DataFrame({\"PassengerId\":passenger_id,\"Survived\":test_prediction})","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:47.417646Z","iopub.execute_input":"2022-07-16T02:44:47.418365Z","iopub.status.idle":"2022-07-16T02:44:47.424136Z","shell.execute_reply.started":"2022-07-16T02:44:47.418315Z","shell.execute_reply":"2022-07-16T02:44:47.423026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_result.to_csv(\"titanic_forsubmission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:47.908418Z","iopub.execute_input":"2022-07-16T02:44:47.909056Z","iopub.status.idle":"2022-07-16T02:44:47.915587Z","shell.execute_reply.started":"2022-07-16T02:44:47.909020Z","shell.execute_reply":"2022-07-16T02:44:47.914657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_importances=pd.DataFrame({\"feature_importances\":model.feature_importances_})","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:48.448947Z","iopub.execute_input":"2022-07-16T02:44:48.449751Z","iopub.status.idle":"2022-07-16T02:44:48.455130Z","shell.execute_reply.started":"2022-07-16T02:44:48.449707Z","shell.execute_reply":"2022-07-16T02:44:48.454274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.barplot(tr_train_X.columns,feature_importances.feature_importances)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T02:44:49.265418Z","iopub.execute_input":"2022-07-16T02:44:49.266426Z","iopub.status.idle":"2022-07-16T02:44:49.429833Z","shell.execute_reply.started":"2022-07-16T02:44:49.266389Z","shell.execute_reply":"2022-07-16T02:44:49.428711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}