# Logistic Regression Algorithm

## Logistic Regression Analysis with Python

`import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as sns%matplotlib inline`
`train = pd.read_csv('titanic_train.csv')train.head()`
`plt.figure(figsize=(10,6))sns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap="Greens")`
`sns.set_style('whitegrid')sns.countplot(x='Survived',data=train,palette='pastel')`
`sns.set_style('whitegrid')sns.countplot(x='Survived',hue='Sex',data=train,palette='RdBu_r')`
`sns.set_style('whitegrid')sns.countplot(x='Survived',hue='Pclass',data=train,palette='viridis')`
`plt.figure(figsize=(10,7))sns.distplot(train["Age"].dropna(),kde=False,bins=30);`
`plt.figure(figsize=(12, 7))sns.boxplot(x='Pclass',y='Age',data=train,palette='viridis')`
`def trans_age(cols):    Age = cols[0]    Pclass = cols[1]        if pd.isnull(Age):                if Pclass == 1:            return 37         elif Pclass == 2:            return 29        else:            return 24    else:        return Age`
`train['Age'] = train[['Age','Pclass']].apply(trans_age,axis=1)`
`train.drop('Cabin',axis=1,inplace=True)train.head()`
`plt.figure(figsize=(10,6))sns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap="Greens")`
`sex = pd.get_dummies(train['Sex'],drop_first=True)embark = pd.get_dummies(train['Embarked'],drop_first=True)train.drop(['Sex','Embarked','Name','Ticket'],axis=1,inplace=True)`
`train = pd.concat([train,sex,embark],axis=1)train.head()`
`from sklearn.model_selection import train_test_splitX = train.drop('Survived',axis=1)y = train['Survived']X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=42)`
`from sklearn.linear_model import LogisticRegressionlogmodel = LogisticRegression()logmodel.fit(X_train,y_train)`
`predictions = logmodel.predict(X_test)`
`from sklearn.metrics import classification_report, confusion_matrixprint(confusion_matrix(y_test,predictions))print("\n")print(classification_report(y_test,predictions))`

--

--

--

## More from Orhan Yagizer Cinar

Founder @ Codecort |Leader @ YOOOTH| Yeti & YetGen 21'2| Advisory Board Member @ GelecektekiSen| Blogger | Data Science orhanyagizercinar.com

Love podcasts or audiobooks? Learn on the go with our new app.

## Orhan Yagizer Cinar

Founder @ Codecort |Leader @ YOOOTH| Yeti & YetGen 21'2| Advisory Board Member @ GelecektekiSen| Blogger | Data Science orhanyagizercinar.com