|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random
def 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[0]
y = dataset[1]
print("Dataset:")
for i in range(len(X)):
for j in range(len(X[i])):
print(str(X[i][j]), end = "\t")
print(y[i])
def NB(training_dataset, testing_dataset) :
X_training = training_dataset[0]
y_training = training_dataset[1]
X_testing = testing_dataset[0]
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[i])) :
freq_P_j_1 = 0
freq_P_j_0 = 0
for k in range(len(X_training)) :
if X_training[k][j] == X_testing[i][j] and y_training[k] == 1 :
freq_P_j_1 += 1
elif X_training[k][j] == X_testing[i][j] and y_training[k] == 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
|
|