<jupyter_start><jupyter_text>Importing important libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report,confusion_matrix
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import precision_score,accuracy_score,recall_score,f1_score,mean_absolute_error,mean_squared_error
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Data Reading<jupyter_code>data=pd.read_csv("indian_liver_patient.csv")
data.head()<jupyter_output><empty_output><jupyter_text>Performing Basic EDA<jupyter_code>plot=plt.figure(figsize=(10,6))
sns.heatmap(data.corr(),cmap="Greens")<jupyter_output><empty_output><jupyter_text>"Values such as "Total_Bilirubin and Direct_Bilirubin" , "Alamine_Aminotransferase and Aspartate_Aminotransferase" , " Total_Protien and Albumin and Albumin_and_Globulin_Ratio" are observed to be related to each-other"<jupyter_code>infected=data[data['Dataset']==1]
#person who are suffering some kind of liver- disease.
infected.head()
sns.set_style("darkgrid")
figure=plt.figure(figsize=(10,6))
sns.distplot(infected["Age"],bins=10,kde=False,axlabel='AGE',color="blue")<jupyter_output>C:\Users\tripathi\anaconda3\lib\site-packages\seaborn\distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)<jupyter_text>"People in the age of 30-40 years are the one who are most infected with the liver disorder"<jupyter_code>sns.countplot(x='Gender',data=infected,palette='rainbow')<jupyter_output><empty_output><jupyter_text>"Men are more infected in comparison of women"  Data PreProcessing<jupyter_code>#converting the male / female text data point to numerical for better adaptibility of the models and easy to scale the model
for i in range(583):
    if data.iloc[i,1]=='Male':
        data.iloc[i,1]=0
    else:
        data.iloc[i,1]=1
        
data['Gender'].unique()
data.head()
for i in range(583):
    if data.iloc[i,10]==2:
        data.iloc[i,10]=0
        
data['Dataset'].unique()
#converting the dataset==2 to dataset==0 for uninfected definition 
#cheaking for the null values
for i in range(11):
    print(sum(data.iloc[:,i].isnull()))    
data['Albumin_and_Globulin_Ratio'].fillna(data['Albumin_and_Globulin_Ratio'].mean(),inplace=True)
sum(data['Albumin_and_Globulin_Ratio'].isnull())
#handeled the null values succesfully!<jupyter_output><empty_output><jupyter_text>Scaling the data for training the Models<jupyter_code>scaler = StandardScaler()
scaler.fit(data.drop('Dataset',axis=1))
scaled_features = scaler.transform(data.drop('Dataset',axis=1))
data_scaled = pd.DataFrame(scaled_features,columns=data.columns[:-1])
data_scaled.head()
X=data_scaled
y=data['Dataset']<jupyter_output><empty_output><jupyter_text>Spliting of the data in X_train and X_test set<jupyter_code>X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3)
y_test.value_counts()<jupyter_output><empty_output><jupyter_text>Support Vector Machine Algo (SVM)<jupyter_code>model1=SVC()
#hyper-parameter tuning
param_grid = {'C': [0.1,1, 10,50,100,200,1000], 'gamma': [1,0.1,0.01,0.001,0.0001], 'kernel': ['rbf']} 
grid = GridSearchCV(SVC(),param_grid,refit=True,verbose=3)
grid.fit(X_train,y_train)
model1_predictions=grid.predict(X_test)
model1_acc=accuracy_score(y_test,model1_predictions)
model1_prec=precision_score(y_test,model1_predictions)
model1_rec=recall_score(y_test,model1_predictions)
model1_f1=f1_score(y_test,model1_predictions)
model1_mas=mean_absolute_error(y_test,model1_predictions)
model1_rms=np.sqrt(mean_squared_error(y_test,model1_predictions))
print(classification_report(y_test,model1_predictions))<jupyter_output>precision    recall  f1-score   support

           0       0.53      0.19      0.28        47
           1       0.76      0.94      0.84       128

    accuracy                           0.74       175
   macro avg       0.64      0.56      0.56       175
weighted avg       0.70      0.74      0.69       175<jupyter_text>Logistic Regression Algo<jupyter_code>model2=LogisticRegression()
model2.fit(X_train,y_train)
model2_predictions=model2.predict(X_test)
model2_acc=accuracy_score(y_test,model2_predictions)
model2_prec=precision_score(y_test,model2_predictions)
model2_rec=recall_score(y_test,model2_predictions)
model2_f1=f1_score(y_test,model2_predictions)
model2_mas=mean_absolute_error(y_test,model2_predictions)
model2_rms=np.sqrt(mean_squared_error(y_test,model2_predictions))
print(classification_report(y_test,model2_predictions))<jupyter_output>precision    recall  f1-score   support

           0       0.45      0.21      0.29        47
           1       0.76      0.91      0.83       128

    accuracy                           0.72       175
   macro avg       0.61      0.56      0.56       175
