Question: trying to run code but getting error likelihood_function() missing 1 required positional argument: 'var' when running y_pred=clf.predict(x). All imports are done and work correctly and
trying to run code but getting error "likelihood_function() missing 1 required positional argument: 'var'" when running y_pred=clf.predict(x). All imports are done and work correctly and predict function worked using normal likelihood
clf class is:
class NaiveBayesClassifier: def __init__(self, likelihood='normal', k=None): self.likelihood = likelihood # Let # K = number of unique classes # N = number of test instances # d = number of inputs (input dimensionality) # Numpy array unique classes, shape = (K,) self.classes = None # Numpy array of class priors, P(C), shape self.priors = None # Numpy array of likelihoods, P(x|C), shape (N, K), self.likelihoods = None # Numpy array of posterior probabilities, P(C|x), shape = (N, K) self.posteriors = None ## For the Guassian Density # means, shape = (K, d) self.avgs = None # variances, shape = (K, d) self.vars = None ## For the knn Density # number of neighbors to use self.k = k # store training X self.X_train = None # store trainging y self.y_train = None def generate_classes(self, y): """ Generate the classes based on y, and store in self.classes :param y: array of class targets """ self.classes = np.unique(y) def generate_priors(self, y): """ Compute the prior probabilities and store self.priors :param y: array of class targets """ self.priors = np.array([np.mean(y == c) for c in self.classes]) def knn_density_function(self, x_train, x_predict): """ Implements k-nearest neighbor density estimate (Alpaydin Eq 8.8) :param x_train: 1d numpy array :param x_predict: 1d numpy array :returns probabilities at x predict, shape = x_predict.shape """ # Find the distance to kth nearest neighbor result = [] for x0 in x_predict: dist = np.abs(x_train - x0) index = np.argsort(dist) result.append(dist[index[self.k-1]]) dist_k = np.array(result) # Find the probability at x using knn density # Note: Equation 8.8 may return probabilites greater than 1. # For probabilities greater than 1, set it equal to 1. probabilities = (self.k / (2 * len(x_train) * dist_k)) * np.sum(np.abs(x_train - x0) < dist_k.reshape(-1, 1), axis=0) probabilities[probabilities > 1] = 1 return probabilities # Gaussian part def generate_avgs(self, x, y): """ Return mean for each class and for each attribute """ avgs = [] for c in self.classes: avgs.append(np.mean(x[y == c], axis=0)) return np.array(avgs) def generate_vars(self, x, y):
n_classes = np.unique(y).shape[0] n_features = x.shape[1]
variances = np.zeros((n_classes, n_features))
for i in range(n_classes): class_indices = np.where(y == i)[0] class_data = x[class_indices]
for j in range(n_features): variances[i][j] = np.var(class_data[:, j])
return variances
def fit(self, x, y): """ Fit the Naive Bayes Classifier to the data. :param x: numpy array of input features :param y: numpy array of class targets """ self.generate_classes(y) self.generate_priors(y) if self.likelihood == 'normal': self.avgs = self.generate_avgs(x, y) self.vars = self.generate_vars(x, y) elif self.likelihood == 'knn': self.X_train = x self.y_train = y else: raise ValueError("Invalid likelihood function specified")
def likelihood_function(self, x, mean, var): """ Implements the likelihood function for each attribute :param x: 1d numpy array of input feature values :param mean: mean of the attribute's values :param var: variance of the attribute's values :return: likelihood of the attribute's values """ if self.likelihood == 'normal': # Gaussian Density return norm(mean, np.sqrt(var)).pdf(x) elif self.likelihood == 'knn': # KNN Density return self.knn_density_function(self.X_train[:, None], x)
def fit(self, x, y): """ Fit the Naive Bayes model to the training data :param x: array of input features :param y: array of class targets """ # Generate the classes and priors self.generate_classes(y) self.generate_priors(y) if self.likelihood == 'normal': # Generate the means and variances for each attribute self.avgs = self.generate_avgs(x, y) self.vars = self.generate_vars(x, y) elif self.likelihood == 'knn': self.X_train = x self.y_train = y def predict(self, x): """ Predict the class targets for input data :param x: array of input features :return: array of predicted class targets """ if self.likelihood == 'normal': # Compute the likelihoods self.likelihoods = np.zeros((x.shape[0], len(self.classes))) for i, c in enumerate(self.classes): self.likelihoods[:,i] = np.prod(self.likelihood_function(x, self.avgs[i], self.vars[i]), axis=1) elif self.likelihood == 'knn': # Compute the likelihoods self.likelihoods = np.zeros((x.shape[0], len(self.classes))) for i, c in enumerate(self.classes): self.likelihoods[:,i] = np.prod(self.likelihood_function(x, self.X_train[self.y_train == c]), axis=1) # Compute the posteriors self.posteriors = self.likelihoods * self.priors self.posteriors /= np.sum(self.posteriors, axis=1, keepdims=True) # Return the class with the highest posterior return self.classes[np.argmax(self.posteriors, axis=1)] def score(self, X, y, sample_weight=None): return accuracy_score(self.predict(X), y, sample_weight=sample_weight)
code to run is:
iris = load_iris()
x = iris['data']
y = iris['target']
# Create an instance of the classifier with a normal likelihood distribution
clf = NaiveBayesClassifier(likelihood='knn', k=3)
# # Fit the classifier to the training data
clf.fit(x, y)
# # Use the classifier to make predictions on new data
y_pred = clf.predict(x)
# # Evaluate the accuracy of the classifier
accuracy = clf.score(x, y)
print('Accuracy:', accuracy)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
