大佬们帮忙看看这个最最最基本的朴素贝叶斯代码有什么问题啊
import randomdef generate_dataset(num_instances, num_attributes):
X = [] # all feature values for each instance in the dataset
y = [] # all label values for each instance in the dataset
for i in range(num_instances):
current_instance = []
for j in range(num_attributes):
current_instance.append(random.randint(1,3))
X.append(current_instance)
y.append(random.randint(0,1))
return (X, y)
def print_dataset(dataset):
X = dataset
y = dataset
print("Dataset:")
for i in range(len(X)):
for j in range(len(X)):
print(str(X), end = "\t")
print(y)
def NB(training_dataset, testing_dataset) :
X_training = training_dataset
y_training = training_dataset
X_testing = testing_dataset
Freq_1 = 0
Freq_0 = 0
predicted_y = []
print(X_training)###
print(y_training)###
print(X_testing) ###
for h in y_training :
if h == 1 :
Freq_1 += 1
else :
Freq_0 += 1
P_0 = Freq_1/(len(y_training))
P_1 = Freq_0/(len(y_training))
for i in range(len(X_testing)):
Final_P_1 = 1
Final_P_0 = 1
for j in range(len(X_testing)) :
freq_P_j_1 = 0
freq_P_j_0 = 0
for k in range(len(X_training)) :
if X_training == X_testing and y_training == 1 :
freq_P_j_1 += 1
elif X_training == X_testing and y_training == 0 :
freq_P_j_0 += 0
P_j_1 = freq_P_j_1/(freq_P_j_1 + freq_P_j_0)
P_j_0 = freq_P_j_0/(freq_P_j_1 + freq_P_j_0)
Final_P_1 = Final_P_1 * P_j_1
Final_P_0 = Final_P_0 * P_j_0
Final_P_1 = Final_P_1 * P_1
Final_P_0 = Final_P_0 * P_0
if Final_P_1 > Final_P_0 :
Output = 1
elif Final_P_1 < Final_P_0 :
Output = 0
predicted_y.append(Output)
return predicted_y
最后的预测值为什么全都是1... emmm机器学习还没学....不会
页:
[1]