weighted avg       0.68      0.72      0.68       175<jupyter_text>Random Forest Classifier Algo<jupyter_code>model3= RandomForestClassifier(n_estimators=600)
model3.fit(X_train,y_train)
model3_predictions=model3.predict(X_test)
model3_acc=accuracy_score(y_test,model3_predictions)
model3_prec=precision_score(y_test,model3_predictions)
model3_rec=recall_score(y_test,model3_predictions)
model3_f1=f1_score(y_test,model3_predictions)
model3_mas=mean_absolute_error(y_test,model3_predictions)
model3_rms=np.sqrt(mean_squared_error(y_test,model3_predictions))
print(classification_report(y_test,model3_predictions))
model4=DecisionTreeClassifier()
model4.fit(X_train,y_train)
model4_predictions=model4.predict(X_test)
model4_acc=accuracy_score(y_test,model4_predictions)
model4_prec=precision_score(y_test,model4_predictions)
model4_rec=recall_score(y_test,model4_predictions)
model4_f1=f1_score(y_test,model4_predictions)
model4_mas=mean_absolute_error(y_test,model4_predictions)
model4_rms=np.sqrt(mean_squared_error(y_test,model4_predictions))
print(classification_report(y_test,model4_predictions))<jupyter_output>precision    recall  f1-score   support

           0       0.43      0.43      0.43        47
           1       0.79      0.79      0.79       128

    accuracy                           0.69       175
   macro avg       0.61      0.61      0.61       175
weighted avg       0.69      0.69      0.69       175<jupyter_text>K-Nearest Neighbour Algo (kNN)<jupyter_code>error_rate = []

for i in range(1,40):
    
    model5 = KNeighborsClassifier(n_neighbors=i)
    model5.fit(X_train,y_train)
    pred_i = model5.predict(X_test)
    error_rate.append(np.mean(pred_i != y_test))
#Hyper-parameter tunining

plt.figure(figsize=(10,6))
plt.plot(range(1,40),error_rate,color='red', linestyle='dashed', marker='o',markerfacecolor='green', markersize=10)
plt.title('Error Rate vs. K Value')
plt.xlabel('K')
plt.ylabel('Error Rate')
error_rate[33]
model5 = KNeighborsClassifier(n_neighbors=33)

model5.fit(X_train,y_train)
model5_predictions = model5.predict(X_test)

print('WITH K=36')
print('\n')
print(classification_report(y_test,model5_predictions))
model5_acc=accuracy_score(y_test,model5_predictions)
model5_prec=precision_score(y_test,model5_predictions)
model5_rec=recall_score(y_test,model5_predictions)
model5_f1=f1_score(y_test,model5_predictions)
model5_mas=mean_absolute_error(y_test,model5_predictions)
model5_rms=np.sqrt(mean_squared_error(y_test,model5_predictions))<jupyter_output><empty_output><jupyter_text>Comparison between all the models(5) trained<jupyter_code>dict1={'Machine Learning Algorithm':['Support Vector Machine','Logistic Regression','Random Forest Classifier','Decision Tree Classifier','K Nearest Neighbours']
   ,'Accuracy_Score':[model1_acc*100,model2_acc*100,model3_acc*100,model4_acc*100,model5_acc*100]
   ,'Precision_Score':[model1_prec*100,model2_prec*100,model3_prec*100,model4_prec*100,model5_prec*100]
   ,'Recall_Score':[model1_rec*100,model2_rec*100,model3_rec*100,model4_rec*100,model5_rec*100]
   ,'F1_Score':[model1_f1*100,model2_f1*100,model3_f1*100,model4_f1*100,model5_f1*100]
   ,'Mean_Absolute_Error':[model1_mas*100,model2_mas*100,model3_mas*100,model4_mas*100,model5_mas*100]
   ,'Root_Mean_Square_Error':[model1_rms*100,model2_rms*100,model3_rms*100,model4_rms*100,model5_rms*100]}
final=pd.DataFrame(dict1)
final.head()
final.plot.bar(x='Machine Learning Algorithm',figsize=(14,7))
plt.legend(loc='center left', bbox_to_anchor=(1,0.5))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10772860031453606, 'fraction_numerical': 0.055830150527971245, 'mean_word_length': 6.339653751030503, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '448314', 'license': 'MIT', 'n_tokens_mistral': 3510, 'n_tokens_neox': 3245, 'n_words': 467, 'path': 'comparison of models .ipynb', 'repo_name': 'thisisharrsh/Comparison-of-5-MLmodels', 'size': '8902'}