<jupyter_start><jupyter_text>Importing the Dependencies<jupyter_code>import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score<jupyter_output><empty_output><jupyter_text>Data Collection and Processing<jupyter_code># loading the csv data to a Pandas DataFrame
heart_data = pd.read_csv('heart.csv')
# print first 5 rows of the dataset
heart_data.head()
# print last 5 rows of the dataset
heart_data.tail()
# number of rows and columns in the dataset
heart_data.shape
# getting some info about the data
heart_data.info()
# checking for missing values
heart_data.isnull().sum()
# statistical measures about the data
heart_data.describe()
# checking the distribution of Target Variable
heart_data['target'].value_counts()<jupyter_output><empty_output><jupyter_text>1 --> Defective Heart0 --> Healthy Heart Splitting the Features and Target<jupyter_code>X = heart_data.drop(columns='target', axis=1)
Y = heart_data['target']
print(X)
print(Y)<jupyter_output>0      1
1      1
2      1
3      1
4      1
      ..
298    0
299    0
300    0
301    0
302    0
Name: target, Length: 303, dtype: int64<jupyter_text>Splitting the Data into Training data & Test Data<jupyter_code>X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, stratify=Y, random_state=2)
#X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=2)
print(X.shape, X_train.shape, X_test.shape)<jupyter_output>(303, 13) (242, 13) (61, 13)<jupyter_text>Model Training Logistic Regression<jupyter_code>model = LogisticRegression()
# training the LogisticRegression model with Training data
model.fit(X_train, Y_train)<jupyter_output>c:\users\111ar\appdata\local\programs\python\python37\lib\site-packages\sklearn\linear_model\_logistic.py:765: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
  extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)<jupyter_text>Model Evaluation Accuracy Score<jupyter_code># accuracy on training data
X_train_prediction = model.predict(X_train)
training_data_accuracy = accuracy_score(X_train_prediction, Y_train)
print('Accuracy on Training data : ', training_data_accuracy)
# accuracy on test data
X_test_prediction = model.predict(X_test)
test_data_accuracy = accuracy_score(X_test_prediction, Y_test)
print('Accuracy on Test data : ', test_data_accuracy)<jupyter_output>Accuracy on Test data :  0.819672131147541<jupyter_text>Building a Predictive System<jupyter_code>input_data = (62,0,0,140,268,0,0,160,0,3.6,0,2,2)

# change the input data to a numpy array
input_data_as_numpy_array= np.asarray(input_data)

# reshape the numpy array as we are predicting for only on instance
input_data_reshaped = input_data_as_numpy_array.reshape(1,-1)

prediction = model.predict(input_data_reshaped)
print(prediction)

if (prediction[0]== 0):
  print('The Person does not have a Heart Disease')
else:
  print('The Person has Heart Disease')<jupyter_output>[0]
The Person does not have a Heart Disease<jupyter_text>saving the model<jupyter_code>import pickle
with open('heart_disease_model','wb') as file:
    pickle.dump(model,file)
#reading the model
with open('heart_disease_model','rb') as file:
    model = pickle.load(file)
    
model.predict([[1,2,3,4,5,6,7,8,9,10,11,12,13]])<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup