问题描述
STrainmatrix = SynTraining.values
STestmatrix = SynTest.values
# read in datasets
Xtrain = STrainmatrix[:,:-1]
ytrain = STrainmatrix[:,-1].astype(int)
Xtest = STestmatrix[:,:-1]
ytest = STestmatrix[:,-1].astype(int)
nte,nf = Xtest.shape # get the dimension of testing samples
# nte is the number of testing samples
# nf is the number of features/dimensions
Pw0 = Pw1 = 0.5
# training process -- derive the model
# also measure the runtime including both training and testing time
t0 = time.time() # start time
arr = Xtrain[ytrain == 0] # get training samples for class 0
covs0 = np.cov(np.transpose(arr)) # calculate Sigma
means0 = np.mean(arr,axis=0) # mean along rows
arr = Xtrain[ytrain == 1] # get training samples for class 1
covs1 = np.cov(np.transpose(arr)) # calculate Sigma
means1 = np.mean(arr,axis=0) # mean along rows
covavg = (covs0 + covs1) / 2 # average of Sigmas
varavg = np.sum(np.diagonal(covavg)) / nf # average of variances
meanavg = (means0 + means1) /2
# testing process -- predict
y = [] # save the classification label of testing samples
for i in range(nte):
g0 = -(np.dot(Xtest[i]-means0,Xtest[i]-means0)) / (2 * varavg) + np.log(Pw0)
g1 = -(np.dot(Xtest[i]-means1,Xtest[i]-means1)) / (2 * varavg) + np.log(Pw1)
if g0 >= g1:
y.append(0)
else:
y.append(1)
t1 = time.time() # ending time
# calculate overall classification accuracy
acc1 = np.count_nonzero(y == ytest) / len(y)
run1 = t1-t0
print(f'The overall accuracy is: {acc1},finished in {run1} seconds')
# training process -- derive the model
# also measure the runtime including both training and testing time
t2 = time.time() # start time
arr = Xtrain[ytrain == 0] # get training samples for class 0
covs0 = np.cov(np.transpose(arr)) # calculate Sigma
means0 = np.mean(arr,axis=0) # mean along rows
covavg = (covs0 + covs1) / 2 # average of Sigmas
varavg = np.sum(np.diagonal(covavg)) / nf # average of variances
meanavg = (means0 + means1) /2
# testing process -- predict
y1 = [] # save the classification label of testing samples
for i in range(nte):
g0 = -((np.dot(np.dot(Xtest[i]-means0,inv(covs0)),(Xtest[i]-means0).T)) /(2)) + np.log(Pw0)
g1 = -((np.dot(np.dot(Xtest[i]-means1,inv(covs1)),(Xtest[i]-means1).T)) /(2)) + np.log(Pw1)
if g0 >= g1:
y1.append(0)
else:
y1.append(1)
t3 = time.time() # ending time
# calculate overall classification accuracy
acc2 = np.count_nonzero(y1 == ytest) / len(y1)
run2 = t3-t2
print(f'The overall accuracy is: {acc2},finished in {run2} seconds')
# training process -- derive the model
# also measure the runtime including both training and testing time
t4 = time.time() # start time
arr = Xtrain[ytrain == 0] # get training samples for class 0
covs0 = np.cov(np.transpose(arr)) # calculate Sigma
means0 = np.mean(arr,axis=0) # mean along rows
covavg = (covs0 + covs1) / 2 # average of Sigmas
varavg = np.sum(np.diagonal(covavg)) / nf # average of variances
meanavg = (means0 + means1) /2
# testing process -- predict
y2 = [] # save the classification label of testing samples
for i in range(nte):
g0 = -((np.dot(np.dot(Xtest[i]- means0,(Xtest[i]- means0).T))/(2)) - (np.log(det(inv(covs0))))/2 + np.log(Pw0)
g1 = -((np.dot(np.dot(Xtest[i]- means1,(Xtest[i]- means1).T))/(2)) - (np.log(det(inv(covs1))))/2 + np.log(Pw1)
if g0 >= g1:
y2.append(0)
else:
y2.append(1)
t5 = time.time() # ending time
# calculate overall classification accuracy
acc3 = np.count_nonzero(y2 == ytest) / len(y2)
run3 = t5-t4
print(f'The overall accuracy is: {acc3},finished in {run3} seconds')
这是执行算法并为我提供准确性和运行时间的代码。如何绘制这三种情况的决策边界?我也试图找到类的准确性,但是我不明白这意味着什么以及我将如何做到这一点。下面是我的尝试。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)