Created
August 5, 2013 23:10
-
-
Save rsivapr/6160484 to your computer and use it in GitHub Desktop.
Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import statsmodels.api as sm | |
import pylab as pl | |
import numpy as np | |
import sklearn.metrics | |
print "Reading DATA.CSV ... \n" | |
df = pd.read_csv('data.csv') | |
df.columns = ["numb", "age","frailty","sex","val"] | |
for i in range(df.shape[0]): | |
if df['sex'][i] == 'M': | |
df['sex'][i] = 1 | |
else: | |
df['sex'][i] = 0 | |
dummy_ranks = pd.get_dummies(df['sex']) | |
cols_to_keep = ['val','age','frailty'] | |
data = df[cols_to_keep].join(dummy_ranks.ix[:, 'M' :]) | |
data['intercept'] = 1 | |
print "Training model ... \n" | |
train_cols = data.columns[1:] | |
logit = sm.Logit(data['val'], data[train_cols]) | |
result = logit.fit() | |
print "\n\n\n\nSUMMARY OF REGRESSION \n==============================================================================" | |
print result.summary() | |
print "95% CONFIDENCE INTERVALS \n==============================================================================" | |
print result.conf_int() | |
prediction = result.predict( data[train_cols] ) | |
y = np.array(data['val']) | |
pred = np.array(prediction) | |
fpr, tpr, thr = sklearn.metrics.roc_curve(y, pred) | |
#Calculating AUC | |
print "Calculating AUC ...\n" | |
print "AUC = ", sklearn.metrics.auc(fpr, tpr) | |
#plotting curve | |
print "\nPlotting the ROC curve ...\n" | |
pl.clf() | |
pl.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % sklearn.metrics.auc(fpr, tpr)) | |
inp = raw_input("Name the file (.png): \n") | |
pl.savefig(inp) | |
print "File %s.png saved in the current directory." % inp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
argparse==1.2.1 | |
distribute==0.7.3 | |
matplotlib==1.3.0 | |
nose==1.3.0 | |
numpy==1.7.1 | |
pandas==0.12.0 | |
patsy==0.1.0 | |
pyparsing==2.0.1 | |
python-dateutil==2.1 | |
pytz==2013b | |
scikit-learn==0.13.1 | |
scipy==0.12.0 | |
six==1.3.0 | |
statsmodels==0.5.0 | |
tornado==3.1 | |
wsgiref==0.1.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment