# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve
from sklearn.linear_model import LogisticRegression as LR
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn import metrics


DF = pd.read_csv("downloads/dataset_mushrooms.csv")
pd.set_option('display.max_columns', None)
DF[:5]

print(DF.columns)
print(len(DF.columns))

Sub = pd.read_csv("downloads/submission_mushrooms.csv")
Sub[:5]

Y = np.array([1 if x=="p" else 0 for x in DF["class"]])
print(Y)
np.unique(Y)

X = DF.drop(["class"], axis = 1)
X.shape

Merged = X.append(Sub)
Merged.shape

Merged_dropped = Merged.drop(columns=["veil-type"])
Merged_dropped.shape

Merged_dropped["stalk-root"] = Merged_dropped["stalk-root"].str.replace("?","b")
print(Merged_dropped.shape)
print(np.unique(Merged_dropped["stalk-root"]))

Merged_dummies = pd.get_dummies(Merged_dropped)
print(Merged_dummies.shape)
print(np.unique(Merged_dummies))

Sub_processed = Merged_dummies[7921:]
print("Length =",len(Sub_processed), "shape =",Sub_processed.shape)
X_processed = Merged_dummies[0:7921]
print("Length =",len(X_processed), "shape =",Sub_processed.shape)

# +
model=LR()
acc_1=[]
auc_1= []

X2 = np.array(X_processed)
xtrain,xtest,ytrain,ytest=train_test_split(X2,Y)
model.fit(xtrain,ytrain)
yp=model.predict(xtest)
probabilities=model.predict_proba(xtest) [:,1]
FPR,TPR,Threshold = roc_curve(ytest,probabilities)

plot1= plt.plot(FPR,TPR)

acc_1.append(accuracy_score(ytest,yp))
auc_1.append(metrics.auc(FPR,TPR))


print(auc_1)
print(acc_1)

plt.annotate("confusion_matrix\n" + str(confusion_matrix(Y,yp2)), xy = (.6,.3), 
                xytext =(.6, .3),)  



plt.gcf().set_size_inches(5,5)
plt.plot([0,1],[0,1],c = "k", ls=":")
plt.xlabel("False Positive Ratio",fontsize=16)
plt.ylabel("True Positive Ratio",fontsize=16)
plt.title("Poisonous Shroom Indicator",fontsize=16)
# -

len(Y)

len(yp)

yp2=model.predict(X2)
con = confusion_matrix(Y,yp2)
con

# +
P = np.array(Sub_processed)

yp5=model.predict(P)


h = pd.DataFrame(yp5)
h2 = h.rename({0:'Y_predicted'},axis=1)
h2
# -

h2.to_csv("Group4_Mushrooms_Log-Reg_Y-predicted", index=True)

# +
e_total = []
p_total = []

for value in yp5:
    if value == 1:
        p_total.append("1")
    else:
        e_total.append("0")
print("There are " + str(len(p_total)) + " poisonous mushrooms.")
print("There are " + str(len(e_total)) + " edible mushrooms.")

# -



StarcoderdataJupyterScriptsDedupFiltered