{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# import dataset:\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d1ed67c33d65fee072b3092fe0339b29408c67f7"},"cell_type":"code","source":"# import dataset :\ndf=pd.read_csv(\"../input/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3c74394aae2786c9e64437a2312c94ee9c7ddfaa"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b07d9b31f00e4fd2c2b4a36d3f7499c980c6a4fb"},"cell_type":"code","source":"# some missing we can see with the help of seaborn:\n# visualization:\nsns.heatmap(df.isnull(),yticklabels=False,cbar=False,cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5a8056f6a08d4b362070baa73088dd1e988e0340"},"cell_type":"code","source":"# yellow is missing data:\n# so now going dealing with missing data:\nsns.set_style('whitegrid')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6f884cfe3dea2f2fbd8289aa1d5663f60997f4ff"},"cell_type":"code","source":"# with sex:\nsns.countplot(x='Survived',data=df,hue='Sex',palette='RdBu_r')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc587c65290abf62e25fe0779bc959d74bacc65c"},"cell_type":"code","source":"# now passengerclass:\nsns.countplot(x='Survived',hue='Pclass',data=df,palette='RdBu_r')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6598e64abecd22fd6017c60220003ae78cae6e18"},"cell_type":"code","source":"# age of the people on titanic:\nsns.distplot(df['Age'].dropna(),bins=20,kde=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"990efc96bacff58718cb24994bcc3da21d572e4e"},"cell_type":"code","source":"# explore other the column:\ndf.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68de32d73c43893d3f9c3886ab2db8685592ef3b"},"cell_type":"code","source":"sns.countplot(x='SibSp',data=df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"80169e6db6c005d3473446f483dbd1ebc136ffe9"},"cell_type":"code","source":"# Now check the fair (The price he pay):\nsns.distplot(df['Fare'],bins=20,kde=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6282d760b44470711721c59316b42dea86b1acee"},"cell_type":"code","source":"# Cleaning a data in this part:\nsns.boxplot(x='Pclass',y='Age',data=df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76c102bd467f9f66c233b0988e1b051a6e4b0333"},"cell_type":"code","source":"# filling the age cloumn:\ndef input_age(cols):\n    Age=cols[0]\n    Pclass=cols[1]\n    \n    if(pd.isnull(Age)):\n        if(Pclass==1):\n            return 37\n        if(Pclass==2):\n            return 29\n        else:\n            return 24\n    else:\n        return Age","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"97057f693febae6d2a7975c6cc5738720014d6b5"},"cell_type":"code","source":"df['Age']=df[['Age','Pclass']].apply(input_age,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca5bf33f27d87730b48ac776091dd01011b5fc31"},"cell_type":"code","source":"sns.heatmap(df.isnull(),yticklabels=False,cbar=False,cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bbe570186039d13c72443d22a9fc1577be173c04"},"cell_type":"code","source":"# for cabin column:\ndf.drop('Cabin',axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"46b56f3a4413b48142c7f35b7b3f0dd51caf1254"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d9471712736e70803b84df51f65a233ef382bf45"},"cell_type":"code","source":"sns.heatmap(df.isnull(),yticklabels=False,cbar=False,cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca2a30422ac54b9908a592ab55a0a7c875482a1f"},"cell_type":"code","source":"# cleaning the dataset( this make for machine learning model):\npd.get_dummies(df['Sex'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c695af642305f6002101295701d8346b8d07a92"},"cell_type":"code","source":"sex=pd.get_dummies(df['Sex'],drop_first=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba9b51aa884ba1a19e35f6da8d7f8d433bc6cf1d"},"cell_type":"code","source":"embarked=pd.get_dummies(df['Embarked'],drop_first=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"10b1594ae86096defee0e00ef92147d8906e4ea4"},"cell_type":"code","source":"df=pd.concat([df,sex,embarked],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6e9200fef736fa2f11643106138b13632c3389dc"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d2180d1089f64aa3eb92b1e13b6c9227d4a1efdb"},"cell_type":"code","source":"df.drop(['Sex','Embarked','Ticket'],axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"808b0847ef390ec03ea1dc43b6adb10a819973c3"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bf4fe313ae396d7afb88412ebbbcddf82668ce5c"},"cell_type":"code","source":"df.drop('PassengerId',axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5695f1110b67b23df4f7f48bbdc05f88ceae171f"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7d24ff3204b4048a91b77528e030e214782db32"},"cell_type":"code","source":"df.drop('Name',axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"98baef288601e0722e1d2b71903ef7f1f0e75a18"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"088ccfe2d0851bda2d65b7438923d58a42cb26ec"},"cell_type":"code","source":"# make dataset into dependent and independent set:\nX=df.iloc[:,1:8].values\ny=df.iloc[:,0].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"91e4755d7120af760f9ce968f0030da52eb82c53"},"cell_type":"code","source":"X","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"34497854793d07697a628ebf60d4db0c253b5d0d"},"cell_type":"code","source":"y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e83d69246d96aad2189e96086bdc2f0f87d2155b"},"cell_type":"code","source":"#  spliting the dataset into train and test set:\nfrom sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c735125ceffb5e47fc776bfcf96b0c12b171a307"},"cell_type":"code","source":"# feature scaling :\nfrom sklearn.preprocessing import StandardScaler\nsc=StandardScaler()\nX_train=sc.fit_transform(X_train)\nX_test=sc.fit_transform(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"45f1ca559fab5bf60cc2407124d2211ce1a8cb27"},"cell_type":"code","source":"#fiting the model into the Logistic classification:\nfrom sklearn.linear_model import LogisticRegression\nclassifier=LogisticRegression()\nclassifier.fit(X_train,y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"abdec7652b268dc8d799647d66964b91e4067295"},"cell_type":"code","source":"# prediction of new result:\ny_pred=classifier.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bed8d620a19789e405f0215b88d77d5e9fa2a7f5"},"cell_type":"code","source":"# making the classification repeort and confusion matrix:\nfrom sklearn.metrics import classification_report,confusion_matrix\ncm=confusion_matrix(y_test,y_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ae0af17ed849a6032eafbc172a8094408a90fdc4"},"cell_type":"code","source":"cm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dec227880c4e41a8397c5974772f288815640fc5"},"cell_type":"code","source":"cr=classification_report(y_test,y_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"750746c6c1692b3362bb8abfd37a5e80d5ecf8db"},"cell_type":"code","source":"cr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0345d77bb9f2cf172654fa17a0541b8b7b45f650"},"cell_type":"code","source":"(148+71)/268","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"56e3ecf27333e354d8ea0306c79c32c007506ee2"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}