multi_reg.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:aq_weather 作者: eliucidate 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号