mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-07 10:00:55 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
2093504c21
commit
f7d56fa6cb
|
@ -71,10 +71,14 @@ class MultinomialNBClassifier:
|
|||
data_class_i = data[grouped_indices[class_i]]
|
||||
prior_class_i = data_class_i.shape[0] / n_examples
|
||||
self.priors[i] = prior_class_i
|
||||
tot_features_count = data_class_i.sum() # count of all features in class_i
|
||||
features_count = np.array(data_class_i.sum(axis=0))[0] # count of each feature x_j in class_i
|
||||
tot_features_count = data_class_i.sum() # count of all features in class_i
|
||||
features_count = np.array(data_class_i.sum(axis=0))[
|
||||
0
|
||||
] # count of each feature x_j in class_i
|
||||
for j, n_j in enumerate(features_count):
|
||||
self.features_probs[i][j] = (self.alpha + n_j) / (tot_features_count + self.alpha * n_features)
|
||||
self.features_probs[i][j] = (self.alpha + n_j) / (
|
||||
tot_features_count + self.alpha * n_features
|
||||
)
|
||||
|
||||
def predict(self, data: sparse.csr_matrix) -> np.array:
|
||||
"""
|
||||
|
@ -106,7 +110,10 @@ class MultinomialNBClassifier:
|
|||
log_priors = np.log(self.priors)
|
||||
for instance in data:
|
||||
theta = instance.multiply(log_features_probs).sum(axis=1)
|
||||
likelihood = [log_prior_class_i + theta[i] for i, log_prior_class_i in enumerate(log_priors)]
|
||||
likelihood = [
|
||||
log_prior_class_i + theta[i]
|
||||
for i, log_prior_class_i in enumerate(log_priors)
|
||||
]
|
||||
y_pred.append(self.classes[np.argmax(likelihood)])
|
||||
return np.array(y_pred)
|
||||
|
||||
|
@ -115,13 +122,13 @@ def main() -> None:
|
|||
"""
|
||||
Performs the text classification on the twenty_newsgroup dataset from sklearn
|
||||
"""
|
||||
newsgroups_train = fetch_20newsgroups(subset='train')
|
||||
newsgroups_test = fetch_20newsgroups(subset='test')
|
||||
x_train = newsgroups_train['data']
|
||||
y_train = newsgroups_train['target']
|
||||
x_test = newsgroups_test['data']
|
||||
y_test = newsgroups_test['target']
|
||||
vectorizer = TfidfVectorizer(stop_words='english')
|
||||
newsgroups_train = fetch_20newsgroups(subset="train")
|
||||
newsgroups_test = fetch_20newsgroups(subset="test")
|
||||
x_train = newsgroups_train["data"]
|
||||
y_train = newsgroups_train["target"]
|
||||
x_test = newsgroups_test["data"]
|
||||
y_test = newsgroups_test["target"]
|
||||
vectorizer = TfidfVectorizer(stop_words="english")
|
||||
x_train = vectorizer.fit_transform(x_train)
|
||||
x_test = vectorizer.transform(x_test)
|
||||
|
||||
|
@ -130,10 +137,12 @@ def main() -> None:
|
|||
model.fit(x_train, y_train)
|
||||
|
||||
y_pred = model.predict(x_test)
|
||||
print("Accuracy of naive bayes text classifier: " + str(accuracy_score(y_test, y_pred)))
|
||||
print(
|
||||
"Accuracy of naive bayes text classifier: "
|
||||
+ str(accuracy_score(y_test, y_pred))
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
doctest.testmod()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user