<jupyter_start><jupyter_text>New Section<jupyter_code>df = pd.read_csv('diabetes.csv')
df
df.describe()
features = ['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
for i in features:
  sns.histplot(df[i])
  plt.show()
df.plot(subplots=True, figsize=(10, 10))
plt.show()
df.info()
df.isnull().sum()
X = df[df.columns[0:8]]
Y = df[df.columns[8]]
from sklearn.preprocessing import StandardScaler
q = StandardScaler().fit_transform(X)
X_s = pd.DataFrame(q,columns=features)
X_s
from sklearn.model_selection import train_test_split
x, x_test, y, y_test = train_test_split(X_s,Y,test_size=0.1,train_size=0.9)
x_train, x_cv, y_train, y_cv = train_test_split(x,y,test_size = 0.22,train_size =0.78)<jupyter_output><empty_output><jupyter_text>KNN<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
n_neighbors = [3,5,7,10]
from sklearn.model_selection import cross_val_score
for i in n_neighbors:
  clf = KNeighborsClassifier(n_neighbors=i,algorithm='auto')
  scores = cross_val_score(clf, x_cv, y_cv, cv=5)
  print("cross validation score for",i,":",scores.mean())
#when n_neighbors=3
clf = KNeighborsClassifier(n_neighbors=3,algorithm='auto')
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))
#when n_neighbors=5
clf = KNeighborsClassifier(n_neighbors=5,algorithm='auto')
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))
#when n_neighbors=7
clf = KNeighborsClassifier(n_neighbors=7,algorithm='auto')
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))
#when n_neighbors=10
clf = KNeighborsClassifier(n_neighbors=10,algorithm='auto')
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))
#grid search for best parameters
from sklearn.model_selection import GridSearchCV
clf = KNeighborsClassifier()
param_grid = {'n_neighbors': [3,5,7,10,15],'algorithm': ['auto','ball_tree','kd_tree','brute']} 
grid = GridSearchCV(clf, param_grid, refit = True, verbose = 3)
grid.fit(x_train,y_train)

print(grid.best_estimator_)
#knn on best paramters
clf_ = KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',metric_params=None, n_jobs=None, n_neighbors=15, p=2, weights='uniform')
clf_.fit(x_train,y_train)
y_pred = clf_.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))<jupyter_output>Accuracy: 0.7402597402597403
F1 score: 0.5833333333333334
confusion matrix -
[[43  7]
 [13 14]]<jupyter_text>MLP<jupyter_code>from sklearn.neural_network import MLPClassifier
hidden_layer_sizes = [50,100,150,200]
for i in hidden_layer_sizes:
  clf = MLPClassifier(hidden_layer_sizes=i,activation='relu',max_iter=10000)
  scores = cross_val_score(clf, x_cv, y_cv, cv=5)
  print("cross validation score for",i,":",scores.mean())
#when hidden layer size = 50
clf = MLPClassifier(hidden_layer_sizes=50, max_iter=10000)
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))
#when hidden layer size = 100
clf = MLPClassifier(hidden_layer_sizes=100, max_iter=10000)
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))
#when hidden layer size = 150
clf = MLPClassifier(hidden_layer_sizes=150, max_iter=10000)
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))
#when hidden layer size = 200
clf = MLPClassifier(hidden_layer_sizes=200, max_iter=10000)
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))
#grid search for best parameters

clf = MLPClassifier()
param_grid = {'hidden_layer_sizes': [50,100,150,200,250]} 
grid = GridSearchCV(clf, param_grid, refit = True, verbose = 3)
grid.fit(x_train,y_train)

print(grid.best_estimator_)
clf = MLPClassifier(activation='relu', alpha=0.0001, batch_size='auto', beta_1=0.9,
              beta_2=0.999, early_stopping=False, epsilon=1e-08,
              hidden_layer_sizes=100, learning_rate='constant',
              learning_rate_init=0.001, max_fun=15000, max_iter=10000,
              momentum=0.9, n_iter_no_change=10, nesterovs_momentum=True,
              power_t=0.5, random_state=None, shuffle=True, solver='adam',
              tol=0.0001, validation_fraction=0.1, verbose=False,
              warm_start=False)
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))
clf = MLPClassifier(solver="adam", max_iter=5000, activation = "relu",
                    hidden_layer_sizes = (12),                      
                    alpha = 0.01,
                    batch_size = 64,
                    learning_rate_init = 0.001,
                    random_state=2)
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))<jupyter_output><empty_output><jupyter_text>Boosting<jupyter_code>from lightgbm import LGBMClassifier
model_ = LGBMClassifier(n_estimators=100,num_leaves=35,learning_rate=0.1,max_bin=255,max_depth=-1)
model_.fit(x_train,y_train)
pred_lgm=model_.predict(x_test)
x_lgm=accuracy_score(y_test,pred_lgm)
print(f'Accuracy: {x_lgm}')
from sklearn.ensemble import BaggingClassifier
clf_2 = BaggingClassifier(base_estimator= LGBMClassifier(), n_estimators=200).fit(x_train,y_train)
pred_bag_=clf_2.predict(x_test)
x_bag_=accuracy_score(y_test,pred_bag_)
print(f'the accuracy score is {x_bag_}')<jupyter_output><empty_output><jupyter_text>SVM<jupyter_code>from sklearn import svm
clf = svm.SVC()
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))
from sklearn.svm import NuSVC
clf = NuSVC()
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))<jupyter_output>Accuracy: 0.7272727272727273
F1 score: 0.5714285714285714
confusion matrix -
[[42  8]
 [13 14]]<jupyter_text>Logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(random_state=0, penalty="l2", class_weight="balanced", solver="saga", max_iter=5000)
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))<jupyter_output>Accuracy: 0.7532467532467533
F1 score: 0.6885245901639345
confusion matrix -
[[37 13]
 [ 6 21]]<jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
clf=RandomForestClassifier(max_depth=150,n_estimators=100)
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 score:", f1_score(y_test, y_pred))
print("confusion matrix -")
print( confusion_matrix(y_test,y_pred))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1385930001170549, 'fraction_numerical': 0.042959147840337115, 'mean_word_length': 7.763076923076923, 'pattern_counts': {'":': 2, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '658711', 'license': 'MIT', 'n_tokens_mistral': 3606, 'n_tokens_neox': 3339, 'n_words': 536, 'path': 'Diabetes Classification/diabetes_classification.ipynb', 'repo_name': 'void-02/Awesome-Machine-Learning-Models', 'size': '8543'}