<jupyter_start><jupyter_text><jupyter_code>#import needed libraries
import numpy as np #to handle arrays
import pandas as pd #to get data
import tensorflow as tf #for ML algorithms
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler #for normalizing<jupyter_output><empty_output><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/drive')<jupyter_output>Mounted at /content/drive<jupyter_text>New Section<jupyter_code>#read data from file 'heart.data' which has to be in our (google collab) directory
dataset = pd.read_csv('heart.data')
X = dataset.iloc[:, :-1].values #attributs are everything except the last column which is the result
Y = dataset.iloc[:, -1].values #result is only last column<jupyter_output><empty_output><jupyter_text>New Section<jupyter_code>#make output to binary (from 1 and 2 to 0 and 1) 
#--> necessary for the cost function we want to use which is good for binary output
Y = Y - 1

#get our train- and test-data
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.20, random_state = 0)

#here's no need to encode the data, but we have to normalize the attributes
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)

ann = tf.keras.models.Sequential([

tf.keras.layers.Dense(units = 10, activation = 'relu'), #1st hidden layer

tf.keras.layers.Dense(units = 10, activation = 'relu'), #2nd hidden layer

#result is 1 --> absence, 2 --> presence of heart disease

tf.keras.layers.Dense(units = 1, activation = 'sigmoid') #output layer

])


The heart dataset is explained here:
#the set loss-function/cost-function is good to evaluate binary outputs (1 or more neuron)
ann.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
ann.fit(X_train, Y_train, batch_size = 1, epochs = 3)
test_loss, test_acc = ann.evaluate(X_test, Y_test, verbose = 1)
print('Test accuracy:', test_acc)

#here you can enter the attributes of the patient to see if he has a heart disease :)
result = ann.predict(sc.transform([[70,1,4,130,322,0,2,109,0,2.4,2,3,3]]))

if result < 0.5:
  answer = "no heart disease"
else:
  answer = "heart disease"

#weird indicies on result because result is a rank-2-tensor :)
#round function takes 2 parameters, the value and the decimals you want to round up to
print("The patient has",answer, "with a probability of", round(result[0][0] * 100, 2),"%")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11267605633802817, 'fraction_numerical': 0.022132796780684104, 'mean_word_length': 5.4404145077720205, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '164030', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 858, 'n_tokens_neox': 818, 'n_words': 279, 'path': 'Copy_of_Copy_of_Untitled0.ipynb', 'repo_name': 'ns11345/docs', 'size': '2485'}