<jupyter_start><jupyter_text>- <NAME> - 12/15/2021 - ML - EX02 - Q1<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import Normalizer, StandardScaler
from sklearn.metrics import accuracy_score
import seaborn as sns
from scipy.stats import multivariate_normal
dataset = pd.read_csv('./HeartDisease/heart.csv')
dataset.head()
# Independent features: 
indeps = ['sex', 'cp', 'fbs', 'restecg', 'exang', 'slope', 'ca', 'thal']
dataset.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 1025 entries, 0 to 1024
Data columns (total 14 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   age       1025 non-null   int64  
 1   sex       1025 non-null   int64  
 2   cp        1025 non-null   int64  
 3   trestbps  1025 non-null   int64  
 4   chol      1025 non-null   int64  
 5   fbs       1025 non-null   int64  
 6   restecg   1025 non-null   int64  
 7   thalach   1025 non-null   int64  
 8   exang     1025 non-null   int64  
 9   oldpeak   1025 non-null   float64
 10  slope     1025 non-null   int64  
 11  ca        1025 non-null   int64  
 12  thal      1025 non-null   int64  
 13  target    1025 non-null   int64  
dtypes: float64(1), int64(13)
memory usage: 112.2 KB<jupyter_text>Splite the dataset into 80% of train and 20% of test.<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(dataset.iloc[:, :-1], dataset.iloc[:, -1], test_size=0.2)
train = pd.DataFrame(x_train)
train['target'] = y_train
test = pd.DataFrame(x_test)
test['target'] = y_test<jupyter_output><empty_output><jupyter_text>Bayesian Classifier<jupyter_code>def cal_prior_probabilities(dataset):
    return np.log(dataset.groupby(by = 'target').apply(lambda x: np.divide(len(x), dataset.shape[0]))).values
# Continues with Noraml Distribution
def cal_probability_density(mean, cov, x):    
    var = multivariate_normal(mean=mean, cov=cov)
    prob = var.pdf(x)
    return prob
# Discrete
def cal_probability(dataset, col, x, class_i):
    frequent = dataset.groupby(by='target').apply(lambda a: a.iloc[:, col].value_counts()[x])[class_i]
    return frequent / dataset.groupby(by='target').size()[class_i]
def cal_likelihood_probabilities(df_row, n_unique_labels, dataset, cov, mean_continues):
    
    df_discrete = df_row[indeps]
    df_continues = df_row.drop(indeps)
    
    likelihood_probabilities = []
    for i in range(n_unique_labels):
        likelihood = 0
    
        # For Discretes
        for j in range(dataset.shape[1]):
            if dataset.columns[j] in indeps:
                likelihood += np.log(cal_probability(dataset, j, df_discrete[dataset.columns[j]], i))
        
        # For Continueses
        likelihood += np.log(cal_probability_density(mean_continues[i], cov[i], df_continues))
        
        likelihood_probabilities.append(likelihood)

    return likelihood_probabilities
def NBC_fit(dataset):
    
    unique_labels = dataset['target'].unique()
    n_unique_labels = len(unique_labels)
    
    dataset_continues = dataset.drop(indeps, axis=1)
    dataset_continues['target'] = dataset['target']
    dataset_continues0 = dataset_continues[dataset_continues['target'] == 0]
    dataset_continues1 = dataset_continues[dataset_continues['target'] == 1]
    dataset_continues0 = dataset_continues0.drop('target', axis=1)
    dataset_continues1 = dataset_continues1.drop('target', axis=1)
        
    cov0, mean0 = dataset_continues0.cov(), dataset_continues0.mean()
    cov1, mean1 = dataset_continues1.cov(), dataset_continues1.mean()
    cov, mean_continues = [cov0, cov1], [mean0, mean1]
    
    prior_probabilities = cal_prior_probabilities(dataset)

    return {
      'unique_labels': unique_labels,
      'n_unique_labels': n_unique_labels,
      'prior_probabilities': prior_probabilities,
      'dataset': dataset,
      'cov': cov,
      'mean_continues': mean_continues
    }
def predict(test_dataset, nbc):
    predictions = []
    for i in range(test_dataset.shape[0]):
        prior = nbc['prior_probabilities']
        likelihood = cal_likelihood_probabilities(test_dataset.iloc[i, :-1], nbc['n_unique_labels'], 
                                                  nbc['dataset'], nbc['cov'], nbc['mean_continues'])
        # log(a*b) = loga + logb
        probabilities = prior + likelihood
        mx_idx = np.argmax(probabilities)
        predictions.append(nbc['unique_labels'][mx_idx])

    return predictions
nbc = NBC_fit(train)
predictions = predict(test, nbc)
accuracy = accuracy_score(test.iloc[:, -1], predictions)
print('accuracy: {:.3f}'.format(accuracy))<jupyter_output>accuracy: 0.868
{'fraction_non_alphanumeric': 0.11727349703640982, 'fraction_numerical': 0.03746824724809483, 'mean_word_length': 3.4035414725069897, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '472581', 'license': 'MIT', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1579, 'n_words': 342, 'path': 'ML-HW02/Ex02_Q1_partA.ipynb', 'repo_name': 'nahbos/AUT-Machine-Learning', 'size': '4741'}