generated from streamlit/basic-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprediction.py
61 lines (50 loc) · 1.76 KB
/
prediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
from sklearn.preprocessing import StandardScaler
#from main import user_data
# Load the data
data = pd.read_csv("heart_data.csv")
# Preprocess the data (assuming 'target' is the label column)
X = data.drop(columns=['target'])
y = data['target']
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=40)
# Scale the data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Train the model
model = LogisticRegression()
model.fit(X_train, y_train)
def predict_heart_disease(user_data):
# Scale the user input data
user_data = np.array(user_data).reshape(1, -1)
user_data = scaler.transform(user_data)
# Make a prediction
prediction = model.predict(user_data)
return prediction[0]
def get_accuracy():
#Calculate accuracy on the test set
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
accuracy_percentage = accuracy * 100
print("Accuracy on test set:", accuracy_percentage,"%")
return accuracy_percentage
# Example usage (simulating frontend input):
"""
user_data = [51,1,2,94,227,0,0,154,1,0,0,1,3]
prediction_result = predict_heart_disease(user_data)
print(f"Target: {prediction_result}")
# Print age and sex from user_data
age = user_data[0]
sex = user_data[1]
print(f"Age: {age}")
print(f"Sex: {'Male' if sex == 1 else 'Female'}")
if prediction_result == 0:
print('The Person does not have a Heart Disease')
else:
print('The Person has Heart Disease')
"""