{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:52.598916Z","iopub.execute_input":"2022-07-28T08:12:52.599610Z","iopub.status.idle":"2022-07-28T08:12:52.605476Z","shell.execute_reply.started":"2022-07-28T08:12:52.599561Z","shell.execute_reply":"2022-07-28T08:12:52.604247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('../input/titanic/train.csv')\ndf","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:53.359724Z","iopub.execute_input":"2022-07-28T08:12:53.360357Z","iopub.status.idle":"2022-07-28T08:12:53.392272Z","shell.execute_reply.started":"2022-07-28T08:12:53.360304Z","shell.execute_reply":"2022-07-28T08:12:53.391405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#visualize data\n#Survived and Sex\n#we can see that more female survived compare to male\nsns.countplot(df['Survived'],hue=df['Sex'])","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:53.394017Z","iopub.execute_input":"2022-07-28T08:12:53.394576Z","iopub.status.idle":"2022-07-28T08:12:53.782103Z","shell.execute_reply.started":"2022-07-28T08:12:53.394541Z","shell.execute_reply":"2022-07-28T08:12:53.780781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#more people were traveling single approx >600 people travel single \nsns.countplot(df['Survived'],hue=df['Parch'])","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:53.783905Z","iopub.execute_input":"2022-07-28T08:12:53.784620Z","iopub.status.idle":"2022-07-28T08:12:54.024917Z","shell.execute_reply.started":"2022-07-28T08:12:53.784570Z","shell.execute_reply":"2022-07-28T08:12:54.023817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Sibsp and Survived\n\nsns.countplot(df['SibSp'],hue=df['Survived'])","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:54.027359Z","iopub.execute_input":"2022-07-28T08:12:54.027804Z","iopub.status.idle":"2022-07-28T08:12:54.273333Z","shell.execute_reply.started":"2022-07-28T08:12:54.027769Z","shell.execute_reply":"2022-07-28T08:12:54.272360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!! we will make a new columns family =Sibsp+Parch+1,and drop SibSp,Parch","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:54.274548Z","iopub.execute_input":"2022-07-28T08:12:54.274833Z","iopub.status.idle":"2022-07-28T08:12:54.279490Z","shell.execute_reply.started":"2022-07-28T08:12:54.274807Z","shell.execute_reply":"2022-07-28T08:12:54.278453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Survived and Pclass\n#more pepole not survived who travell in 3rd class  compare to 1at and 2nd and also more pepole were travling in 2rd class\nsns.countplot(df['Pclass'],hue=df['Survived'])","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:54.280757Z","iopub.execute_input":"2022-07-28T08:12:54.281713Z","iopub.status.idle":"2022-07-28T08:12:54.485295Z","shell.execute_reply.started":"2022-07-28T08:12:54.281678Z","shell.execute_reply":"2022-07-28T08:12:54.484189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#visualize numeric data\n#age have right skeewed  distribution\nsns.distplot(df['Age'],hist=False,color='black')","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:54.486728Z","iopub.execute_input":"2022-07-28T08:12:54.487050Z","iopub.status.idle":"2022-07-28T08:12:54.678467Z","shell.execute_reply.started":"2022-07-28T08:12:54.487022Z","shell.execute_reply":"2022-07-28T08:12:54.677585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#age have outlier\nsns.boxplot(df['Age'],color='green')","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:54.679914Z","iopub.execute_input":"2022-07-28T08:12:54.681123Z","iopub.status.idle":"2022-07-28T08:12:54.843753Z","shell.execute_reply.started":"2022-07-28T08:12:54.681080Z","shell.execute_reply":"2022-07-28T08:12:54.842675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Fare have more right skeewed data\nsns.distplot(df['Fare'],color='black',hist=False)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:54.845239Z","iopub.execute_input":"2022-07-28T08:12:54.845593Z","iopub.status.idle":"2022-07-28T08:12:55.051365Z","shell.execute_reply.started":"2022-07-28T08:12:54.845564Z","shell.execute_reply":"2022-07-28T08:12:55.050375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#Fare have more outlier\nsns.boxplot(df['Fare'],color='Green')","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:55.054606Z","iopub.execute_input":"2022-07-28T08:12:55.054931Z","iopub.status.idle":"2022-07-28T08:12:55.208541Z","shell.execute_reply.started":"2022-07-28T08:12:55.054902Z","shell.execute_reply":"2022-07-28T08:12:55.206979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #chek multicollinearity\n# from statsmodels.stats.outliers_influence import variance_inflation_factor\n# vif_data = pd.DataFrame()\n# vif_data[\"feature\"] = df.columns\n# vif_data[\"VIF\"] = [variance_inflation_factor(df.values, i)\n#                           for i in range(len(df.columns))]","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:55.210296Z","iopub.execute_input":"2022-07-28T08:12:55.210625Z","iopub.status.idle":"2022-07-28T08:12:55.214970Z","shell.execute_reply.started":"2022-07-28T08:12:55.210597Z","shell.execute_reply":"2022-07-28T08:12:55.214141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:55.215756Z","iopub.execute_input":"2022-07-28T08:12:55.216011Z","iopub.status.idle":"2022-07-28T08:12:55.233657Z","shell.execute_reply.started":"2022-07-28T08:12:55.215986Z","shell.execute_reply":"2022-07-28T08:12:55.232796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 2 outlier present in Age\n# 20 outlier present in Fare\n# 177 missing values in Age\n# 687 null values in Cabin\n# 2 null values in embarked\n# we will encode {Embarked,Cabin,Sex} column\n# Deal with Name and Ticket Columns","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:55.234975Z","iopub.execute_input":"2022-07-28T08:12:55.235501Z","iopub.status.idle":"2022-07-28T08:12:55.239244Z","shell.execute_reply.started":"2022-07-28T08:12:55.235471Z","shell.execute_reply":"2022-07-28T08:12:55.238513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#add family column remove Parch and SibSp","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:55.240384Z","iopub.execute_input":"2022-07-28T08:12:55.241272Z","iopub.status.idle":"2022-07-28T08:12:55.253392Z","shell.execute_reply.started":"2022-07-28T08:12:55.241240Z","shell.execute_reply":"2022-07-28T08:12:55.252552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df=df","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:55.369800Z","iopub.execute_input":"2022-07-28T08:12:55.370196Z","iopub.status.idle":"2022-07-28T08:12:55.375299Z","shell.execute_reply.started":"2022-07-28T08:12:55.370163Z","shell.execute_reply":"2022-07-28T08:12:55.374212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df['Family']=new_df['SibSp']+new_df['Parch']+1\nnew_df=new_df.drop(['SibSp','Parch'],axis=1)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:55.377390Z","iopub.execute_input":"2022-07-28T08:12:55.378395Z","iopub.status.idle":"2022-07-28T08:12:55.389794Z","shell.execute_reply.started":"2022-07-28T08:12:55.378290Z","shell.execute_reply":"2022-07-28T08:12:55.388795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:55.391431Z","iopub.execute_input":"2022-07-28T08:12:55.392112Z","iopub.status.idle":"2022-07-28T08:12:55.419904Z","shell.execute_reply.started":"2022-07-28T08:12:55.392067Z","shell.execute_reply":"2022-07-28T08:12:55.418792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#countplot of Survived and  Family\nsns.countplot(new_df['Survived'],hue=new_df['Family'])","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:55.941687Z","iopub.execute_input":"2022-07-28T08:12:55.942895Z","iopub.status.idle":"2022-07-28T08:12:56.206972Z","shell.execute_reply.started":"2022-07-28T08:12:55.942833Z","shell.execute_reply":"2022-07-28T08:12:56.205865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.describe()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:56.209747Z","iopub.execute_input":"2022-07-28T08:12:56.210398Z","iopub.status.idle":"2022-07-28T08:12:56.243712Z","shell.execute_reply.started":"2022-07-28T08:12:56.210349Z","shell.execute_reply":"2022-07-28T08:12:56.242881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# <10% pepole have age >50 \n# <10% pepople have age<10\n#so we fiill null value with random value have range between (10,60) \nnew_df[new_df['Age']<10].shape[0],new_df[new_df['Age']>50].shape[0]","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:56.245512Z","iopub.execute_input":"2022-07-28T08:12:56.245942Z","iopub.status.idle":"2022-07-28T08:12:56.255550Z","shell.execute_reply.started":"2022-07-28T08:12:56.245901Z","shell.execute_reply":"2022-07-28T08:12:56.254695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fill null values of age\nfor i in range(new_df.shape[0]):\n    if np.isnan(new_df['Age'][i]):\n        a=np.random.randint(10,60)\n        new_df['Age'][i]=a\n\n        ","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:56.256648Z","iopub.execute_input":"2022-07-28T08:12:56.257423Z","iopub.status.idle":"2022-07-28T08:12:56.333462Z","shell.execute_reply.started":"2022-07-28T08:12:56.257379Z","shell.execute_reply":"2022-07-28T08:12:56.332141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#we can see that age is now is more normel Distributed \nplt.figure(figsize=(12,8)) \nplt.subplot(1, 2, 1)\nsns.distplot(new_df['Age'],hist=False,color='black')\nplt.subplot(1, 2, 2)\nsns.distplot(df['Age'],hist=False,color='yellow')","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:56.337385Z","iopub.execute_input":"2022-07-28T08:12:56.338092Z","iopub.status.idle":"2022-07-28T08:12:56.659670Z","shell.execute_reply.started":"2022-07-28T08:12:56.338047Z","shell.execute_reply":"2022-07-28T08:12:56.658868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#now work on Fare Column\n#approx 9%pepole have Fare >100 and approx 2.5 % pepole have Fare >200 \nnew_df[new_df['Fare']>60].shape[0],new_df[new_df['Fare']>200].shape[0]","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:56.660984Z","iopub.execute_input":"2022-07-28T08:12:56.661357Z","iopub.status.idle":"2022-07-28T08:12:56.671941Z","shell.execute_reply.started":"2022-07-28T08:12:56.661294Z","shell.execute_reply":"2022-07-28T08:12:56.670756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#apply  Quantile\nQ3=new_df['Fare'].quantile(0.75)\nQ1=new_df['Fare'].quantile(0.25)\nIQR=Q3-Q1\nupper_limit=Q3+1.5*IQR\nlower_limit=Q1-1.5*IQR","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:56.673574Z","iopub.execute_input":"2022-07-28T08:12:56.674458Z","iopub.status.idle":"2022-07-28T08:12:56.685938Z","shell.execute_reply.started":"2022-07-28T08:12:56.674419Z","shell.execute_reply":"2022-07-28T08:12:56.684928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df['Fare']=np.where(new_df['Fare']>upper_limit,upper_limit,np.where(new_df['Fare']<lower_limit,lower_limit,new_df['Fare']))\nnew_df.describe()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:56.687684Z","iopub.execute_input":"2022-07-28T08:12:56.688050Z","iopub.status.idle":"2022-07-28T08:12:56.722463Z","shell.execute_reply.started":"2022-07-28T08:12:56.688019Z","shell.execute_reply":"2022-07-28T08:12:56.721450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#we can see that Fare is not more right_skeewed \nplt.figure(figsize=(12,8)) \nplt.subplot(1, 2, 1)\nsns.distplot(new_df['Fare'],hist=False,color='black')\nplt.subplot(1, 2, 2)\nsns.distplot(df['Fare'],hist=False,color='red')","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:56.724913Z","iopub.execute_input":"2022-07-28T08:12:56.725491Z","iopub.status.idle":"2022-07-28T08:12:57.077524Z","shell.execute_reply.started":"2022-07-28T08:12:56.725458Z","shell.execute_reply":"2022-07-28T08:12:57.076266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.isnan(new_df['Cabin'][888])","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.079284Z","iopub.execute_input":"2022-07-28T08:12:57.080024Z","iopub.status.idle":"2022-07-28T08:12:57.088904Z","shell.execute_reply.started":"2022-07-28T08:12:57.079972Z","shell.execute_reply":"2022-07-28T08:12:57.087794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#work on Cat.. data\n#fill null values of  Cabin with NA and Embarked with S BCZ  most frequent_value is S\nnew_df['Cabin'].fillna(value='NA',inplace=True)\nnew_df['Embarked'].fillna(value='S',inplace=True)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.091162Z","iopub.execute_input":"2022-07-28T08:12:57.091572Z","iopub.status.idle":"2022-07-28T08:12:57.100434Z","shell.execute_reply.started":"2022-07-28T08:12:57.091538Z","shell.execute_reply":"2022-07-28T08:12:57.099469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.isnull().sum()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.101775Z","iopub.execute_input":"2022-07-28T08:12:57.102644Z","iopub.status.idle":"2022-07-28T08:12:57.123058Z","shell.execute_reply.started":"2022-07-28T08:12:57.102609Z","shell.execute_reply":"2022-07-28T08:12:57.122164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# in null values not present and now we encode DATA \nnew_df.info()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.124680Z","iopub.execute_input":"2022-07-28T08:12:57.125247Z","iopub.status.idle":"2022-07-28T08:12:57.140771Z","shell.execute_reply.started":"2022-07-28T08:12:57.125212Z","shell.execute_reply":"2022-07-28T08:12:57.139538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#map Gender(nominal) and Embarked(ordinal)\nnew_df['Sex']=new_df['Sex'].map({'male':1,'female':0})\nnew_df['Embarked']=new_df['Embarked'].map({'S':0,'Q':1,'C':2})\nnew_df","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.146432Z","iopub.execute_input":"2022-07-28T08:12:57.146883Z","iopub.status.idle":"2022-07-28T08:12:57.176305Z","shell.execute_reply.started":"2022-07-28T08:12:57.146844Z","shell.execute_reply":"2022-07-28T08:12:57.175042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#in cabin we replace  value with first value of every row in Cabin\nfor i in range(new_df.shape[0]):\n    new_df['Cabin'][i]=new_df['Cabin'][i][0]","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.179169Z","iopub.execute_input":"2022-07-28T08:12:57.179876Z","iopub.status.idle":"2022-07-28T08:12:57.502913Z","shell.execute_reply.started":"2022-07-28T08:12:57.179825Z","shell.execute_reply":"2022-07-28T08:12:57.501522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df['Cabin'].unique()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.504591Z","iopub.execute_input":"2022-07-28T08:12:57.504950Z","iopub.status.idle":"2022-07-28T08:12:57.517931Z","shell.execute_reply.started":"2022-07-28T08:12:57.504920Z","shell.execute_reply":"2022-07-28T08:12:57.517023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#encode them ordinal case \nnew_df['Cabin']=new_df['Cabin'].map({'T':0,'N':1,'G':2,'F':3,'E':4,'D':5,'C':6,'B':7,'A':8})","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.519440Z","iopub.execute_input":"2022-07-28T08:12:57.520055Z","iopub.status.idle":"2022-07-28T08:12:57.532121Z","shell.execute_reply.started":"2022-07-28T08:12:57.520021Z","shell.execute_reply":"2022-07-28T08:12:57.531215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.info()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.533731Z","iopub.execute_input":"2022-07-28T08:12:57.534494Z","iopub.status.idle":"2022-07-28T08:12:57.552845Z","shell.execute_reply.started":"2022-07-28T08:12:57.534460Z","shell.execute_reply":"2022-07-28T08:12:57.551403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#deal with Ticket\nnew_df['Ticket']","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.554533Z","iopub.execute_input":"2022-07-28T08:12:57.555096Z","iopub.status.idle":"2022-07-28T08:12:57.569106Z","shell.execute_reply.started":"2022-07-28T08:12:57.555048Z","shell.execute_reply":"2022-07-28T08:12:57.567555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df['Ticket'][31]","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.572815Z","iopub.execute_input":"2022-07-28T08:12:57.573218Z","iopub.status.idle":"2022-07-28T08:12:57.581389Z","shell.execute_reply.started":"2022-07-28T08:12:57.573186Z","shell.execute_reply":"2022-07-28T08:12:57.579927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# i take only ticket number\nlst=[]\nfor i in range(new_df.shape[0]):\n    s=''\n    ticket=(new_df['Ticket'][i])[::-1]\n    for j in ticket:\n        if j==' ' or j=='.' or j>='A' and j<='Z':\n\n            break\n        else:\n            s+=j\n    lst.append(s[::-1])","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.583002Z","iopub.execute_input":"2022-07-28T08:12:57.583476Z","iopub.status.idle":"2022-07-28T08:12:57.603259Z","shell.execute_reply.started":"2022-07-28T08:12:57.583437Z","shell.execute_reply":"2022-07-28T08:12:57.601632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df['Ticket'][597]","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.605025Z","iopub.execute_input":"2022-07-28T08:12:57.605556Z","iopub.status.idle":"2022-07-28T08:12:57.616527Z","shell.execute_reply.started":"2022-07-28T08:12:57.605504Z","shell.execute_reply":"2022-07-28T08:12:57.615146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#index of LINE\nfor i in range(len(lst)):\n    if(lst[i]==''):\n        lst[i]='2343'\n        \n    lst[i]=float(lst[i])\n","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.618003Z","iopub.execute_input":"2022-07-28T08:12:57.618415Z","iopub.status.idle":"2022-07-28T08:12:57.626847Z","shell.execute_reply.started":"2022-07-28T08:12:57.618384Z","shell.execute_reply":"2022-07-28T08:12:57.625619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df['Ticket']=lst\nnew_df","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.628435Z","iopub.execute_input":"2022-07-28T08:12:57.629715Z","iopub.status.idle":"2022-07-28T08:12:57.658899Z","shell.execute_reply.started":"2022-07-28T08:12:57.629599Z","shell.execute_reply":"2022-07-28T08:12:57.657463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.describe()","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.660586Z","iopub.execute_input":"2022-07-28T08:12:57.661060Z","iopub.status.idle":"2022-07-28T08:12:57.709785Z","shell.execute_reply.started":"2022-07-28T08:12:57.661023Z","shell.execute_reply":"2022-07-28T08:12:57.708328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df['Name']","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.712475Z","iopub.execute_input":"2022-07-28T08:12:57.713013Z","iopub.status.idle":"2022-07-28T08:12:57.723061Z","shell.execute_reply.started":"2022-07-28T08:12:57.712959Z","shell.execute_reply":"2022-07-28T08:12:57.721857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name=[]\nfor i in range(new_df.shape[0]):\n    if 'Mrs' in new_df['Name'][i]:\n        name.append('Mrs')\n    elif 'Mr.' in new_df['Name'][i]:\n        name.append('Mr')\n    elif 'Miss' in new_df['Name'][i]:\n        name.append('Miss')\n    elif 'Master' in new_df['Name'][i]:\n        name.append('Master')\n    else:\n        name.append('Other')\n    ","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.724468Z","iopub.execute_input":"2022-07-28T08:12:57.724825Z","iopub.status.idle":"2022-07-28T08:12:57.751963Z","shell.execute_reply.started":"2022-07-28T08:12:57.724796Z","shell.execute_reply":"2022-07-28T08:12:57.750594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df['Name']=name","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.754242Z","iopub.execute_input":"2022-07-28T08:12:57.754725Z","iopub.status.idle":"2022-07-28T08:12:57.763525Z","shell.execute_reply.started":"2022-07-28T08:12:57.754687Z","shell.execute_reply":"2022-07-28T08:12:57.762522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.765713Z","iopub.execute_input":"2022-07-28T08:12:57.766081Z","iopub.status.idle":"2022-07-28T08:12:57.797189Z","shell.execute_reply.started":"2022-07-28T08:12:57.766050Z","shell.execute_reply":"2022-07-28T08:12:57.795544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df['Name']=new_df['Name'].map({'Mr':1,'Miss':0,'Mrs':2,'Master':3,'Other':4})","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.799061Z","iopub.execute_input":"2022-07-28T08:12:57.799641Z","iopub.status.idle":"2022-07-28T08:12:57.808541Z","shell.execute_reply.started":"2022-07-28T08:12:57.799601Z","shell.execute_reply":"2022-07-28T08:12:57.807298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.810366Z","iopub.execute_input":"2022-07-28T08:12:57.810744Z","iopub.status.idle":"2022-07-28T08:12:57.839302Z","shell.execute_reply.started":"2022-07-28T08:12:57.810713Z","shell.execute_reply":"2022-07-28T08:12:57.838098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#scaling with Ticket,Age,Fare","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.841172Z","iopub.execute_input":"2022-07-28T08:12:57.841554Z","iopub.status.idle":"2022-07-28T08:12:57.846909Z","shell.execute_reply.started":"2022-07-28T08:12:57.841521Z","shell.execute_reply":"2022-07-28T08:12:57.845780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()\nnew_df['Age']=scaler.fit_transform(new_df[['Age']])\nnew_df['Fare']=scaler.fit_transform(new_df[['Fare']])\nnew_df['Ticket']=scaler.fit_transform(new_df[['Ticket']])","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.848529Z","iopub.execute_input":"2022-07-28T08:12:57.848891Z","iopub.status.idle":"2022-07-28T08:12:57.874328Z","shell.execute_reply.started":"2022-07-28T08:12:57.848850Z","shell.execute_reply":"2022-07-28T08:12:57.872813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.876470Z","iopub.execute_input":"2022-07-28T08:12:57.876932Z","iopub.status.idle":"2022-07-28T08:12:57.898559Z","shell.execute_reply.started":"2022-07-28T08:12:57.876896Z","shell.execute_reply":"2022-07-28T08:12:57.897208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train the model\nfrom sklearn.model_selection import  train_test_split,cross_val_score\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier,VotingClassifier,BaggingClassifier,AdaBoostClassifier\nfrom sklearn.metrics  import r2_score,accuracy_score\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.neighbors import KNeighborsClassifier","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.900260Z","iopub.execute_input":"2022-07-28T08:12:57.900739Z","iopub.status.idle":"2022-07-28T08:12:57.910416Z","shell.execute_reply.started":"2022-07-28T08:12:57.900699Z","shell.execute_reply":"2022-07-28T08:12:57.909166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=new_df['Survived']\nx=new_df.drop(columns=['Survived'],axis=1)\nx.shape,y.shape","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.911718Z","iopub.execute_input":"2022-07-28T08:12:57.912093Z","iopub.status.idle":"2022-07-28T08:12:57.925295Z","shell.execute_reply.started":"2022-07-28T08:12:57.912061Z","shell.execute_reply":"2022-07-28T08:12:57.924162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=10)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.926920Z","iopub.execute_input":"2022-07-28T08:12:57.927425Z","iopub.status.idle":"2022-07-28T08:12:57.935262Z","shell.execute_reply.started":"2022-07-28T08:12:57.927375Z","shell.execute_reply":"2022-07-28T08:12:57.934244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#83 accuracy with LogisticRegression\nLR=LogisticRegression(max_iter=1000)\nLR.fit(x_train,y_train)\nlr_pred=LR.predict(x_test)\naccuracy_score(y_test,lr_pred)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:57.936973Z","iopub.execute_input":"2022-07-28T08:12:57.937307Z","iopub.status.idle":"2022-07-28T08:12:58.068385Z","shell.execute_reply.started":"2022-07-28T08:12:57.937279Z","shell.execute_reply":"2022-07-28T08:12:58.067139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#87 accuracy with Random forest \nRF=RandomForestClassifier(n_estimators=1000,n_jobs=-1,max_depth=100,max_samples=100)\nRF.fit(x_train,y_train)\nrf_pred=RF.predict(x_test)\naccuracy_score(y_test,rf_pred)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:12:58.070724Z","iopub.execute_input":"2022-07-28T08:12:58.071163Z","iopub.status.idle":"2022-07-28T08:13:00.934259Z","shell.execute_reply.started":"2022-07-28T08:12:58.071118Z","shell.execute_reply":"2022-07-28T08:13:00.932964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#82 accuracy of decision tree\nDT=DecisionTreeClassifier(max_depth=10,min_samples_split=2)\nDT.fit(x_train,y_train)\ndt_pred=DT.predict(x_test)\naccuracy_score(y_test,dt_pred)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:13:00.935765Z","iopub.execute_input":"2022-07-28T08:13:00.936211Z","iopub.status.idle":"2022-07-28T08:13:00.954139Z","shell.execute_reply.started":"2022-07-28T08:13:00.936171Z","shell.execute_reply":"2022-07-28T08:13:00.953162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SVM=SVC()\nKNN=KNeighborsClassifier(n_neighbors=3,n_jobs=-1)\nestimators=[('DT',DT),('KNN',KNN),('SVM',SVM)]","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:13:00.960093Z","iopub.execute_input":"2022-07-28T08:13:00.960457Z","iopub.status.idle":"2022-07-28T08:13:00.966295Z","shell.execute_reply.started":"2022-07-28T08:13:00.960429Z","shell.execute_reply":"2022-07-28T08:13:00.965558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#not improve with VotingClassifier\nVT=VotingClassifier(estimators=estimators,n_jobs=-1,weights=[500,1,1])\nVT.fit(x_train,y_train)\nvt_pred=VT.predict(x_test)\naccuracy_score(vt_pred,y_test)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:13:00.967298Z","iopub.execute_input":"2022-07-28T08:13:00.967696Z","iopub.status.idle":"2022-07-28T08:13:02.613630Z","shell.execute_reply.started":"2022-07-28T08:13:00.967661Z","shell.execute_reply":"2022-07-28T08:13:02.612392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Bagging Classifier : improve accuracy with bagging classifier\nBG=BaggingClassifier(base_estimator=DT,n_jobs=-1,n_estimators=100,max_samples=400,max_features=5)\nBG.fit(x_train,y_train)\nbg_pred=BG.predict(x_test)\naccuracy_score(bg_pred,y_test)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:13:02.615162Z","iopub.execute_input":"2022-07-28T08:13:02.615546Z","iopub.status.idle":"2022-07-28T08:13:03.228141Z","shell.execute_reply.started":"2022-07-28T08:13:02.615514Z","shell.execute_reply":"2022-07-28T08:13:03.227370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#good result with ada boost \nAD=AdaBoostClassifier(n_estimators=100,learning_rate=.4)\nAD.fit(x_train,y_train)\nad_pred=AD.predict(x_test)\naccuracy_score(ad_pred,y_test)","metadata":{"datalore":{"type":"CODE","hide_input_from_viewers":false,"hide_output_from_viewers":false},"execution":{"iopub.status.busy":"2022-07-28T08:13:03.230600Z","iopub.execute_input":"2022-07-28T08:13:03.231009Z","iopub.status.idle":"2022-07-28T08:13:03.483144Z","shell.execute_reply.started":"2022-07-28T08:13:03.230969Z","shell.execute_reply":"2022-07-28T08:13:03.482113Z"},"trusted":true},"execution_count":null,"outputs":[]}]}