[5369f3]: / diabatics.py

Download this file

129 lines (100 with data), 4.0 kB

  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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.metrics import accuracy_score
dataset=pd.read_csv('diabetes.csv')
# print(dataset.head())
print(dataset.shape)
# print(dataset.describe())
# print(dataset['Outcome'].value_counts())
# print(dataset.groupby('Outcome').mean())
#seperating data and labels
X=dataset.drop(columns='Outcome',axis=1)
Y=dataset["Outcome"]
print(X)
# print(Y)
# standardisation
scaler=StandardScaler()
scaler.fit(X)
std_data=scaler.transform(X)
print(std_data)
X=std_data
# print(X)
# print(Y)
#splitting training and training set
x_train,x_test,y_train,y_test=train_test_split(X,Y,test_size=0.1,stratify=Y,random_state=3)
print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)
#training the model
classifier=svm.SVC(kernel='linear')
#training the support vector machine classifier
print(classifier.fit(x_train,y_train))
#model evaluation
x_train_accuracy=classifier.predict(x_train)
training_data_accuracy=accuracy_score(x_train_accuracy,y_train)
print("accuracy score of training data:",training_data_accuracy)
x_test_accuracy=classifier.predict(x_test)
test_data_accuracy=accuracy_score(x_test_accuracy,y_test)
print("accuracy score of test data:",test_data_accuracy)
#making a prediction
input_data=(10,100,88,60,110,46.8,0.942,31)
#changing the input data to numpy array
numpy_array=np.asarray(input_data)
#reshape the array
reshaped=numpy_array.reshape(1,-1)
std_data=scaler.transform(reshaped)
# print(std_data)
prediction=classifier.predict(std_data)
# print(prediction)
if(prediction[0]==0):
print("the person is not diabetic")
else:
print("the person is diabetic")
# import numpy as np
# import pandas as pd
# from sklearn.preprocessing import StandardScaler
# from sklearn.model_selection import train_test_split
# from sklearn import svm
# from sklearn.metrics import accuracy_score
# class DiabetesPredictor:
# def __init__(self, dataset_path):
# self.dataset_path = dataset_path
# self.model = svm.SVC(kernel='linear')
# self.scaler = StandardScaler()
# def load_data(self):
# dataset = pd.read_csv(self.dataset_path)
# X = dataset.drop(columns='Outcome', axis=1)
# Y = dataset['Outcome']
# return X, Y
# def preprocess_data(self, X):
# self.scaler.fit(X)
# return self.scaler.transform(X)
# def train_model(self, X, Y):
# X_train, X_test, Y_train, Y_test = train_test_split(
# X, Y, test_size=0.2, stratify=Y, random_state=2
# )
# self.model.fit(X_train, Y_train)
# # Evaluate the model
# train_accuracy = accuracy_score(Y_train, self.model.predict(X_train))
# test_accuracy = accuracy_score(Y_test, self.model.predict(X_test))
# return train_accuracy, test_accuracy
# def predict(self, input_data):
# input_array = np.asarray(input_data).reshape(1, -1)
# standardized_data = self.scaler.transform(input_array)
# prediction = self.model.predict(standardized_data)
# return 'Diabetic' if prediction[0] == 1 else 'Not Diabetic'
# if __name__ == "__main__":
# # Initialize predictor
# predictor = DiabetesPredictor('diabetes.csv')
# # Load and preprocess data
# X, Y = predictor.load_data()
# X = predictor.preprocess_data(X)
# # Train the model
# train_accuracy, test_accuracy = predictor.train_model(X, Y)
# print(f"Training Accuracy: {train_accuracy:.2f}")
# print(f"Testing Accuracy: {test_accuracy:.2f}")
# # Make a prediction
# input_data = (5, 166, 72, 19, 175, 25.8, 0.587, 51)
# result = predictor.predict(input_data)
# print(f"Prediction for input data: {result}")