<jupyter_start><jupyter_text>Logistic Regression classification with binary response variable<jupyter_code>train=pd.read_csv("trainT.csv")
test=pd.read_csv("testT.csv")
train.head(6)
df=train[['Survived','Pclass','Sex','Age','Fare']]
df.head(5)
#change male to 1 and female to 0
df["Sex"] = df["Sex"].apply(lambda sex:1 if sex=="male" else 0)
df.head(5) #sex as ctegorical varible
#handle missing values of age
df["Age"] = df["Age"].fillna(df["Age"].median())
df["Fare"] = df["Fare"].fillna(df["Fare"].median())
df.isnull().sum()
X_train = df.drop("Survived", axis=1) #predictors from training 
Y_train = df["Survived"]


logreg = LogisticRegression() #define the logistic regression model
logreg.fit(X_train, Y_train)
## lets look at the test data
test.head()
X_test=test[['Pclass','Sex','Age','Fare']]
X_test["Sex"] = X_test["Sex"].apply(lambda sex:1 if sex=="male" else 0)
X_test.isnull().sum()
X_test["Age"] = X_test["Age"].fillna(X_test["Age"].median())
X_test=X_test.dropna()
X_test.isnull().sum()
X_test.head(5)
Y_pred = logreg.predict(X_test)
acc_log = round(logreg.score(X_train, Y_train) * 100, 2) #accuracy of the model
acc_log
X_train.head(3)
Y_train.head(3)
g=X_train[['Sex']]
logreg = LogisticRegression() #define the logistic regression model
logreg.fit(g, Y_train)
gT=X_test[['Sex']]
gT.head(4)
preds = logreg.predict_proba(gT)

preds = pd.DataFrame(preds)
preds.columns = ["Death_prob", "Survival_prob"]
# Generate table of predictions vs Sex
pd.crosstab(train["Sex"], preds.ix[:, "Survival_prob"]) #predict the probability of survival according to gender<jupyter_output>C:\Users\Minerva\Anaconda2\lib\site-packages\ipykernel\__main__.py:2: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  from ipykernel import kernelapp as app<jupyter_text>Test for Classification Accuracy<jupyter_code>train=pd.read_csv("trainT.csv")
df=train[['Survived','Pclass','Sex','Age','Fare']]
#change male to 1 and female to 0
df["Sex"] = df["Sex"].apply(lambda sex:1 if sex=="male" else 0)
#handle missing values of age
df["Age"] = df["Age"].fillna(df["Age"].median())
df["Fare"] = df["Fare"].fillna(df["Fare"].median()) #more immune to outliers
df.head(7)
X= df.drop("Survived", axis=1)
Y = df["Survived"]

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = .3, random_state=25) #30% hold out for testing
logreg = LogisticRegression() #define the logistic regression model
logreg.fit(X_train, y_train)
Y_pred = logreg.predict(X_test) #predict the response variable based on predictors in the test set
from sklearn.metrics import confusion_matrix
confusion_matrix = confusion_matrix(y_test, Y_pred)
confusion_matrix
from sklearn.metrics import accuracy_score
accuracy_score(y_test, Y_pred) #compare with the actual y values, y_test (hold outs) with predicted y
from sklearn.metrics import cohen_kappa_score
cohen_kappa_score(y_test, Y_pred) #Scores above .8 are generally considered good agreement; 
from sklearn.metrics import classification_report
report = classification_report(y_test, Y_pred)
print(report)<jupyter_output>precision    recall  f1-score   support

          0       0.80      0.82      0.81       165
          1       0.71      0.68      0.69       103

avg / total       0.77      0.77      0.77       268
{'fraction_non_alphanumeric': 0.14407276995305165, 'fraction_numerical': 0.020833333333333332, 'mean_word_length': 5.6065891472868215, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '496489', 'license': 'MIT', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1212, 'n_words': 297, 'path': 'section 6_part 1/Accuracy_classification.ipynb', 'repo_name': 'sanjanapackt/PacktPublishing-Clustering-and-Classification-with-Machine-Learning-in-Python', 'size': '3408'}