machine learning projects with python code example

Example: machine learning projects in python

from sklearn.datasets import load_wine
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import StackingClassifier, RandomForestClassifier
from sklearn.model_selection import StratifiedShuffleSplit
random_state = 42

X,y = load_wine(return_X_y = True)
mean, std = X.mean(axis = 0), X.std(axis = 0)
X = (X - mean)/std
def make_models():
  models = []
  models.append(('rfc', RandomForestClassifier(random_state = random_state, n_estimators = 20)))
  models.append(('svm', SVC(C = 0.01)))
  models.append(('gnb', GaussianNB()))
  return models

models = make_models()
clf = StackingClassifier(estimators = models, final_estimator = RandomForestClassifier(random_state = random_state),)

scores = []
i = 0
for indices in kfold.split(X, y):
  train_indices, test_indices = indices
  X_train, X_test = X[train_indices], X[test_indices] # Task 2 split the data set with StratifiedKFold
  y_train, y_test = y[train_indices], y[test_indices]
  clf.fit(X_train, y_train)
  curr_score = clf.score(X_test, y_test)
  i+=1
  print(f'{i}- Fold #{i} accuracy = ', str(round(curr_score,4)*100) + '%') # Task 4 dispaly the score
  scores.append(curr_score)
print(10*'--')
print('The mean of  scores is: ', str(round(sum(scores)/k,4)*100) + '%') #Task 4 display the mean of scors

#for more projects:
#https://github.com/MohammedAlbaqerH