<jupyter_start><jupyter_text>Logistic regression binary response variables (Y)- 0 or 1 Xs can be numerical or categorical<jupyter_code>%matplotlib inline

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import statsmodels.api as sm

df = pd.read_csv("trainT.csv") #titanic 
df.head(5)
df.shape
df.isnull().sum() #number of nas in a column
df = df[["Survived","Pclass","Age","Fare"]]
df=df.dropna() #drops nas
df.head(7)
plt.figure(figsize=(6,4))
fig, ax = plt.subplots()
df.Survived.value_counts().plot(kind='barh', color="blue", alpha=.65)
ax.set_ylim(-1, len(df.Survived.value_counts())) 
plt.title("Survival Breakdown (1 = Survived, 0 = Died)")
sns.factorplot(x="Pclass", y="Fare", hue="Survived", data=df, kind="box")
#formula = 'Survived ~ C(Pclass) + C(Sex) + Age + Fare'  #c indicates categorical
y=df[['Survived']]
print(type(y))
x=df[["Pclass","Age","Fare"]]
print(type(x))
# Make the model
logit =sm.Logit(y, x.astype(float)) #import statsmodels.api as sm
 
# Fit the model
result = logit.fit()
print result.summary()
#log [p/(1-p)] = -.28*Pclass + .0146 * faree -0.01*Age
#how a 1 unit increase or decrease in a variable affects the odds of surviving
#Number of successes:1 failure
  # odds
  print np.exp(result.params)
#odds that passengers die increase by a factor of 0.98 for each unit change in age.
#prob = odds / (1 + odds) .
#probability of finding someone dead on basis of age = 0.98/(1+0.98)

from patsy import dmatrices
import pandas as pd
from sklearn.linear_model import LogisticRegression
import statsmodels.discrete.discrete_model as sm

df2=pd.read_csv("trainT.csv") 
df2.head(7)
df2 = df2[["Survived","Pclass","Sex","Age","Fare"]]
df2.head(6)
df2=df2.dropna()
df2.head(6)
y, X = dmatrices('Survived ~ C(Pclass) + C(Sex) + Age + Fare', df2, return_type = 'dataframe')
#c indicates categorical

# sklearn output
model = LogisticRegression(fit_intercept = False, C = 1e9)
mdl = model.fit(X, y)
model.coef_

logit = sm.Logit(y, X)
logit.fit().params
# Fit the model
result = logit.fit()
print result.summary()
# create a results dictionary to hold our regression results for easy analysis later        
results = {} 
#http://hamelg.blogspot.co.uk/2015/11/python-for-data-analysis-part-28.html
# create a regression friendly dataframe using patsy's dmatrices function
y,x = dmatrices(formula, data=df, return_type='dataframe')

# instantiate our model
model = sm.Logit(y,x)

# fit our model to the training data
res = model.fit()

# save the result for outputing predictions later
results['Logit'] = [res, formula]
res.summary()
# fare is not statistically significant
formula = 'Survived ~ C(Pclass) + C(Sex) + Age' 
results = {} 
# create a regression friendly dataframe using patsy's dmatrices function
y,x = dmatrices(formula, data=df, return_type='dataframe')

# instantiate our model
model = sm.Logit(y,x)

# fit our model to the training data
res = model.fit()

# save the result for outputing predictions later
results['Logit'] = [res, formula]
res.summary()<jupyter_output>Optimization terminated successfully.
         Current function value: 0.453279
         Iterations 6
StarcoderdataJupyterStructuredCleanDedup