{"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":"import pandas as pd\nimport numpy as np\nnp.seterr(divide='ignore',invalid='ignore')\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-10T03:21:02.800328Z","iopub.execute_input":"2022-07-10T03:21:02.801161Z","iopub.status.idle":"2022-07-10T03:21:03.942406Z","shell.execute_reply.started":"2022-07-10T03:21:02.801031Z","shell.execute_reply":"2022-07-10T03:21:03.940949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('../input/c/titanic/train.csv')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:03.947009Z","iopub.execute_input":"2022-07-10T03:21:03.947518Z","iopub.status.idle":"2022-07-10T03:21:03.989172Z","shell.execute_reply.started":"2022-07-10T03:21:03.947483Z","shell.execute_reply":"2022-07-10T03:21:03.987846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 训练集特征说明\n- PassengerID (ID)\n- Survived (是否存活)\n- Pclass (客舱等级，重要)\n- Name (姓名，可结合爬虫)\n- Sex (性别，重要)\n- Age (年龄，重要)\n- SibSp (旁系亲友)\n- Parch (直系亲属)\n- Ticket (票编号)\n- Fare (票价)\n- Cabin (客舱编号)\n- Embarked (上船港口编号)","metadata":{}},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:03.990927Z","iopub.execute_input":"2022-07-10T03:21:03.991715Z","iopub.status.idle":"2022-07-10T03:21:04.019715Z","shell.execute_reply.started":"2022-07-10T03:21:03.991662Z","shell.execute_reply":"2022-07-10T03:21:04.018872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"可以看到 Age 和 Cabin 和 Embarked都存在空值","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv('../input/c/titanic/test.csv')\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:04.021885Z","iopub.execute_input":"2022-07-10T03:21:04.022444Z","iopub.status.idle":"2022-07-10T03:21:04.047704Z","shell.execute_reply.started":"2022-07-10T03:21:04.02241Z","shell.execute_reply":"2022-07-10T03:21:04.046706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:04.048993Z","iopub.execute_input":"2022-07-10T03:21:04.049539Z","iopub.status.idle":"2022-07-10T03:21:04.063416Z","shell.execute_reply.started":"2022-07-10T03:21:04.049505Z","shell.execute_reply":"2022-07-10T03:21:04.062184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data = pd.concat([train_data,test_data],ignore_index=True)\nall_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:04.065256Z","iopub.execute_input":"2022-07-10T03:21:04.066429Z","iopub.status.idle":"2022-07-10T03:21:04.094161Z","shell.execute_reply.started":"2022-07-10T03:21:04.066367Z","shell.execute_reply":"2022-07-10T03:21:04.092761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 数据初步分析","metadata":{}},{"cell_type":"code","source":"train_data['Survived'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:04.096868Z","iopub.execute_input":"2022-07-10T03:21:04.098191Z","iopub.status.idle":"2022-07-10T03:21:04.10768Z","shell.execute_reply.started":"2022-07-10T03:21:04.098136Z","shell.execute_reply":"2022-07-10T03:21:04.106758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cdt=train_data['Survived'].value_counts()\nplt.bar(x=('0','1'),height=cdt/sum(cdt))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:04.109443Z","iopub.execute_input":"2022-07-10T03:21:04.110032Z","iopub.status.idle":"2022-07-10T03:21:04.317523Z","shell.execute_reply.started":"2022-07-10T03:21:04.109986Z","shell.execute_reply":"2022-07-10T03:21:04.316327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. 性别与存活率的关系","metadata":{}},{"cell_type":"code","source":"sns.barplot(x = 'Sex' , y = 'Survived',data = train_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:04.319284Z","iopub.execute_input":"2022-07-10T03:21:04.319985Z","iopub.status.idle":"2022-07-10T03:21:04.589365Z","shell.execute_reply.started":"2022-07-10T03:21:04.319937Z","shell.execute_reply":"2022-07-10T03:21:04.588016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"2.客舱等级和存活率关系","metadata":{}},{"cell_type":"code","source":"sns.barplot(x = 'Pclass' , y = 'Survived' , data = train_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:04.594492Z","iopub.execute_input":"2022-07-10T03:21:04.595605Z","iopub.status.idle":"2022-07-10T03:21:04.88835Z","shell.execute_reply.started":"2022-07-10T03:21:04.595551Z","shell.execute_reply":"2022-07-10T03:21:04.887051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"3. 旁系亲属数量和存活率的关系","metadata":{}},{"cell_type":"code","source":"sns.barplot(x = 'SibSp' , y = 'Survived' , data = train_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:04.889755Z","iopub.execute_input":"2022-07-10T03:21:04.890474Z","iopub.status.idle":"2022-07-10T03:21:05.360687Z","shell.execute_reply.started":"2022-07-10T03:21:04.890424Z","shell.execute_reply":"2022-07-10T03:21:05.359529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"4.直系亲属数量和存活率的关系","metadata":{}},{"cell_type":"code","source":"sns.barplot(x = 'Parch' , y = 'Survived' , data = train_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:05.364136Z","iopub.execute_input":"2022-07-10T03:21:05.365404Z","iopub.status.idle":"2022-07-10T03:21:05.860613Z","shell.execute_reply.started":"2022-07-10T03:21:05.365348Z","shell.execute_reply":"2022-07-10T03:21:05.859172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"5.从不同的生还情况的密度图看，15岁附近的生还率有明显区别","metadata":{}},{"cell_type":"code","source":"facet = sns.FacetGrid(train_data , hue = 'Survived' , aspect= 2 )\nfacet.map(sns.kdeplot , 'Age',shade = True)\n#kdeplot核密度估计\nfacet.set(xlim = (0,train_data['Age'].max()))\nfacet.add_legend()\nplt.xlabel('Age')\nplt.ylabel('density')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:05.862335Z","iopub.execute_input":"2022-07-10T03:21:05.86366Z","iopub.status.idle":"2022-07-10T03:21:06.30033Z","shell.execute_reply.started":"2022-07-10T03:21:05.863605Z","shell.execute_reply":"2022-07-10T03:21:06.299016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"6.上船港口和存活率的关系\n","metadata":{}},{"cell_type":"code","source":"sns.countplot('Embarked' , hue = 'Survived' , data = train_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:06.302217Z","iopub.execute_input":"2022-07-10T03:21:06.302572Z","iopub.status.idle":"2022-07-10T03:21:06.525711Z","shell.execute_reply.started":"2022-07-10T03:21:06.30254Z","shell.execute_reply":"2022-07-10T03:21:06.524281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"7.不同称呼的乘客幸存率不同(把name中的称呼都提取出来，并且对同个人具有多个称呼标签进行统一）","metadata":{}},{"cell_type":"code","source":"train_data['Name']","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:06.528151Z","iopub.execute_input":"2022-07-10T03:21:06.529052Z","iopub.status.idle":"2022-07-10T03:21:06.537765Z","shell.execute_reply.started":"2022-07-10T03:21:06.528995Z","shell.execute_reply":"2022-07-10T03:21:06.536937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['Title'] = all_data['Name'].apply(lambda x:x.split(',')[1].split('.')[0].strip())","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:06.538923Z","iopub.execute_input":"2022-07-10T03:21:06.540578Z","iopub.status.idle":"2022-07-10T03:21:06.554099Z","shell.execute_reply.started":"2022-07-10T03:21:06.540527Z","shell.execute_reply":"2022-07-10T03:21:06.552804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['Title'].head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:06.555383Z","iopub.execute_input":"2022-07-10T03:21:06.555842Z","iopub.status.idle":"2022-07-10T03:21:06.571682Z","shell.execute_reply.started":"2022-07-10T03:21:06.555793Z","shell.execute_reply":"2022-07-10T03:21:06.570134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['Title']","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:06.573757Z","iopub.execute_input":"2022-07-10T03:21:06.579482Z","iopub.status.idle":"2022-07-10T03:21:06.592715Z","shell.execute_reply.started":"2022-07-10T03:21:06.579423Z","shell.execute_reply":"2022-07-10T03:21:06.591315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Title_dict = {}\nTitle_dict.update(dict.fromkeys(['Capt','Col','Major','Dr','Rev'],'Officer'))\nTitle_dict.update(dict.fromkeys(['Don','Str','the Countess','Dona','Lady'],'Royalty'))\nTitle_dict.update(dict.fromkeys(['Mme','Ms','Mrs'],'Mrs'))\nTitle_dict.update(dict.fromkeys(['Mlle','Miss'],'Miss'))\nTitle_dict.update(dict.fromkeys(['Mr'],'Mr'))\nTitle_dict.update(dict.fromkeys(['Master','Jonkheer'],'Master'))\n\nall_data['Title'] = all_data['Title'].map(Title_dict)\nsns.barplot(x = 'Title',  y = 'Survived', data = all_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:06.594751Z","iopub.execute_input":"2022-07-10T03:21:06.595879Z","iopub.status.idle":"2022-07-10T03:21:07.010659Z","shell.execute_reply.started":"2022-07-10T03:21:06.595822Z","shell.execute_reply":"2022-07-10T03:21:07.009155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"8.亲属数量和存活率的关系","metadata":{}},{"cell_type":"code","source":"all_data['FamilySize'] = all_data['SibSp'] + all_data['Parch'] + 1\nsns.barplot(x = 'FamilySize' , y = 'Survived' , data = all_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:07.012024Z","iopub.execute_input":"2022-07-10T03:21:07.013136Z","iopub.status.idle":"2022-07-10T03:21:07.50579Z","shell.execute_reply.started":"2022-07-10T03:21:07.01306Z","shell.execute_reply":"2022-07-10T03:21:07.504939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"9.不同客舱的乘客幸存率不同\n（新增Deck特征，先把Cabin空缺值填充为Unknown，再提取Cabin中的首字母构成乘客的甲板号）","metadata":{}},{"cell_type":"code","source":"all_data['Cabin'] = all_data['Cabin'].fillna('Unknown')\nall_data['Desk'] = all_data['Cabin'].str.get(0)\nsns.barplot(x = 'Desk' , y = 'Survived' , data = all_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:07.506972Z","iopub.execute_input":"2022-07-10T03:21:07.507585Z","iopub.status.idle":"2022-07-10T03:21:07.97117Z","shell.execute_reply.started":"2022-07-10T03:21:07.507549Z","shell.execute_reply":"2022-07-10T03:21:07.970103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"10.共票数与生存率的关系（统计每个乘客的共票数）","metadata":{}},{"cell_type":"code","source":"Ticket_count = dict(all_data['Ticket'].value_counts())\nall_data['TicketGroup'] = all_data['Ticket'].apply(lambda x:Ticket_count[x])\nsns.barplot(x = 'TicketGroup', y = 'Survived' , data = all_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:07.972711Z","iopub.execute_input":"2022-07-10T03:21:07.973467Z","iopub.status.idle":"2022-07-10T03:21:08.482243Z","shell.execute_reply.started":"2022-07-10T03:21:07.973418Z","shell.execute_reply":"2022-07-10T03:21:08.481077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Ticket_depart(s):\n    if (s >= 2) and (s <= 4):\n        return 2\n    elif ((s>4) and (s<=8)) or (s == 1):\n        return 1\n    elif s > 8:\n        return 0\nall_data['TicketGroup'] = all_data['TicketGroup'].apply(Ticket_depart)\nsns.barplot(x = 'TicketGroup' , y = 'Survived' , data = all_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:08.483619Z","iopub.execute_input":"2022-07-10T03:21:08.483973Z","iopub.status.idle":"2022-07-10T03:21:08.761025Z","shell.execute_reply.started":"2022-07-10T03:21:08.483941Z","shell.execute_reply":"2022-07-10T03:21:08.760235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 数据清洗","metadata":{}},{"cell_type":"markdown","source":"1. 缺失值清洗         \nAge缺失值为263，用Sex,Title,Pclass三个特征构建随机森林模型，填充年龄缺失值","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nage_df = all_data[['Age','Pclass','Sex','Title']]\nage_df = pd.get_dummies(age_df)\n#只会将分类变量变成虚拟变量，不会将连续的数值变量变成虚拟变量\nage_df","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:08.762135Z","iopub.execute_input":"2022-07-10T03:21:08.763016Z","iopub.status.idle":"2022-07-10T03:21:09.119132Z","shell.execute_reply.started":"2022-07-10T03:21:08.76298Z","shell.execute_reply":"2022-07-10T03:21:09.11804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"known_age = age_df[age_df.Age.notnull()].values\nunknown_age = age_df[age_df.Age.isnull()].values\nknown_age","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.120639Z","iopub.execute_input":"2022-07-10T03:21:09.120991Z","iopub.status.idle":"2022-07-10T03:21:09.132723Z","shell.execute_reply.started":"2022-07-10T03:21:09.120959Z","shell.execute_reply":"2022-07-10T03:21:09.131885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = known_age[:,0]\nX = known_age[:,1:]\nrfr = RandomForestRegressor(random_state=0,n_estimators=100,n_jobs=-1)\nrfr.fit(X,y)\npredictedAge = rfr.predict(unknown_age[:,1::])\nall_data.loc[(all_data.Age.isnull()) ,'Age'] = predictedAge","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.133845Z","iopub.execute_input":"2022-07-10T03:21:09.134315Z","iopub.status.idle":"2022-07-10T03:21:09.496932Z","shell.execute_reply.started":"2022-07-10T03:21:09.134268Z","shell.execute_reply":"2022-07-10T03:21:09.495931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictedAge","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.500158Z","iopub.execute_input":"2022-07-10T03:21:09.500652Z","iopub.status.idle":"2022-07-10T03:21:09.511545Z","shell.execute_reply.started":"2022-07-10T03:21:09.500605Z","shell.execute_reply":"2022-07-10T03:21:09.510377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"embarked 也有2个缺失值，根据这个个体的其他信息，直接填充","metadata":{}},{"cell_type":"code","source":"all_data[all_data['Embarked'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.519358Z","iopub.execute_input":"2022-07-10T03:21:09.519836Z","iopub.status.idle":"2022-07-10T03:21:09.543058Z","shell.execute_reply.started":"2022-07-10T03:21:09.519791Z","shell.execute_reply":"2022-07-10T03:21:09.541795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data.groupby(by=['Pclass','Embarked']).Fare.median()  #用fare的中位数","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.544642Z","iopub.execute_input":"2022-07-10T03:21:09.544997Z","iopub.status.idle":"2022-07-10T03:21:09.557397Z","shell.execute_reply.started":"2022-07-10T03:21:09.544964Z","shell.execute_reply":"2022-07-10T03:21:09.556342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['Embarked'] = all_data['Embarked'].fillna('C')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.558875Z","iopub.execute_input":"2022-07-10T03:21:09.559258Z","iopub.status.idle":"2022-07-10T03:21:09.566139Z","shell.execute_reply.started":"2022-07-10T03:21:09.559225Z","shell.execute_reply":"2022-07-10T03:21:09.565135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"fare 的缺失值，用中位数填充","metadata":{}},{"cell_type":"code","source":"all_data[all_data['Fare'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.567547Z","iopub.execute_input":"2022-07-10T03:21:09.56817Z","iopub.status.idle":"2022-07-10T03:21:09.590593Z","shell.execute_reply.started":"2022-07-10T03:21:09.568135Z","shell.execute_reply":"2022-07-10T03:21:09.589371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fare = all_data[(all_data['Embarked'] == 'S') & (all_data['Pclass'] == 3)].Fare.median()\nall_data['Fare'] = all_data['Fare'].fillna(fare)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.592276Z","iopub.execute_input":"2022-07-10T03:21:09.592905Z","iopub.status.idle":"2022-07-10T03:21:09.604035Z","shell.execute_reply.started":"2022-07-10T03:21:09.592868Z","shell.execute_reply":"2022-07-10T03:21:09.603026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"2.异常值处理         \n- 多人家庭中没有获救的女性和儿童为异常值","metadata":{}},{"cell_type":"code","source":"all_data['Surname'] = all_data['Name'].apply(lambda x : x.split(',')[0].strip()) ","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.605749Z","iopub.execute_input":"2022-07-10T03:21:09.606558Z","iopub.status.idle":"2022-07-10T03:21:09.614356Z","shell.execute_reply.started":"2022-07-10T03:21:09.606519Z","shell.execute_reply":"2022-07-10T03:21:09.613347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Surname_count = dict(all_data['Surname'].value_counts())\nall_data['Family_count'] = all_data['Surname'].apply(lambda x : Surname_count[x])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.616024Z","iopub.execute_input":"2022-07-10T03:21:09.616658Z","iopub.status.idle":"2022-07-10T03:21:09.764106Z","shell.execute_reply.started":"2022-07-10T03:21:09.616624Z","shell.execute_reply":"2022-07-10T03:21:09.762986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#找出所有处于多人家庭的儿童和妇女和成年男性\nFemale_Child_Count = all_data.loc[(all_data['Family_count'] >= 2) & ((all_data['Age'] <= 12) | (all_data['Sex'] == 'female'))]\nMale_Adult_Count = all_data.loc[(all_data['Family_count'] >= 2) & (all_data['Age'] > 12) & (all_data['Sex'] == 'male')]","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.765649Z","iopub.execute_input":"2022-07-10T03:21:09.76629Z","iopub.status.idle":"2022-07-10T03:21:09.780439Z","shell.execute_reply.started":"2022-07-10T03:21:09.766253Z","shell.execute_reply":"2022-07-10T03:21:09.779183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Female_Child_Count","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.782045Z","iopub.execute_input":"2022-07-10T03:21:09.783033Z","iopub.status.idle":"2022-07-10T03:21:09.822348Z","shell.execute_reply.started":"2022-07-10T03:21:09.782981Z","shell.execute_reply":"2022-07-10T03:21:09.821472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Female_Child = pd.DataFrame(Female_Child_Count.groupby('Surname')['Survived'].mean().value_counts())\nFemale_Child.columns = ['GroupCount']\nFemale_Child","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.823736Z","iopub.execute_input":"2022-07-10T03:21:09.824308Z","iopub.status.idle":"2022-07-10T03:21:09.840696Z","shell.execute_reply.started":"2022-07-10T03:21:09.824273Z","shell.execute_reply":"2022-07-10T03:21:09.83952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(x = Female_Child.index , y = Female_Child['GroupCount'] , data = all_data).set_xlabel('AverageSurvived')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:09.842132Z","iopub.execute_input":"2022-07-10T03:21:09.843077Z","iopub.status.idle":"2022-07-10T03:21:10.040151Z","shell.execute_reply.started":"2022-07-10T03:21:09.843042Z","shell.execute_reply":"2022-07-10T03:21:10.038875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Male_Adult = pd.DataFrame(Male_Adult_Count.groupby('Surname')['Survived'].mean().value_counts())\nMale_Adult.columns = ['GroupCount']\nMale_Adult","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:10.041829Z","iopub.execute_input":"2022-07-10T03:21:10.042841Z","iopub.status.idle":"2022-07-10T03:21:10.058907Z","shell.execute_reply.started":"2022-07-10T03:21:10.042792Z","shell.execute_reply":"2022-07-10T03:21:10.057666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"普遍规律是女性和儿童的幸存率较高， 成年男性的幸存率较低，所以把不符合普遍规律的反常组选出来单独处理。          \n把女性和儿童组里幸存率为0的组设置为遇难组，成年男性组里存活率为1的组设为幸存组，推测处于遇难组的女性和儿童幸存的可能性可能较低，处于幸存组的成年男性幸存的可能性较高。","metadata":{}},{"cell_type":"code","source":"Female_Child_Count = Female_Child_Count.groupby('Surname')['Survived'].mean()\nDead_List = set(Female_Child_Count[Female_Child_Count.apply(lambda x : x==0)].index)\nDead_List","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:10.060208Z","iopub.execute_input":"2022-07-10T03:21:10.060739Z","iopub.status.idle":"2022-07-10T03:21:10.073777Z","shell.execute_reply.started":"2022-07-10T03:21:10.060702Z","shell.execute_reply":"2022-07-10T03:21:10.072501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Male_Adult_Count = Male_Adult_Count.groupby('Surname')['Survived'].mean()\nSurvived_List = set(Male_Adult_Count[Male_Adult_Count.apply(lambda x : x== 1)].index)\nSurvived_List","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:10.075827Z","iopub.execute_input":"2022-07-10T03:21:10.076554Z","iopub.status.idle":"2022-07-10T03:21:10.088778Z","shell.execute_reply.started":"2022-07-10T03:21:10.076508Z","shell.execute_reply":"2022-07-10T03:21:10.087202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"将测试集中的异常值改为正常值","metadata":{}},{"cell_type":"code","source":"#将测试集中所有幸存组的成员改成女性和儿童，将遇难组的都改成男性\ntrain = all_data.loc[all_data['Survived'].notnull()]\ntest = all_data.loc[all_data['Survived'].isnull()]\n\ntest.loc[(test['Surname'].apply(lambda x : x in Dead_List)),'Sex'] = 'male'\ntest.loc[(test['Surname'].apply(lambda x : x in Dead_List)),'Age'] = 60\ntest.loc[(test['Surname'].apply(lambda x : x in Dead_List)),'Title'] = 'Mr'\ntest.loc[(test['Surname'].apply(lambda x : x in Survived_List)),'Age'] = 5\ntest.loc[(test['Surname'].apply(lambda x : x in Survived_List)),'Sex'] = 'female'\ntest.loc[(test['Surname'].apply(lambda x : x in Survived_List)),'Title'] = 'Miss'","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:10.090114Z","iopub.execute_input":"2022-07-10T03:21:10.091293Z","iopub.status.idle":"2022-07-10T03:21:10.109018Z","shell.execute_reply.started":"2022-07-10T03:21:10.091251Z","shell.execute_reply":"2022-07-10T03:21:10.107924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 特征转换","metadata":{}},{"cell_type":"code","source":"\nall_data = pd.concat([train,test])\nall_data = all_data[['Survived','Pclass','Sex','Age','Fare','Embarked','Title','FamilySize','Desk','TicketGroup']]\nall_data = pd.get_dummies(all_data)\ntrain = all_data[all_data['Survived'].notnull()]\ntest = all_data[all_data['Survived'].isnull()].drop('Survived',axis = 1)\nX = train.values[:,1:]\ny = train.values[:,0]","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:10.110379Z","iopub.execute_input":"2022-07-10T03:21:10.110878Z","iopub.status.idle":"2022-07-10T03:21:10.13473Z","shell.execute_reply.started":"2022-07-10T03:21:10.110846Z","shell.execute_reply":"2022-07-10T03:21:10.133769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['Survived'].isnull()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:10.135912Z","iopub.execute_input":"2022-07-10T03:21:10.136402Z","iopub.status.idle":"2022-07-10T03:21:10.143868Z","shell.execute_reply.started":"2022-07-10T03:21:10.136371Z","shell.execute_reply":"2022-07-10T03:21:10.143029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:10.144983Z","iopub.execute_input":"2022-07-10T03:21:10.145482Z","iopub.status.idle":"2022-07-10T03:21:10.166056Z","shell.execute_reply.started":"2022-07-10T03:21:10.145451Z","shell.execute_reply":"2022-07-10T03:21:10.164693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.pipeline import Pipeline\nfrom sklearn.ensemble import  RandomForestClassifier\nfrom sklearn.model_selection import  GridSearchCV\nfrom sklearn.feature_selection import SelectKBest\nfrom warnings import simplefilter\nsimplefilter(action='ignore', category=FutureWarning)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:10.167905Z","iopub.execute_input":"2022-07-10T03:21:10.169011Z","iopub.status.idle":"2022-07-10T03:21:10.191575Z","shell.execute_reply.started":"2022-07-10T03:21:10.168888Z","shell.execute_reply":"2022-07-10T03:21:10.190361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe = Pipeline([('select', SelectKBest(k=20)),\n            ('classify',RandomForestClassifier(random_state=10,max_features='sqrt'))\n            ])\nparm_test = {'classify__n_estimators':list(range(15,30,2)),\n            'classify__max_depth':list(range(3,20,2))\n            }\n\ngsearch = GridSearchCV(estimator=pipe,param_grid=parm_test,scoring='roc_auc',cv = 10)\ngsearch.fit(X,y)\nprint(gsearch.best_params_,gsearch.best_score_)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:10.193485Z","iopub.execute_input":"2022-07-10T03:21:10.194099Z","iopub.status.idle":"2022-07-10T03:21:49.248816Z","shell.execute_reply.started":"2022-07-10T03:21:10.194047Z","shell.execute_reply":"2022-07-10T03:21:49.247552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.pipeline import make_pipeline\nselect = SelectKBest( k = 20)\nclf = RandomForestClassifier(random_state=10,warm_start=True,\n                            n_estimators = 27,\n                            max_depth = 7,\n                            max_features = 'sqrt'\n                            )\npipeline = make_pipeline(select,clf)\npipeline.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:49.250027Z","iopub.execute_input":"2022-07-10T03:21:49.250393Z","iopub.status.idle":"2022-07-10T03:21:49.315098Z","shell.execute_reply.started":"2022-07-10T03:21:49.250361Z","shell.execute_reply":"2022-07-10T03:21:49.313917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom sklearn import model_selection,metrics\ncv_score = model_selection.cross_val_score(pipeline,X,y,cv = 10)\nprint('CV score: Mean-%.7g | Std -%.7g' % (np.mean(cv_score),np.std(cv_score)))","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:21:49.316717Z","iopub.execute_input":"2022-07-10T03:21:49.31737Z","iopub.status.idle":"2022-07-10T03:21:49.84154Z","shell.execute_reply.started":"2022-07-10T03:21:49.317332Z","shell.execute_reply":"2022-07-10T03:21:49.840298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = pipeline.predict(test)\nsubmission = pd.DataFrame({'PassengerID' : test.index+1 , 'Survived': predictions.astype(np.int32)})\nsubmission.to_csv(r'./submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T03:22:19.361583Z","iopub.execute_input":"2022-07-10T03:22:19.362931Z","iopub.status.idle":"2022-07-10T03:22:19.384572Z","shell.execute_reply.started":"2022-07-10T03:22:19.362872Z","shell.execute_reply":"2022-07-10T03:22:19.383376Z"},"trusted":true},"execution_count":null,"outputs":[]}]}