File: C:/xampp/htdocs/FograCT1/machineLearningGrayCC.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("grayCMY50.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['C'].values
ya_value = dfPro3['M'].values
yb_value = dfPro3['Y'].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/25))
print("demax :" + str(demax))
print("de761 :" + str(de761))
print("de762 :" + str(de762))
print("de763 :" + str(de763))
print("de764 :" + str(de764))
tL=58
ta=0.23
tb=0.64
mL=58.84
ma=1.15
mb=-2.73
tC = arL + brL[1] * tL + brL[2] * ta + brL[3] * tb + brL[4] * pow(tL, 2) + brL[5] * tL * ta + brL[6] * tL * tb + brL[7] * pow(ta, 2) + brL[8] * ta * tb + brL[9] * pow(tb, 2)
tM = ara + bra[1] * tL + bra[2] * ta + bra[3] * tb + bra[4] * pow(tL, 2) + bra[5] * tL * ta + bra[6] * tL * tb + bra[7] * pow(ta, 2) + bra[8] * ta * tb + bra[9] * pow(tb, 2)
tY = arb + brb[1] * tL + brb[2] * ta + brb[3] * tb + brb[4] * pow(tL, 2) + brb[5] * tL * ta + brb[6] * tL * tb + brb[7] * pow(tb, 2) + brb[8] * ta * tb + brb[9] * pow(tb, 2)
print("fLcal :" + str(tC))
print("facal :" + str(tM))
print("fbcal :" + str(tY))
mC = arL + brL[1] * mL + brL[2] * ma + brL[3] * mb + brL[4] * pow(mL, 2) + brL[5] * mL * ma + brL[6] * mL * mb + brL[7] * pow(ma, 2) + brL[8] * ma * mb + brL[9] * pow(mb, 2)
mM = ara + bra[1] * mL + bra[2] * ma + bra[3] * mb + bra[4] * pow(mL, 2) + bra[5] * mL * ma + bra[6] * mL * mb + bra[7] * pow(ma, 2) + bra[8] * ma * mb + bra[9] * pow(mb, 2)
mY = arb + brb[1] * mL + brb[2] * ma + brb[3] * mb + brb[4] * pow(mL, 2) + brb[5] * mL * ma + brb[6] * mL * mb + brb[7] * pow(mb, 2) + brb[8] * ma * mb + brb[9] * pow(mb, 2)
print("fLcal :" + str(mC))
print("facal :" + str(mM))
print("fbcal :" + str(mY))
############################
#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()
########################################################
dfCR30 = pd.read_csv("grayLab127.csv")
dfPro3 = pd.read_csv("grayCMY50.csv")
X_valueR = dfPro3[['C', 'M', 'Y']].values
yL_valueR = dfCR30['L'].values
ya_valueR = dfCR30['a'].values
yb_valueR= dfCR30['b'].values
poly_model = PolynomialFeatures(degree=2)
poly_x_valuesR = poly_model.fit_transform(X_valueR)
regression_model = LinearRegression()
regression_model.fit(poly_x_valuesR, yL_valueR)
yL_predR = regression_model.predict(poly_x_valuesR)
f.write(str(yL_predR))
print("yL_pred 52:" + str(yL_predR))
print("yL_pred[0]:" + str(yL_predR[0]))
print("yL_value[0]:" + str(yL_valueR[0]))
# print("coef 53:" + str(regression_model.coef_))
# print("icept 54 :" + str(regression_model.intercept_))
arLR = regression_model.intercept_ # 截距
brLR = regression_model.coef_ # 迴歸係數
print("擬合參數:截距 arL", arLR, ",迴歸係數bL:", brLR)
######################### regression a
regression_model.fit(poly_x_valuesR, ya_valueR)
ya_predR = regression_model.predict(poly_x_valuesR)
f.write(str(ya_predR))
# print("ya_pred 67:" + str(ya_pred))
# print("coef 68:" + str(regression_model.coef_))
# print("icept 69 :" + str(regression_model.intercept_))
araR = regression_model.intercept_ # 截距
braR = regression_model.coef_ # 迴歸係數
print("擬合參數:截距 ara", araR, ",迴歸係數bra:", braR)
############################################################
regression_model.fit(poly_x_valuesR, yb_value)
yb_predR = regression_model.predict(poly_x_valuesR)
f.write(str(yb_predR))
# print("yb_pred 80:" + str(yb_pred))
# print("coef 81:" + str(regression_model.coef_))
# print("icept 82 :" + str(regression_model.intercept_))
arbR = regression_model.intercept_ # 截距
brbR = regression_model.coef_ # 迴歸係數
print("擬合參數:截距 arb", arbR, ",迴歸係數bra:", brbR)
#