<jupyter_start><jupyter_text>---    - This code mainly aiming to understand preprocessing     - Discovering the data and handling with missing data    - Understanding of data types and type conversions    - Making an easy and understandable machine learning model    - Seeing how decison tree works---<jupyter_code># common libnraries import
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# ml preprocessing and model building modules
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.metrics import confusion_matrix, plot_confusion_matrix, accuracy_score, classification_report
# loading data - readable and easy to manipulate
# renaming - preprocessing
df = pd.read_csv("datasets/processed.switzerland.data",
                 sep=',',
                 header=None,
                 names = ['age','sex','chestpain','bps','cholestrol','fbs','restecg','thalach','exang','oldpeak','slope','ca','thal','prediction']
                ) # header yok, sutun ismini ben verecegim
# detail infor about the dataset
# https://archive.ics.uci.edu/ml/datasets/heart+disease
df.head()
cols = df.columns
for col in cols:
    print(df[col].unique())
    # no categorical value<jupyter_output>[32 34 35 36 38 40 41 42 43 45 46 47 48 50 51 52 53 54 55 56 57 58 59 60
 61 62 63 64 65 66 67 68 69 70 72 73 74]
[1 0]
[1 4 3 2]
['95' '115' '?' '110' '105' '100' '135' '150' '125' '145' '140' '155'
 '160' '120' '130' '165' '80' '180' '170' '200' '185']
[0]
['?' '0' '1']
['0' '?' '1' '2']
['127' '154' '130' '125' '166' '156' '179' '128' '150' '120' '144' '176'
 '99' '122' '145' '140' '138' '133' '113' '118' '149' '124' '110' '139'
 '92' '104' '170' '163' '60' '126' '82' '95' '115' '135' '141' '155' '83'
 '97' '98' '100' '148' '103' '121' '131' '182' '105' '175' '94' '119'
 '143' '63' '70' '77' '117' '123' '134' '72' '78' '109' '86' '114' '93'
 '67' '90' '108' '136' '?' '157']
['0' '1' '?']
['.7' '.2' '?' '1' '2.8' '0' '-1.1' '1.6' '-1.5' '1.5' '2' '.5' '-.1'
 '-2.6' '2.1' '-.7' '2.2' '3' '.1' '.3' '-2' '-1' '1.8' '1.4' '2.6' '.9'
 '2.4' '1.1' '.4' '2.5' '1.7' '-.8' '-.5' '-.9' '3.7' '1.3']
['1' '?' '2' '3']
['?' '1' '2']
['?' '7' '6' '3']
[1 3 2 0 4]<jupyter_text>Missing data operations<jupyter_code># bazi veriler numeric olmasına ragmen object type olarak gozukuyor, bunun bir sebebi olabilir, örneğin 
# içerisinde 'boş' bırakılan veriler categorical bir değer ile doldurulmuştur 
# ya da gelişigüzel olarak belirtilmiş olabilir
# detaylı incelenmeli

# eger numeric olup object olan veriler varsa bunları numerice cevirince string değerler NaN olacaktır
# UCI sitesindeki veriseti bilgilerine dayanarak kategorik olan sutunları float'a cevirebilirz.
df.select_dtypes(include=['object',"category"]).columns
for column in df.select_dtypes(include=['object',"category"]).columns:
    df[column] = pd.to_numeric(df[column],errors="coerce")
df.info()
print(df.isnull().sum()) 
df.info() # gorunuste missing data yok fakat tabloda gorunene gore bazi verilerde ? işaretleri var
#df.isin(['?']).sum() # veri setinde bunlardan biri var mı? ['?', None, '-']
# birçok veride soru işareti var yani kayıp veri
#df.isin(['?']).sum()
df.head()
# bazi sutunlarda cok fazla bos veri var bunlari tek tek incelemeli
print("fbs  : ",df["fbs"].unique())
print("ca   : ",df["ca"].unique())
print("thal : ",df["thal"].unique())
print("slope: ",df["slope"].unique())
percent_missing = df.isnull().sum() * 100 / len(df)
missing_value_df = pd.DataFrame({'percent_missing': percent_missing})
missing_value_df.sort_values('percent_missing', ascending=False)
# drop - ca, fbs, thal
df.drop(columns=["ca","fbs","thal"], inplace=True)
# slope incelemesi
#slope: the slope of the peak exercise ST segment
#-- Value 1: upsloping
#-- Value 2: flat
#-- Value 3: downsloping
df["slope"].value_counts().plot.bar()
# checking correlations
corr = df.corr()
print(corr["slope"].sort_values())
print()
df["oldpeak"].value_counts().plot.bar()
# categorical kayıp veriler en yaygın olanlar ile degistirildi.
def fillna(col):
    col.fillna(col.value_counts().index[0], inplace=True)
    return col
df_no_missing = df.copy()
df_no_missing.apply(lambda col:fillna(col))
df_no_missing.isna().sum().sum()<jupyter_output><empty_output><jupyter_text>Machine Learning Operations<jupyter_code>X = df_no_missing.drop(columns="prediction",axis=1).copy()
y = df["prediction"].copy()
y.unique() # need to convert 0 - 1 pairs  to make this process easier
# if someone is greter then 0, they willl. pretend like they have heart disease
y_zero_one = y > 0
y[y_zero_one] = 1
y.unique()
# data scaling and encoding - getting ready to train
from sklearn.preprocessing import StandardScaler, LabelEncoder
scaler = StandardScaler()
LEncoder = LabelEncoder()

X_scaled = scaler.fit_transform(X)
y = LEncoder.fit_transform(y)
X_train, X_test, y_train, y_test = train_test_split(X_scaled,y, test_size=0.2, random_state=42)

# model training adn predictions - checking the result out
clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train,y_train)

y_pred = clf.predict(X_test)
print("Accuracy : " , accuracy_score(y_test, y_pred))
# print prediction results
predictions = clf.predict(X_test)
print(classification_report(y_test, predictions))
# to make this output clear and to understand decision tree's mechanism behind
plt.figure(figsize=(20,15))
plot_tree(clf,filled=True,
         rounded=True,
         class_names=["Yes", "No"],
         feature_names=X.columns
         )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1664868752247393, 'fraction_numerical': 0.0749730312837109, 'mean_word_length': 4.782744282744282, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '568861', 'license': 'MIT', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2101, 'n_words': 582, 'path': 'DecisionTrees/DecisionTreeEndToEnd.ipynb', 'repo_name': 'enesoriginal/Data-Science-Projects', 'size': '5562'}