Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1. import numpy as np
  2. import pandas
  3. from sklearn.base import BaseEstimator, ClassifierMixin
  4. from sklearn.datasets import make_classification
  5. from sklearn.metrics import euclidean_distances, accuracy_score
  6. from sklearn.model_selection import train_test_split
  7. from sklearn.utils.multiclass import check_classification_targets
  8. from sklearn.utils.validation import check_X_y, check_is_fitted, check_array
  9. from math import sqrt
  10. import math
  11.  
  12.  
  13. class MyCustomClassifier(BaseEstimator, ClassifierMixin):
  14.  
  15.     def fit(self, X, y):
  16.         X, y = check_X_y(X, y)
  17.         check_classification_targets(y)
  18.         X2 = []
  19.         global mean2
  20.         mean2 = []
  21.         global std2
  22.         std2 = []
  23.  
  24.         # set number of classes
  25.         global classes
  26.         classes = np.unique(y)
  27.         print(classes)
  28.         global features
  29.         features = X.shape [1]
  30.         print (X.shape)
  31.         print(features)
  32.         #X_1 = [[0 for col in range(features)] for row in range(len(classes))]
  33.  
  34.     # share between classes
  35.         X2.append([])
  36.         for i in range(len(classes)):
  37.             X2.append([])
  38.             for j in range(len(X)):
  39.                 X2.append([])
  40.                 if y[j] == i:
  41.                     X2[i].append(X[j])
  42.  
  43.     # count mean and std
  44.  
  45.         #mean2.append([])
  46.         #std2.append([])
  47.         for i in range(len(classes)):
  48.             mean2.append([])
  49.             std2.append([])
  50.             mean2[i].extend(np.mean(X2[i], axis=0))#extend
  51.             std2[i].extend(np.std(X2[i], axis=0))
  52.  
  53.  
  54.     def predict(self, X):
  55.         normdistribution = []
  56.         y_pred_X = []
  57.         y_pred = []
  58.  
  59. # normal distribution
  60.         #print (mean2[1][0])
  61.         normdistribution.append([])
  62.         for i in range(len(X)):
  63.             normdistribution.append([])
  64.             for j in range(len(classes)):
  65.                 normdistribution[i].append([])
  66.                 for k in range(features):
  67.                     normdistribution[i][j].append([])
  68.                     function = math.exp(-((X[i][k] - mean2[j][k]) ** 2) / (2 * (std2[j][k] ** 2)))/ (std2[j][k] * sqrt(2 * math.pi))
  69.                     normdistribution[i][j][k] = function
  70.         y_pred_X.append([])
  71.         for i in range(len(X)):
  72.             y_pred_X.append([])
  73.             for j in range(len(classes)):
  74.                 y_pred_X[i].append([])
  75.                 for k in range(features):
  76.                     if k == 0:
  77.                         temp = 1
  78.                     else:
  79.                         temp = y_pred_X [i][j]
  80.                     y_pred_X[i][j] = temp * normdistribution[i][j][k]
  81.  
  82.         global max
  83.  
  84.         for i in range(len(X)):
  85.             max = 0
  86.             for j in range(len(classes)-1):
  87.                 if y_pred_X[i][j] > y_pred_X[i][j+1]:
  88.                     max = j
  89.                 else:
  90.                     max = j+1
  91.  
  92.             y_pred.append(max)
  93.  
  94.         return y_pred
  95. # Input validation
  96. '''
  97. X, y = make_classification(n_samples=1000, n_features=2, n_redundant=0, n_classes=2, random_state=0)
  98. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
  99.  
  100. Gauss = MyCustomClassifier()
  101. Gauss.fit(X_train, y_train)
  102. y=Gauss.predict(X_test)
  103.  
  104. print("ACC %.3f in own implementation" % accuracy_score(y, y_test))
  105.  
  106. '''
  107. from sklearn.utils.estimator_checks import check_estimator
  108.  
  109. check_estimator(MyCustomClassifier)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement