import ee
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
import pandas as pd
from numpy import mean
from numpy import std
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedKFold
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
from google.colab import files
r2_score(y_test, y_pred_mars)
lst = np.array(range(0,95))
lst
y_test=pd.DataFrame(y_test)
y_test
test=y_test.set_index([lst])
test
k=np.array(x_test.index.values)
k
y_pred=pd.DataFrame(y_pred_mars)
y_pred.columns=['yPred']
y_pred
res=pd.concat([test,y_pred],axis=1)
res
ord = np.array(range(0,284))
inp=x_train.set_index([ord])
inp
fc=[]
D=[]
l=[]
t=[]
fy=[]
for i in range(0,284):
fc.append(inp.iat[i,0])
for i in range(0,284):
D.append(inp.iat[i,1])
for i in range(0,284):
l.append(inp.iat[i,2])
for i in range(0,284):
t.append(inp.iat[i,3])
for i in range(0,284):
fy.append(inp.iat[i,4])
y_train=pd.DataFrame(y_train)
y_train
x_test
xtest=x_test.set_index([lst])
xtest
result=[]
for i in range(0,284):
a=float(max([0,(D[i]-360)]))
b=float(max([0,(360-D[i])]))
c=float(max([0,(fc[i]-164.35)]))
d=float(max([0,(164.35-fc[i])]))
e=float(max([0,(fy[i]-843)]))
f=float(max([0,(843-fy[i])]))
g=float(max([0,(l[i]-551.9)]))
h=float(max([0,(551.9-l[i])]))
k=float(max([0,(D[i]-114.43)]))
m=float(max([0,(114.43-D[i])]))
out=16163.3+(1234.6a)-(45.7582 b)+(1354.1t[i])+(299.724a)-(2.3622bt[i])-(114.675d)+(47.573et[i])+(450.939ft[i])+(0.427781bd)-(0.00013788gft[i])-(1.25544D[i]ft[i])-(0.0130897D[i]bt[i])+(0.00142636kbd)-(0.000984524mbd)-(0.123574D[i]b)-(0.214354ebt[i])-(1.25315fbt[i])
result.append(out)
result=pd.DataFrame(result)
result.columns=['Predicted']
result
ord = np.array(range(0,284))
y_train=y_train.set_index([ord])
y_train
proph=pd.concat([y_train,result],axis=1)
proph
fc=[]
D=[]
l=[]
t=[]
fy=[]
for i in range(0,95):
fc.append(xtest.iat[i,0])
for i in range(0,95):
D.append(xtest.iat[i,1])
for i in range(0,95):
l.append(xtest.iat[i,2])
for i in range(0,95):
t.append(xtest.iat[i,3])
for i in range(0,95):
fy.append(xtest.iat[i,4])
prediction=[]
for i in range(0,95):
a=float(max([0,(D[i]-360)]))
b=float(max([0,(360-D[i])]))
c=float(max([0,(fc[i]-164.35)]))
d=float(max([0,(164.35-fc[i])]))
e=float(max([0,(fy[i]-843)]))
f=float(max([0,(843-fy[i])]))
g=float(max([0,(l[i]-551.9)]))
h=float(max([0,(551.9-l[i])]))
k=float(max([0,(D[i]-114.43)]))
m=float(max([0,(114.43-D[i])]))
out=16163.3+(1234.6a)-(45.7582 b)+(1354.1t[i])+(299.724a)-(2.3622bt[i])-(114.675d)+(47.573et[i])+(450.939ft[i])+(0.427781bd)-(0.00013788gft[i])-(1.25544D[i]ft[i])-(0.0130897D[i]bt[i])+(0.00142636kbd)-(0.000984524mbd)-(0.123574D[i]b)-(0.214354ebt[i])-(1.25315fbt[i])
prediction.append(out)
prediction=pd.DataFrame(prediction)
prediction.columns=['Predicted']
prediction
ord = np.array(range(0,95))
y_test=y_test.set_index([ord])
y_test
proph2=pd.concat([y_test,prediction],axis=1)
proph2
-- coding: utf-8 --
"""MARS.ipynb
Automatically generated by Colaboratory.
Original file is located at https://colab.research.google.com/drive/1f1ym9L21LPv8mCx_0UXKCi5FNe4Ax9YD """
!python --version
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
import ee import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.model_selection import train_test_split import pandas as pd from numpy import mean from numpy import std from sklearn.model_selection import cross_val_score from sklearn.model_selection import RepeatedKFold from sklearn.metrics import r2_score from sklearn.metrics import mean_squared_error from google.colab import files
Clone repo
!pip install git+https://github.com/scikit-learn-contrib/py-earth@v0.2dev !pip install scipy >= 0.16 !pip install scikit-learn !pip install six
Import model
import pyearth as Earth
uploaded = files.upload()
df=pd.read_excel(io.BytesIO(uploaded['datasetcorrect Copy.xlsx']))
df
x= pd.concat([df.drop(["Pexp"],axis=1)]) y=df["Pexp"] x.head() y.head() x.shape y.shape
sns.lmplot(x="fy",y="Pexp",data = df) plt.show
sns.lmplot(x="fc",y="Pexp",data = df) plt.show
sns.lmplot(x="D",y="Pexp",data = df) plt.show
sns.lmplot(x="L",y="Pexp",data = df) plt.show
sns.lmplot(x="t",y="Pexp",data = df) plt.show
46
define the model
model = Earth(max_terms=379, max_degree=3) x_train,x_test,y_train,y_test= train_test_split(x,y,test_size=0.25,random_state=46) y_train.head
define the evaluation procedure
for i in range (1,50): cv = RepeatedKFold(n_splits=5, n_repeats=20, random_state=i)
evaluate the model and collect results
n_scores = cross_val_score(model, x_train, y_train, scoring='r2', cv=cv, n_jobs=-1)
report performance
print('%d R2: %.3f (%.3f)' % (i,mean(n_scores), std(n_scores)))
for i in range(1,10): x_train,x_test,y_train,y_test= train_test_split(x,y,test_size=0.25,random_state=i) y_train.head
define the evaluation procedure
for j in range (1,5): cv = RepeatedKFold(n_splits=5, n_repeats=20, random_state=j)
evaluate the model and collect results
define the evaluation procedure
cv = RepeatedKFold(n_splits=5, n_repeats=20, random_state=34)
evaluate the model and collect results
n_scores = cross_val_score(model, x_train, y_train, scoring='r2', cv=cv, n_jobs=-1)
report performance
print('R2: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
"""MARS_model = Earth(max_terms=379, max_degree=3)
MARS_model_fitted = MARS_model.fit(x_train, y_train)
y_pred_mars = MARS_model_fitted.predict(x_test)
r2_score(y_test, y_pred_mars) y_test """
MARS_model = Earth(max_terms=379, max_degree=3) MARS_model_fitted = MARS_model.fit(x_train, y_train)
y_pred_mars = MARS_model_fitted.predict(x_test)
r2_score(y_test, y_pred_mars) lst = np.array(range(0,95)) lst y_test=pd.DataFrame(y_test) y_test test=y_test.set_index([lst]) test
k=np.array(x_test.index.values)
k
y_pred=pd.DataFrame(y_pred_mars) y_pred.columns=['yPred'] y_pred res=pd.concat([test,y_pred],axis=1) res ord = np.array(range(0,284)) inp=x_train.set_index([ord]) inp fc=[] D=[] l=[] t=[] fy=[] for i in range(0,284): fc.append(inp.iat[i,0]) for i in range(0,284): D.append(inp.iat[i,1]) for i in range(0,284): l.append(inp.iat[i,2]) for i in range(0,284): t.append(inp.iat[i,3]) for i in range(0,284): fy.append(inp.iat[i,4]) y_train=pd.DataFrame(y_train) y_train x_test xtest=x_test.set_index([lst]) xtest
sns.lmplot(x="Pexp",y="yPred",data = res) plt.show
corr_matrix = df.corr() sns.heatmap(corr_matrix, annot=True) plt.show()
"""print(MARS_model_fitted.summary())"""
print(MARS_model_fitted.summary())
result=[] for i in range(0,284): a=float(max([0,(D[i]-360)])) b=float(max([0,(360-D[i])])) c=float(max([0,(fc[i]-164.35)])) d=float(max([0,(164.35-fc[i])])) e=float(max([0,(fy[i]-843)])) f=float(max([0,(843-fy[i])])) g=float(max([0,(l[i]-551.9)])) h=float(max([0,(551.9-l[i])])) k=float(max([0,(D[i]-114.43)])) m=float(max([0,(114.43-D[i])])) out=16163.3+(1234.6a)-(45.7582 b)+(1354.1t[i])+(299.724a)-(2.3622bt[i])-(114.675d)+(47.573et[i])+(450.939ft[i])+(0.427781bd)-(0.00013788gft[i])-(1.25544D[i]ft[i])-(0.0130897D[i]bt[i])+(0.00142636kbd)-(0.000984524mbd)-(0.123574D[i]b)-(0.214354ebt[i])-(1.25315fbt[i]) result.append(out)
result=pd.DataFrame(result) result.columns=['Predicted'] result ord = np.array(range(0,284)) y_train=y_train.set_index([ord]) y_train proph=pd.concat([y_train,result],axis=1) proph
fc=[] D=[] l=[] t=[] fy=[] for i in range(0,95): fc.append(xtest.iat[i,0]) for i in range(0,95): D.append(xtest.iat[i,1]) for i in range(0,95): l.append(xtest.iat[i,2]) for i in range(0,95): t.append(xtest.iat[i,3]) for i in range(0,95): fy.append(xtest.iat[i,4]) prediction=[] for i in range(0,95): a=float(max([0,(D[i]-360)])) b=float(max([0,(360-D[i])])) c=float(max([0,(fc[i]-164.35)])) d=float(max([0,(164.35-fc[i])])) e=float(max([0,(fy[i]-843)])) f=float(max([0,(843-fy[i])])) g=float(max([0,(l[i]-551.9)])) h=float(max([0,(551.9-l[i])])) k=float(max([0,(D[i]-114.43)])) m=float(max([0,(114.43-D[i])])) out=16163.3+(1234.6a)-(45.7582 b)+(1354.1t[i])+(299.724a)-(2.3622bt[i])-(114.675d)+(47.573et[i])+(450.939ft[i])+(0.427781bd)-(0.00013788gft[i])-(1.25544D[i]ft[i])-(0.0130897D[i]bt[i])+(0.00142636kbd)-(0.000984524mbd)-(0.123574D[i]b)-(0.214354ebt[i])-(1.25315fbt[i]) prediction.append(out) prediction=pd.DataFrame(prediction) prediction.columns=['Predicted'] prediction ord = np.array(range(0,95)) y_test=y_test.set_index([ord]) y_test proph2=pd.concat([y_test,prediction],axis=1) proph2
MARS_model = Earth(max_terms=379, max_degree=3) MARS_model_fitted = MARS_model.fit(x_train, y_train)
y_pred_mars = MARS_model_fitted.predict(x_test)
r2_score(y_test, y_pred_mars)