Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas
- from sklearn.base import BaseEstimator, ClassifierMixin
- from sklearn.datasets import make_classification
- from sklearn.metrics import euclidean_distances, accuracy_score
- from sklearn.model_selection import train_test_split
- from sklearn.utils.multiclass import check_classification_targets
- from sklearn.utils.validation import check_X_y, check_is_fitted, check_array
- from math import sqrt
- import math
- class MyCustomClassifier(BaseEstimator, ClassifierMixin):
- def fit(self, X, y):
- X, y = check_X_y(X, y)
- check_classification_targets(y)
- X2 = []
- global mean2
- mean2 = []
- global std2
- std2 = []
- # set number of classes
- global classes
- classes = np.unique(y)
- print(classes)
- global features
- features = X.shape [1]
- print (X.shape)
- print(features)
- #X_1 = [[0 for col in range(features)] for row in range(len(classes))]
- # share between classes
- X2.append([])
- for i in range(len(classes)):
- X2.append([])
- for j in range(len(X)):
- X2.append([])
- if y[j] == i:
- X2[i].append(X[j])
- # count mean and std
- #mean2.append([])
- #std2.append([])
- for i in range(len(classes)):
- mean2.append([])
- std2.append([])
- mean2[i].extend(np.mean(X2[i], axis=0))#extend
- std2[i].extend(np.std(X2[i], axis=0))
- def predict(self, X):
- normdistribution = []
- y_pred_X = []
- y_pred = []
- # normal distribution
- #print (mean2[1][0])
- normdistribution.append([])
- for i in range(len(X)):
- normdistribution.append([])
- for j in range(len(classes)):
- normdistribution[i].append([])
- for k in range(features):
- normdistribution[i][j].append([])
- function = math.exp(-((X[i][k] - mean2[j][k]) ** 2) / (2 * (std2[j][k] ** 2)))/ (std2[j][k] * sqrt(2 * math.pi))
- normdistribution[i][j][k] = function
- y_pred_X.append([])
- for i in range(len(X)):
- y_pred_X.append([])
- for j in range(len(classes)):
- y_pred_X[i].append([])
- for k in range(features):
- if k == 0:
- temp = 1
- else:
- temp = y_pred_X [i][j]
- y_pred_X[i][j] = temp * normdistribution[i][j][k]
- global max
- for i in range(len(X)):
- max = 0
- for j in range(len(classes)-1):
- if y_pred_X[i][j] > y_pred_X[i][j+1]:
- max = j
- else:
- max = j+1
- y_pred.append(max)
- return y_pred
- # Input validation
- '''
- X, y = make_classification(n_samples=1000, n_features=2, n_redundant=0, n_classes=2, random_state=0)
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
- Gauss = MyCustomClassifier()
- Gauss.fit(X_train, y_train)
- y=Gauss.predict(X_test)
- print("ACC %.3f in own implementation" % accuracy_score(y, y_test))
- '''
- from sklearn.utils.estimator_checks import check_estimator
- check_estimator(MyCustomClassifier)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement