<jupyter_start><jupyter_text>`This notebook is an Illustration to create a Simple Stochastic Gradient Descent Linear Model Classifier with Sklearn using Python 🐍`For more information, check out the [SGDClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html).   **Setup**Let's setup the imports:<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
from pylab import rcParams
import matplotlib.pyplot as plt
from matplotlib import rc
from google.colab import drive
from sklearn.model_selection import train_test_split
from sklearn.linear_model import SGDClassifier
from sklearn import linear_model, metrics, model_selection
from sklearn.metrics import confusion_matrix, classification_report 


%matplotlib inline

sns.set(style='whitegrid', palette='muted', font_scale=1.5)

plt.rcParams['figure.figsize'] = (13, 9)

plt.style.use('fivethirtyeight')

RANDOM_SEED = 42

np.random.seed(RANDOM_SEED)<jupyter_output><empty_output><jupyter_text>Exploratory Data AnalysisLet's load the Heart Disease UCI dataset which is uploaded on my Google Drive.[The Dataset](https://archive.ics.uci.edu/ml/datasets/heart+Disease)<jupyter_code>!gdown --id 1Hfgu_Tui5Tw2lWfJBOIV7TfVlI6uwiE0<jupyter_output>Downloading...
From: https://drive.google.com/uc?id=1Hfgu_Tui5Tw2lWfJBOIV7TfVlI6uwiE0
To: /content/heart_disease.csv

  0% 0.00/11.3k [00:00<?, ?B/s]
100% 11.3k/11.3k [00:00<00:00, 27.5MB/s]<jupyter_text>Let's read the CVS file:<jupyter_code>df = pd.read_csv('/content/heart_disease.csv')<jupyter_output><empty_output><jupyter_text>First few rows in the DataFrame:<jupyter_code>df.head()<jupyter_output><empty_output><jupyter_text>Shape of the DataFrame:<jupyter_code>df.shape
df.columns<jupyter_output><empty_output><jupyter_text>`Let's now do some Data Visualization Using Seaborn and Matplotlib.`Let's take a look at a distplot of the Age column:<jupyter_code>sns.distplot(df['age'], color = 'black')
plt.title('Distribution of Age', fontsize = 20, fontweight = 'bold')
plt.xlabel('Age', fontsize = 20, fontweight = 'bold')
plt.show()<jupyter_output><empty_output><jupyter_text>Let's take a look at a countplot of the Target column:<jupyter_code>sns.countplot(df['target'], palette = 'muted')
plt.title('Distribution of Target', fontsize = 20, fontweight = 'bold')
plt.xlabel('Target', fontweight='bold')
plt.ylabel('Count', fontweight='bold')
plt.show()<jupyter_output><empty_output><jupyter_text>Let's take a look at a Pieplot of the Sex column:<jupyter_code>size = df['sex'].value_counts()
colors = ['lightblue', 'lightgreen']
labels = "Male", "Female"
explode = [0.01, 0]

my_circle = plt.Circle((0, 0), 0.7, color = 'white')
#displays in percentage with exploding slice

