File: C:/xampp/htdocs/FograCT1/machineLearningGrayRR.py
# from statistics import _sum
import pandas as pd
import numpy as np
#import seaborn as sns
#import pyodbc # connect to sql server
import matplotlib.pyplot as plt
from pandas import DataFrame, Series
from pyexpat import model
from sklearn.feature_selection import f_regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# import pandas
from sklearn import linear_model
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import *
dfCR30 = pd.read_csv("grayLab127.csv")
dfPro3 = pd.read_csv("grayRGB127.csv")
#yLm_value = df2['lm'].values
pathL = 'grayML.php'
fL = open(pathL, 'w')
path = 'output.txt'
f = open(path, 'w')
#print(dfPro3.sample(n=72))
########################################################
X_value = dfCR30[['L', 'a', 'b']].values
yL_value = dfPro3['R'].values
ya_value = dfPro3['G'].values
yb_value = dfPro3['B'].values
degree = 2
poly_model = PolynomialFeatures(degree=degree)
poly_x_values = poly_model.fit_transform(X_value)
regression_model = LinearRegression()
regression_model.fit(poly_x_values, yL_value)
yL_pred = regression_model.predict(poly_x_values)
f.write(str(yL_pred))
print("yL_pred 52:" + str(yL_pred))
print("yL_pred[0]:" + str(yL_pred[0]))
print("yL_value[0]:" + str(yL_value[0]))
# print("coef 53:" + str(regression_model.coef_))
# print("icept 54 :" + str(regression_model.intercept_))
arL = regression_model.intercept_ # 截距
brL = regression_model.coef_ # 迴歸係數
print("擬合參數:截距 arL", arL, ",迴歸係數bL:", brL)
######################### regression a
regression_model.fit(poly_x_values, ya_value)
ya_pred = regression_model.predict(poly_x_values)
f.write(str(ya_pred))
# print("ya_pred 67:" + str(ya_pred))
# print("coef 68:" + str(regression_model.coef_))
# print("icept 69 :" + str(regression_model.intercept_))
ara = regression_model.intercept_ # 截距
bra = regression_model.coef_ # 迴歸係數
print("擬合參數:截距 ara", ara, ",迴歸係數bra:", bra)
############################################################
regression_model.fit(poly_x_values, yb_value)
yb_pred = regression_model.predict(poly_x_values)
f.write(str(yb_pred))
# print("yb_pred 80:" + str(yb_pred))
# print("coef 81:" + str(regression_model.coef_))
# print("icept 82 :" + str(regression_model.intercept_))
arb = regression_model.intercept_ # 截距
brb = regression_model.coef_ # 迴歸係數
print("擬合參數:截距 arb", arb, ",迴歸係數bra:", brb)
#$TC=1262.183611+-51.299552*$TL+7.545716*$Ta+18.671053*$Tb+0.571956*pow($TL,2)+-0.140851*$TL*$Ta+-0.394538*$TL*$Tb+0.0153*pow($Ta,2)+0.058541*$Ta*$Tb+0.068508*pow($Tb,2);
if degree == 3 :
fLcalex = str(round(arL, 6)) + "+" + str(round(brL[1], 6)) + "*ml" + "+" + str(round(brL[2], 6)) + "*ma" + "+" + str(round(brL[3], 6)) + "*mb" + "+" + str(round(brL[4], 6)) + "*ml^2"+ "+" + str(round(brL[5], 6)) + "*ml*ma" + "+" + str(round(brL[6], 6)) + "*ml*mb"+ "+" + str(round(brL[7], 6)) + "*ma^2" + "+" + str(round(brL[8], 6)) + "*ma*mb"+ "+" + str(round(brL[9], 6)) + "*mb^2"
facalex = str(round(ara, 6)) + "+" + str(round(bra[1], 6)) + "*ml" + "+" + str(round(bra[2], 6)) + "*ma" + "+" + str(round(bra[3], 6)) + "*mb" + "+" + str(round(bra[4], 6)) + "*ml^2"+ "+" + str(round(bra[5], 6)) + "*ml*ma" + "+" + str(round(bra[6], 6)) + "*ml*mb"+ "+" + str(round(bra[7], 6)) + "*ma^2" + "+" + str(round(bra[8], 6)) + "*ma*mb"+ "+" + str(round(bra[9], 6)) + "*mb^2"
fbcalex = str(round(arb, 6)) + "+" + str(round(brb[1], 6)) + "*ml" + "+" + str(round(brb[2], 6)) + "*ma" + "+" + str(round(brb[3], 6)) + "*mb" + "+" + str(round(brb[4], 6)) + "*ml^2"+ "+" + str(round(brb[5], 6)) + "*ml*ma" + "+" + str(round(brb[6], 6)) + "*ml*mb"+ "+" + str(round(brb[7], 6)) + "*ma^2" + "+" + str(round(brb[8], 6)) + "*ma*mb"+ "+" + str(round(brb[9], 6)) + "*mb^2"
fLcalexn=str(round(regression_model.intercept_,6)) + "+" + str(round(regression_model.coef_[1],6)) + "*ml" + "+" + str(round(regression_model.coef_[2],6)) + "*ma" + "+" + str(round(regression_model.coef_[3],6)) + "*mb" + "+" + str(round(regression_model.coef_[4],6)) + "*ml^2" + "+" + str(round(regression_model.coef_[5],6)) + "*ml*ma" + "+" + str(round(regression_model.coef_[6],6)) + "*ml*mb" + "+" + str(round(regression_model.coef_[7],6)) + "*ma^2" + "+" + str(round(regression_model.coef_[8],6)) + "*ma*mb" + "+" + str(round(regression_model.coef_[9],6)) + "*mb^2" + "+" + str(round(regression_model.coef_[10],6)) + "*ml^3" + "+" + str(round(regression_model.coef_[11],6)) + "*ml^2*ma" + "+" + str(round(regression_model.coef_[12],6)) + "*ml^2*mb" + "+" + str(round(regression_model.coef_[13],6)) + "*ml*ma^2" + "+" + str(round(regression_model.coef_[14],6)) + "*ml*ma*mb" + "+" + str(round(regression_model.coef_[15],6)) + "*ml*mb^2"+ "+" + str(round(regression_model.coef_[16],6)) + "*ma^3"+ "+" + str(round(regression_model.coef_[17],6)) + "*ma^2*mb"+ "+" + str(round(regression_model.coef_[18],6)) + "*ma*mb^2"+ "+" + str(round(regression_model.coef_[19],6)) + "*mb^3"
elif degree == 2 :
fLcalex = str(round(arL, 6)) + "+" + str(round(brL[1], 6)) + "*ml" + "+" + str(round(brL[2], 6)) + "*ma" + "+" + str(round(brL[3], 6)) + "*mb" + "+" + str(round(brL[4], 6)) + "*ml^2"+ "+" + str(round(brL[5], 6)) + "*ml*ma" + "+" + str(round(brL[6], 6)) + "*ml*mb"+ "+" + str(round(brL[7], 6)) + "*ma^2" + "+" + str(round(brL[8], 6)) + "*ma*mb"+ "+" + str(round(brL[9], 6)) + "*mb^2"
facalex = str(round(ara, 6)) + "+" + str(round(bra[1], 6)) + "*ml" + "+" + str(round(bra[2], 6)) + "*ma" + "+" + str(round(bra[3], 6)) + "*mb" + "+" + str(round(bra[4], 6)) + "*ml^2"+ "+" + str(round(bra[5], 6)) + "*ml*ma" + "+" + str(round(bra[6], 6)) + "*ml*mb"+ "+" + str(round(bra[7], 6)) + "*ma^2" + "+" + str(round(bra[8], 6)) + "*ma*mb"+ "+" + str(round(bra[9], 6)) + "*mb^2"
fbcalex = str(round(arb, 6)) + "+" + str(round(brb[1], 6)) + "*ml" + "+" + str(round(brb[2], 6)) + "*ma" + "+" + str(round(brb[3], 6)) + "*mb" + "+" + str(round(brb[4], 6)) + "*ml^2"+ "+" + str(round(brb[5], 6)) + "*ml*ma" + "+" + str(round(brb[6], 6)) + "*ml*mb"+ "+" + str(round(brb[7], 6)) + "*ma^2" + "+" + str(round(brb[8], 6)) + "*ma*mb"+ "+" + str(round(brb[9], 6)) + "*mb^2"
fLcaleh = "$TC=" + str(round(arL, 6)) + "+" + str(round(brL[1], 6)) + "*$TL" + "+" + str(round(brL[2], 6)) + "*$Ta" + "+" + str(round(brL[3], 6)) + "*$Tb" + "+" + str(round(brL[4], 6)) + "*pow($TL,2)"+ "+" + str(round(brL[5], 6)) + "*$TL*$Ta" + "+" + str(round(brL[6], 6)) + "*$TL*$Tb"+ "+" + str(round(brL[7], 6)) + "*pow($Ta,2)" + "+" + str(round(brL[8], 6)) + "*$Ta*$Tb"+ "+" + str(round(brL[9], 6)) + "*pow($Tb,2)"
facaleh = "$TM=" + str(round(ara, 6)) + "+" + str(round(bra[1], 6)) + "*$TL" + "+" + str(round(bra[2], 6)) + "*$Ta" + "+" + str(round(bra[3], 6)) + "*$Tb" + "+" + str(round(bra[4], 6)) + "*pow($TL,2)"+ "+" + str(round(bra[5], 6)) + "*$TL*$Ta" + "+" + str(round(bra[6], 6)) + "*$TL*$Tb"+ "+" + str(round(bra[7], 6)) + "*pow($Ta,2)" + "+" + str(round(bra[8], 6)) + "*$Ta*$Tb"+ "+" + str(round(bra[9], 6)) + "*pow($Tb,2)"
fbcaleh = "$TY=" + str(round(arb, 6)) + "+" + str(round(brb[1], 6)) + "*$TL" + "+" + str(round(brb[2], 6)) + "*$Ta" + "+" + str(round(brb[3], 6)) + "*$Tb" + "+" + str(round(brb[4], 6)) + "*pow($TL,2)"+ "+" + str(round(brb[5], 6)) + "*$TL*$Ta" + "+" + str(round(brb[6], 6)) + "*$TL*$Tb"+ "+" + str(round(brb[7], 6)) + "*pow($Ta,2)" + "+" + str(round(brb[8], 6)) + "*$Ta*$Tb"+ "+" + str(round(brb[9], 6)) + "*pow($Tb,2)"
elif degree == 1 :
fLcalex = str(round(arL, 6)) + "+" + str(round(brL[1], 6)) + "*ml" + "+" + str(round(brL[2], 6)) + "*ma" + "+" + str(round(brL[3], 6)) + "*mb"
facalex = str(round(ara, 6)) + "+" + str(round(bra[1], 6)) + "*ml" + "+" + str(round(bra[2], 6)) + "*ma" + "+" + str(round(bra[3], 6)) + "*mb"
fbcalex = str(round(arb, 6)) + "+" + str(round(brb[1], 6)) + "*ml" + "+" + str(round(brb[2], 6)) + "*ma" + "+" + str(round(brb[3], 6)) + "*mb"
print("fLcalex :" + fLcalex)
print("aLcalex :" + facalex)
print("bLcalex :" + fbcalex)
fL.write("<?php function GrayML ($TL,$Ta,$Tb) {")
fL.write(fLcaleh)
fL.write(";\n")
fL.write(facaleh)
fL.write(";\n")
fL.write(fbcaleh)
fL.write(";\n")
fL.write("return [$TC, $TM, $TY];}"+"\n"+"?>")
fL.close()
deSum = 0
demax = 0
for i in range(0, 24):
globals()[f"de76{i}"] = pow((pow((yL_value[i]-yL_pred[i]),2)+pow((ya_value[i]-ya_pred[i]),2)+pow((yb_value[i]-yb_pred[i]),2)),0.5)
deSum = deSum + globals()[f"de76{i}"]
demax = max(demax,globals()[f"de76{i}"])
print("de76_"+str(i)+" :" + str(globals()[f"de76{i}"]))
print("deSumAvg :" + str(deSum/40))
print("demax :" + str(demax))
print("de761 :" + str(de761))
print("de762 :" + str(de762))
print("de763 :" + str(de763))
print("de764 :" + str(de764))
mL=51.3981
ma=40
mb=40
testdata = pd.read_csv("testTable.csv")
print(testdata.sample(n=3))
ttl = testdata['lt'].values
tml = testdata['lm'].values
tta = testdata['at'].values
tma = testdata['am'].values
ttb = testdata['bt'].values
tmb = testdata['bm'].values
print(str(ttl))
print(str(ttl[0]))
print(str(tml))
tpl1 = arL + brL[1] * tml[0] + brL[2] * tma[0] + brL[3] * tmb[0] + brL[4] * pow(tml[0] , 2) + brL[5] * tml[0] * tma[0] + brL[6] * tml[0] * tmb[0] + brL[7] * pow(tma[0], 2) + brL[8] * tma[0] * tmb[0] + brL[9] * pow(tmb[0], 2)
tpa1 = ara + bra[1] * tml[0] + bra[2] * tma[0] + bra[3] * tmb[0] + bra[4] * pow(tml[0] , 2) + bra[5] * tml[0] * tma[0] + bra[6] * tml[0] * tmb[0] + bra[7] * pow(tma[0], 2) + bra[8] * tma[0] * tmb[0] + bra[9] * pow(tmb[0], 2)
tpb1 = ara + brb[1] * tml[0] + brb[2] * tma[0] + brb[3] * tmb[0] + brb[4] * pow(tml[0] , 2) + brb[5] * tml[0] * tma[0] + brb[6] * tml[0] * tmb[0] + brb[7] * pow(tma[0], 2) + brb[8] * tma[0] * tmb[0] + brb[9] * pow(tmb[0], 2)
print("fLcal :" + str(tpl1))
print("facal :" + str(tpa1))
print("fbcal :" + str(tpb1))
for k in range(0, 3):
globals()[f"tpl{k}"] = arL + brL[1] * tml[k] + brL[2] * tma[k] + brL[3] * tmb[k] + brL[4] * pow(tml[k], 2) + brL[5] * tml[k] * tma[k] + brL[6] * tml[k] * tmb[k] + brL[7] * pow(tma[k], 2) + brL[8] * tma[k] * tmb[k] + brL[9] * pow(tmb[k], 2)
globals()[f"tpa{k}"] = ara + bra[1] * tml[k] + bra[2] * tma[k] + bra[3] * tmb[k] + bra[4] * pow(tml[k], 2) + bra[5] * tml[k] * tma[k] + bra[6] * tml[k] * tmb[k] + bra[7] * pow(tma[k], 2) + bra[8] * tma[k] * tmb[k] + bra[9] * pow(tmb[k], 2)
globals()[f"tpb{k}"] = arL + brb[1] * tml[k] + brb[2] * tma[k] + brb[3] * tmb[k] + brb[4] * pow(tml[k], 2) + brb[5] * tml[k] * tma[k] + brb[6] * tml[k] * tmb[k] + brb[7] * pow(tma[k], 2) + brb[8] * tma[k] * tmb[k] + brb[9] * pow(tmb[k], 2)
print("tpl0 :" + str(tpl0))
print("tpa0 :" + str(tpa0))
print("tpb0 :" + str(tpb0))
print("tpl1 :" + str(tpl1))
print("tpa1 :" + str(tpa1))
print("tpb1 :" + str(tpb1))
print("tpl2 :" + str(tpl2))
print("tpa2 :" + str(tpa2))
print("tpb2 :" + str(tpb2))
# print("tpl3 :" + str(tpl3))
# print("tpa3 :" + str(tpa3))
# print("tpb3 :" + str(tpb3))
deSum = 0
demax = 0
for i in range(0, 3):
globals()[f"pde76{i}"] = pow((pow((ttl[i]-globals()[f"tpl{i}"]),2)+pow((tta[i]-globals()[f"tpa{i}"]),2)+pow((ttb[i]-globals()[f"tpb{i}"]),2)),0.5)
deSum = deSum + globals()[f"pde76{i}"]
demax = max(demax,globals()[f"pde76{i}"])
print("pde76" + str(i) + " :" + str(globals()[f"pde76{i}"]))
print("deSum :" + str(deSum/3))
print("demax :" + str(demax))
print("de760 :" + str(pde760))
print("de761 :" + str(pde761))
############################
f.close()
fin = open("output.txt", "rt")
fout = open("out.txt", "wt")
for line in fin:
fout.write(line.replace('][', "\t"))
fin.close()
fout.close()
fin = open("out.txt", "rt")
fout = open("out2.txt", "wt")
for line in fin:
fout.write(line.replace('[', ""))
fin.close()
fout.close()
fin = open("out2.txt", "rt")
fout = open("out3.txt", "wt")
for line in fin:
fout.write(line.replace(']', ""))
fin.close()
fout.close()
#