import sys
import math
import xlrd
import xlwt
import xlutils
import data_pre
import xlsxwriter
import numpy as np
from sklearn import svm
from sklearn import preprocessing
from sklearn import model_selection
from sklearn.impute import SimpleImputer
from sklearn.metrics import accuracy_score
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.cross_decomposition import PLSRegression
def PLS():
for tag in [10,20,30,50,100,200,300]:
x, y = data_pre.load('data_test_mean_mean.csv')
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1, train_size=0.7)
pls2 = PLSRegression(n_components = tag)
pls2.fit(x_train, y_train.ravel())
y_test_pre = pls2.predict(x_test)
sumd,mxd,cnt = 0.0,0.0,0.0
for i in range (len(y_test_pre)):
if(math.fabs(y_test_pre[i]-y_test[i])>1):
cnt+=1
sumd+=math.fabs(y_test_pre[i]-y_test[i])
mxd = max(mxd,math.fabs(y_test_pre[i]-y_test[i]))
sumd/=len(y_test_pre)
cnt/=len(y_test_pre)
accuracy =pls2.score(x_test, y_test)
print('{:.2f},{:.2f},{:.2f},{:.2f}%'.format(accuracy,sumd,mxd,cnt*100))
if __name__ == '__main__':
PLS()