plt.pie(size, colors = colors, labels = labels, shadow = True, explode = explode, autopct = '%.2f%%')
plt.title('Distribution of Gender', fontsize = 20, fontweight='bold')
p = plt.gcf()
p.gca().add_artist(my_circle)
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Let's take a look at a Violinplot of the cholesterol column:<jupyter_code>sns.violinplot(df['target'], df['chol'], palette = 'colorblind')
plt.title('Relation of Cholestrol with Target', fontsize = 20, fontweight = 'bold')
plt.xlabel('Target', fontweight='bold')
plt.ylabel('cholesterol', fontweight='bold')
plt.show();<jupyter_output><empty_output><jupyter_text>Let's take a look at a Crosstab plot of the Age column with respect to Target column:<jupyter_code>pd.crosstab(df['age'], df['target']).plot(kind="bar")
plt.title('Heart Disease Frequency for Ages', fontweight = 'bold')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.show();<jupyter_output><empty_output><jupyter_text>Let's take a look at a Crosstab plot of the Chest Pain column with respect to Target column:<jupyter_code>pd.crosstab(df.cp, df.target).plot(kind="bar",color=['#11A5AA','#AA1190'])
plt.title('Heart Disease Frequency According To Chest Pain Type', fontweight = 'bold')
plt.xlabel('Chest Pain Type', fontweight = 'bold')
plt.xticks(rotation = 0)
plt.ylabel('Frequency of Disease or Not', fontweight = 'bold')
plt.show();<jupyter_output><empty_output><jupyter_text>Let's take a look at a Crosstab plot of the Sex column with respect to Target column:<jupyter_code>pd.crosstab(df.sex, df.target).plot(kind="bar",color=['#1CA53B','#AA1111'])
plt.title('Heart Disease Frequency for Sex', fontweight='bold')
plt.xlabel('Sex (0 = Female, 1 = Male)', fontweight='bold')
plt.xticks(rotation=0)
plt.legend(["Haven't Disease", "Have Disease"])
plt.ylabel('Frequency', fontweight='bold')
plt.show();<jupyter_output><empty_output><jupyter_text>Let's take a look at a Crosstab plot of the Slope column with respect to Target column:<jupyter_code>pd.crosstab(df.slope, df.target).plot(kind="bar",color=['#DAF7A6','#FF5733'])
plt.title('Heart Disease Frequency for Slope', fontweight='bold')
plt.xlabel('The Slope of The Peak Exercise ST Segment',fontweight='bold')
plt.xticks(rotation = 0)
plt.ylabel('Frequency', fontweight='bold')
plt.show();<jupyter_output><empty_output><jupyter_text>Let's take a look at a Crosstab plot of the Sex column with respect to Exercise Induced Angina column:<jupyter_code>pd.crosstab(df.sex, df.exang).plot(kind="bar",color=['#FFD700','#FF69B4'])
plt.title('Heart Disease Frequency for Exercise Induced Angina', fontweight='bold')
plt.xlabel('Sex (0 = Female, 1 = Male)', fontweight='bold')
plt.xticks(rotation = 0)
plt.ylabel('Frequency', fontweight='bold')
plt.show();<jupyter_output><empty_output><jupyter_text>Let's take a look at a Barplot of the Chest Pain column with respect to Target column:<jupyter_code>sns.barplot(df["cp"],df['target'], alpha=1)
plt.title("Chest Pain Type", fontweight = 'bold')
plt.xlabel('Chest Type', fontweight='bold')
plt.ylabel('Measure of Target', fontweight='bold');<jupyter_output><empty_output><jupyter_text>Data Preprocessing Here are the requirements:* Check if the DataFrame has nullValues.* Check Correlation Coefficients.<jupyter_code>df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 303 entries, 0 to 302
Data columns (total 14 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   age       303 non-null    int64  
 1   sex       303 non-null    int64  
 2   cp        303 non-null    int64  
 3   trestbps  303 non-null    int64  
 4   chol      303 non-null    int64  
 5   fbs       303 non-null    int64  
 6   restecg   303 non-null    int64  
 7   thalach   303 non-null    int64  
 8   exang     303 non-null    int64  
 9   oldpeak   303 non-null    float64
 10  slope     303 non-null    int64  
 11  ca        303 non-null    int64  
 12  thal      303 non-null    int64  
 13  target    303 non-null    int64  
dtypes: float64(1), int64(13)
memory usage: 33.3 KB<jupyter_text>WooHOO! The dataset has no null values. Let's write a helper function to elaborate the columns in our DataFrame:<jupyter_code>info = ["age","1: male, 0: female","chest pain type, 1: typical angina,\
 2: atypical angina, 3: non-anginal pain, 4: asymptomatic","resting blood pressure"," \
 serum cholestoral in mg/dl","fasting blood sugar > 120 mg/dl","\
 resting electrocardiographic results (values 0,1,2)"," maximum heart rate achieved","\
 exercise induced angina","oldpeak = ST depression induced by exercise relative to rest","\
 the slope of the peak exercise ST segment","number of major vessels (0-3) colored by flourosopy","\
 thal: 3 = normal; 6 = fixed defect; 7 = reversable defect"]



for i in range(len(info)):
    print(df.columns[i]+":\t\t\t"+info[i]) 
df["target"].unique()<jupyter_output><empty_output><jupyter_text>Let's see the correlations.Corr() is used to find the pairwise correlation of all columns in the dataframe:<jupyter_code>print(df.corr(method='kendall')["target"].abs().sort_values(ascending=False))
x_data = df.drop(["target"], axis=1) 
y = df["target"].values<jupyter_output><empty_output><jupyter_text>Our DataFrame now contains all the columns except the target column:<jupyter_code>x_data.head() 
x_data.shape
y.shape<jupyter_output><empty_output><jupyter_text>Let's normalize the inputs and scale them down to 0 and 1:<jupyter_code>x_data = (x_data - np.min(x_data)) / (np.max(x_data) - np.min(x_data)).values
x_data.head()<jupyter_output><empty_output><jupyter_text>Let's split our DataSet into Training and Testing sets<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(x_data, y, test_size = 0.2,random_state=0) 
x_train.shape
x_test.shape
y_train.shape
y_test.shape<jupyter_output><empty_output><jupyter_text>Now, Let's build our SGDClassifier for Heart Disease Prediction:<jupyter_code>sgd = SGDClassifier(loss = 'squared_loss',penalty=None, learning_rate='optimal', max_iter=3000, shuffle=False, verbose=0)
sgd.fit(x_train, y_train)<jupyter_output><empty_output><jupyter_text>Running the Predict funtion:<jupyter_code>y_pred = sgd.predict(x_test) 
y_pred
print("Training Accuracy :", sgd.score(x_train, y_train))
print("Testing Accuracy :", sgd.score(x_test, y_test))<jupyter_output>Training Accuracy : 0.7148760330578512
Testing Accuracy : 0.639344262295082<jupyter_text>WooOops! The model is quite bad. But the main intuition of this notebook is to provide a glimpse on SGDClassifier.This notebook is mainly focused on EDA.<jupyter_code>pred_table = pd.DataFrame({'Actual':  y_test, 'predicted': y_pred})   
pred_table.head() 
cm = confusion_matrix(y_test, y_pred) 
sns.heatmap(cm, annot = True, cmap = 'YlGn'); 
cr = classification_report(y_test, y_pred) 
print(cr)<jupyter_output>precision    recall  f1-score   support

           0       0.55      0.96      0.70        27
           1       0.93      0.38      0.54        34

    accuracy                           0.64        61
   macro avg       0.74      0.67      0.62        61
weighted avg       0.76      0.64      0.61        61
{'fraction_non_alphanumeric': 0.12985301571211352, 'fraction_numerical': 0.032843385707045106, 'mean_word_length': 5.316261203585147, 'pattern_counts': {'":': 1, '<': 96, '<?xml version=': 0, '>': 95, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '24', 'dir': 'jupyter-structured-clean-dedup', 'id': '106796', 'license': 'Apache-2.0', 'n_tokens_mistral': 3788, 'n_tokens_neox': 3411, 'n_words': 855, 'path': 'EDA_Linear_Regression.ipynb', 'repo_name': 'nakshatrasinghh/Machine-Learning', 'size': '9865'}