def sm_multireg(self,Xtrain,ytrain, Xtest, ytest):
self.normalize(Xtrain)
results = sm.OLS(ytrain, Xtrain).fit_regularized()
#print "coefficient: ", results.params
# train accuracy
predictions = results.predict(Xtrain)
correct = 0
for i in range(len(predictions)):
if round(predictions[i], 1) == ytrain[i]:
correct += 1
accuracy = correct * 1.0 / len(ytrain)
print "train accuracy: ", accuracy * 100, "%"
# calculate SSE, SSM & SST
SSE = 0
for i in range(len(predictions)):
SSE += (predictions[i] - ytrain[i])**2
yAverage = np.mean(ytrain)
SSM = 0
for pred in predictions:
SSM += (pred - yAverage)**2
print "SSM:", SSM
SST = SSE + SSM
print "SST:", SST
# calculate PVE = SSM / SST
PVE = SSM / SST
print "PVE:", PVE
# test accuracy
self.normalize(Xtest)
predictions = results.predict(Xtest)
correct = 0
for i in range(len(predictions)):
print round(predictions[i], 1), ytest[i]
if round(predictions[i], 1) == ytest[i]:
correct += 1
accuracy = correct * 1.0 / len(ytest)
print "test accuracy: ", accuracy * 100, "%"
return results
评论列表
文章目录