-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyapp.py
89 lines (69 loc) · 2.91 KB
/
myapp.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import numpy as np
import pandas as pd
import streamlit as st
from PIL import Image
from sklearn import svm
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# load the diabetes dataset
diabetes_df = pd.read_csv('diabetes.csv')
# group the data by outcome to get a sense of the distribution
diabetes_mean_df = diabetes_df.groupby('Outcome').mean()
# split the data into input and target variables
X = diabetes_df.drop('Outcome', axis=1)
y = diabetes_df['Outcome']
# scale the input variables using StandardScaler
scaler = StandardScaler()
scaler.fit(X)
X = scaler.transform(X)
# split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=20, random_state=1)
# create an SVM model with a linear kernel
model = svm.SVC(kernel='linear')
# train the model on the training set
model.fit(X_train, y_train)
# make predictions on the training and testing sets
train_y_pred = model.predict(X_train)
test_y_pred = model.predict(X_test)
# calculate the accuracy of the model on the training and testing sets
train_acc = accuracy_score(train_y_pred, y_train)
test_acc = accuracy_score(test_y_pred, y_test)
# create the Streamlit app
def app():
img = Image.open(r"img.jpeg")
img = img.resize((200,200))
st.image(img,caption="Diabetes Image",width=200)
st.title('Diabetes Prediction')
# create the input form for the user to input new data
st.sidebar.title('Input Features')
preg = st.sidebar.slider('Pregnancies', 0, 17, 3)
glucose = st.sidebar.slider('Glucose', 0, 199, 117)
bp = st.sidebar.slider('Blood Pressure', 0, 122, 72)
skinthickness = st.sidebar.slider('Skin Thickness', 0, 99, 23)
insulin = st.sidebar.slider('Insulin', 0, 846, 30)
bmi = st.sidebar.slider('BMI', 0.0, 67.1, 32.0)
dpf = st.sidebar.slider('Diabetes Pedigree Function', 0.078, 2.42, 0.3725, 0.001)
age = st.sidebar.slider('Age', 21, 81, 29)
# make a prediction based on the user input
input_data = [preg, glucose, bp, skinthickness, insulin, bmi, dpf, age]
input_data_array = np.asarray(input_data)
input_data_scaled = scaler.transform(input_data_array.reshape(1, -1))
prediction = model.predict(input_data_scaled)
# display the prediction to the user
st.write('Based on the input features, the model predicts:')
if prediction[0] == 1:
st.warning('This person has diabetes.')
else:
st.success('This person does not have diabetes.')
# display some summary statistics about the dataset
st.header('Dataset Summary')
st.write(diabetes_df.describe())
st.header('Distribution by Outcome')
st.write(diabetes_mean_df)
# display the model accuracy
st.header('Model Accuracy')
st.write(f'Train set accuracy: {train_acc:.2f}')
st.write(f'Test set accuracy: {test_acc:.2f}')
if __name__ == '__main__':
app()