0% found this document useful (0 votes)
24 views

SVM & RF

The document describes source code to predict diabetes using support vector machines (SVM) and random forest (RF) classifiers. The code loads diabetes patient data, splits it into training and test sets, trains SVM and RF models on the training set, and evaluates the models by measuring accuracy on the test set. Key metrics like confusion matrices and classification reports are also printed to evaluate the models' performance on the test data.

Uploaded by

shivamsaxena3718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

SVM & RF

The document describes source code to predict diabetes using support vector machines (SVM) and random forest (RF) classifiers. The code loads diabetes patient data, splits it into training and test sets, trains SVM and RF models on the training set, and evaluates the models by measuring accuracy on the test set. Key metrics like confusion matrices and classification reports are also printed to evaluate the models' performance on the test data.

Uploaded by

shivamsaxena3718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

 Write a program for diabetes prediction using SVM & RF.

 Source code: (SVM)


import numpy as np
import pandas as pd
diabetes_df = pd.read_csv('diabetes.csv')
diabetes_df_copy = diabetes_df.copy(deep = True)
diabetes_df_copy[['Glucose','BloodPressure','SkinThickness','Insulin','BMI']] =
diabetes_df_copy[['Glucose','BloodPressure','SkinThickness','Insulin','BMI']]
.replace(0,np.NaN)
X = diabetes_df.drop('Outcome', axis=1)
y = diabetes_df['Outcome']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.33,
random_state=7)
from sklearn.svm import SVC
svc_model = SVC()
svc_model.fit(X_train, y_train)
svc_pred = svc_model.predict(X_test)
from sklearn import metrics
print("Accuracy Score =", format(metrics.accuracy_score(y_test, svc_pred)))

from sklearn.metrics import classification_report, confusion_matrix


print(confusion_matrix(y_test, svc_pred))
print(classification_report(y_test,svc_pred))
 Source code: (RF)
import numpy as np
import pandas as pd
diabetes_df = pd.read_csv('diabetes.csv')
diabetes_df_copy = diabetes_df.copy(deep = True)
diabetes_df_copy[['Glucose','BloodPressure','SkinThickness','Insulin','BMI']] =
diabetes_df_copy[['Glucose','BloodPressure','SkinThickness','Insulin','BMI']]
.replace(0,np.NaN)
X = diabetes_df.drop('Outcome', axis=1)
y = diabetes_df['Outcome']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.33,
random_state=7)
from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(n_estimators=200)
rfc.fit(X_train, y_train)
rfc_train = rfc.predict(X_train)
from sklearn import metrics
print("Accuracy_Score =", format(metrics.accuracy_score(y_train, rfc_train)))

from sklearn import metrics


predictions = rfc.predict(X_test)
print("Accuracy_Score =", format(metrics.accuracy_score(y_test, predictions)))

from sklearn.metrics import classification_report, confusion_matrix


print(confusion_matrix(y_test, predictions))
print(classification_report(y_test,predictions))

You might also like