Created
January 16, 2022 15:10
-
-
Save EvanGertis/39c4b382dbe799a27f4f6b989d6f7e55 to your computer and use it in GitHub Desktop.
BayesianClassifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def BayesClassifier(training_set,test_set): | |
classAttribute = 'Volume' | |
products = [] | |
max = -math.inf | |
classWithMaxValue = "" | |
for x in training_set[classAttribute].unique(): | |
D = len(training_set[classAttribute].index) | |
d = len(training_set[training_set[classAttribute] == x].index) | |
pClassAttribute = d/D | |
print("********") | |
print(f'Step 1 calculate p({classAttribute}={x})={pClassAttribute}') | |
p = 0 | |
probabilitiesProduct = 1 | |
print("********") | |
print("Step 2 calculate product of probabilities") | |
for A, values in training_set.iteritems(): | |
if not A == classAttribute: | |
v = training_set[A].iloc[0] | |
p = prob_continous_value(A, v, classAttribute, training_set, x) | |
print(f'p({A}={v}|{classAttribute}={x})={p}') | |
probabilitiesProduct *= p | |
print(f"probabilitiesProduct={probabilitiesProduct}") | |
print("********") | |
# products.append(probabilitiesProduct) | |
ptotal = pClassAttribute*probabilitiesProduct | |
print(f'p({classAttribute}={x}|x)={ptotal}') | |
if ptotal > max: | |
max = ptotal | |
classWithMaxValue = x | |
print(f"winner is {classAttribute}={classWithMaxValue}") | |
calculate_metrics(tp, tn, fn, fp, p, n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment