<jupyter_start><jupyter_text>Importing the essential Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score
from sklearn.metrics import log_loss
import mlxtend.evaluate as mlxe
import seaborn as sns
import cma
from sklearn.model_selection import GridSearchCV
%matplotlib inline<jupyter_output><empty_output><jupyter_text>--> Loading The Dataset<jupyter_code>df = pd.read_csv("heart.csv")
df.head(10)<jupyter_output><empty_output><jupyter_text>Finding The counts of Target Classification Values<jupyter_code>df.target.value_counts()<jupyter_output><empty_output><jupyter_text>1. Displaying Count Plot for Target Variable<jupyter_code>sns.countplot(x="target", data=df, palette="CMRmap")
plt.show()<jupyter_output><empty_output><jupyter_text>2. Displaying count plot of sex variable<jupyter_code>sns.countplot(x="sex",data=df,palette='Greens')<jupyter_output><empty_output><jupyter_text>3. Percentage of patients having or not having heart disease<jupyter_code>countNoDisease = len(df[df.target == 0])
countHaveDisease = len(df[df.target == 1])
print("Percentage of Patients who have a Heart Disease: {:.2f}%".format((countNoDisease / (len(df.target))*100)))
print("Percentage of Patients who do not have Heart Disease: {:.2f}%".format((countHaveDisease / (len(df.target))*100)))<jupyter_output>Percentage of Patients who have a Heart Disease: 45.54%
Percentage of Patients who do not have Heart Disease: 54.46%<jupyter_text>4.Percentage of Male and Female Patients<jupyter_code>countFemale = len(df[df.sex == 0])
countMale = len(df[df.sex == 1])
print("Percentage of Female Patients: {:.2f}%".format((countFemale / (len(df.sex))*100)))
print("Percentage of Male Patients: {:.2f}%".format((countMale / (len(df.sex))*100)))<jupyter_output>Percentage of Female Patients: 31.68%
Percentage of Male Patients: 68.32%<jupyter_text>5. Frequency chart for the variable age<jupyter_code>pd.crosstab(df.age,df.target).plot(kind="bar",figsize=(20,6))
plt.title('Heart Disease Frequency for Ages')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.show()<jupyter_output><empty_output><jupyter_text>6. Creating Model for Logistic Regression<jupyter_code>y = df.target.values
x_data = df.drop(['target'], axis = 1)
x_data<jupyter_output><empty_output><jupyter_text>Normalizing the values<jupyter_code>temp1 = len(x_data.isnull())
temp2 = x_data.shape[0]
print(temp1)
print(temp2)

if(temp1==temp2):
  print("No null values present")
x_norm = (x_data - np.min(x_data)) / (np.max(x_data) - np.min(x_data)).values
x_norm
x_norm[:1]
x_train, x_test, y_train, y_test = train_test_split(x_norm,y,test_size = 0.2,random_state=74)
lr = LogisticRegression()
lr.fit(x_train,y_train)
acc = lr.score(x_test,y_test)*100

print("Test Accuracy {:.2f}%".format(acc))
y_pred = lr.predict(x_test)
print("Precision:",precision_score(y_test, y_pred))
print(y_test.flatten())<jupyter_output>[1 1 1 0 0 0 1 0 1 1 1 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1 1 1 1 0 1 0 1 0 0 1 0
 1 1 0 0 1 0 0 0 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 1]<jupyter_text>7. Finding Loss of Initial model<jupyter_code>initial_loss = log_loss( y_test.flatten(),y_pred ,normalize=True, sample_weight=None, labels=None)
print("Initial loss: ",initial_loss)<jupyter_output>Initial loss:  10.191940489753849<jupyter_text>Checking which solver is best for performing to reduce the loss value of logistic regression<jupyter_code>solver_list = ['liblinear', 'newton-cg', 'lbfgs', 'sag', 'saga']
params = dict(solver=solver_list)
clf = GridSearchCV(lr, params, cv=5)
clf.fit(x_train, y_train)
scores = clf.cv_results_['mean_test_score']

for score, solver in zip(scores, solver_list):
    print(f"  {solver} {score:.3f}" )<jupyter_output>liblinear 0.852
  newton-cg 0.852
  lbfgs 0.852
  sag 0.852
  saga 0.852<jupyter_text>8. Applying Normalization on numerical values and applying alternate solvers to reduce the loss value of the initial model<jupyter_code>df['age']=(df['age']-df['age'].min())/(df['age'].max()-df['age'].min())
df['trestbps']=(df['trestbps']-df['trestbps'].min())/(df['trestbps'].max()-df['trestbps'].min())
df['chol']=(df['chol']-df['chol'].min())/(df['chol'].max()-df['chol'].min())
df['thalach']=(df['thalach']-df['thalach'].min())/(df['thalach'].max()-df['thalach'].min())
df['oldpeak']=(df['oldpeak']-df['oldpeak'].min())/(df['oldpeak'].max()-df['oldpeak'].min())<jupyter_output><empty_output><jupyter_text>Finding IQR(Inter Quartile Range)<jupyter_code>Q1 = df.quantile(0.25)
Q3 = df.quantile(0.75)
IQR = Q3 - Q1
print(IQR)<jupyter_output>age         0.281250
sex         1.000000
cp          2.000000
trestbps    0.188679
chol        0.144977
fbs         0.000000
restecg     1.000000
thalach     0.248092
exang       1.000000
oldpeak     0.258065
slope       1.000000
ca          1.000000
thal        1.000000
target      1.000000
dtype: float64<jupyter_text>Removing outliers from the data<jupyter_code>df_out = df[~((df < (Q1 - 1.5 * IQR)) |(df > (Q3 + 1.5 * IQR))).any(axis=1)]
print(df_out)
y1 = df_out.target.values
x1_data = df_out.drop(['target'], axis = 1)
x1_norm = (x_data - np.min(x_data)) / (np.max(x_data) - np.min(x_data)).values
x1_norm
y<jupyter_output><empty_output><jupyter_text>Checking for outliers<jupyter_code>plt.figure(figsize=(20,7))
plt.style.use('ggplot')
sns.boxplot(df['age'],df['chol'])
plt.show()
plt.figure(figsize=(20,7))
plt.style.use('ggplot')
sns.boxplot(df_out['age'],df_out['chol'])
plt.show()
x1=df_out[['age', 'sex', 'cp', 'trestbps', 'chol', 'fbs', 'restecg', 'thalach',
       'exang', 'oldpeak', 'slope', 'ca', 'thal']].values
y1=df_out[['target']].values
x1_train,x1_test,y1_train,y1_test=train_test_split(x1,y1,test_size=0.2,random_state=34)
logreg = LogisticRegression(solver='liblinear')
#solver='liblinear'
logreg.fit(x1_train,y1_train)
score = logreg.score(x1_test,y1_test)*100
print("Score after applying Liblinear Solver",score)


y1_pred = logreg.predict(x1_test)
#y1_train = y1_train.flatten()
loss = log_loss( y1_test.flatten(),y1_pred)
print("Precision:",precision_score(y_test, y_pred))
#loss = Loss(y1_pred,y1_test.flatten())
print("Loss after applying Liblinear solver: ",loss)<jupyter_output>Precision: 0.8157894736842105
Loss after applying Liblinear solver:  4.5050925557461845
{'fraction_non_alphanumeric': 0.1563573883161512, 'fraction_numerical': 0.05545142143080287, 'mean_word_length': 6.613555291319857, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '196744', 'license': 'Apache-2.0', 'n_tokens_mistral': 2782, 'n_tokens_neox': 2443, 'n_words': 495, 'path': 'Heart_Disease_SVM_Solvers.ipynb', 'repo_name': 'sudar-coder321/Heart_Disease_Logistic-Regression_with_different_solvers', 'size': '6402'}