<jupyter_start><jupyter_text>spline Regression(loop version)<jupyter_code>from scipy.interpolate import interp1d
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy import interpolate
#import smooth data sheet at 120 C (time:0~5000s)
address = 'data/degradation.xlsx'
df = pd.read_excel(address,sheet_name = 'smooth data',usecols = [0],names = None,nrows = 5000)
df_li_Time_smooth = df.values.tolist()
df = pd.read_excel(address,sheet_name = 'smooth data',usecols = [3],names = None,nrows = 5000)
df_li_L_smooth = df.values.tolist()
#plot the data
plt.figure()
plt.plot(df_li_Time_smooth,df_li_L_smooth,'-',color = 'gray')
plt.legend(['real result'])
plt.tick_params(axis='y', colors = 'white')
plt.tick_params(axis='x', colors = 'white')
plt.show()
#import normal data sheet at 120 C (time:0~5000s) 
address = 'data/degradation.xlsx'
x_normal = []
y_normal = []
df = pd.read_excel(address,sheet_name = 'normal data',usecols = [0],names = None,nrows = 5000)
df_li_Time = df.values.tolist()
df = pd.read_excel(address,sheet_name = 'normal data',usecols = [3],names = None,nrows = 5000)
df_li_L = df.values.tolist()      
# randomly select 7 points from normal data
x_normal = np.array(df_li_Time).T
y_normal = np.array(df_li_L).T 
x_normal = x_normal.reshape((5000))
y_normal = y_normal.reshape((5000))
x_random = []
y_random = []
x_random = x_normal[[0,99,999,1999,2999,3999,4999]]
y_random = y_normal[[0,99,999,1999,2999,3999,4999]]
#iterate 10 times with mins value point of each curvle
x_cycle = []
y_cycle = []
x_loop = x_random
y_loop = y_random
list_x_loop = x_loop.tolist()
list_y_loop = y_loop.tolist()
i=0
# can change i to control how many extra data point you would like
while i <6:
    #get the function of spline regression with known data points
    s = interpolate.InterpolatedUnivariateSpline(list_x_loop,list_y_loop)
    x_uni = np.arange(0,5000,1)
    y_uni = s(x_uni) # get all the y value  (0~5000) based on the regession function
    
    #plot figure
    plt.plot(df_li_Time_smooth,df_li_L_smooth,'-',color = 'gray')
    plt.plot(x_uni,y_uni,'-',color = 'red')
    plt.plot(list_x_loop,  list_y_loop,'x',color = 'black')
    plt.tick_params(axis='y', colors = 'white')
    plt.tick_params(axis='x', colors = 'white')
    plt.ylabel('Lifetime',color = 'white')
    plt.xlabel('Time',color = 'white')
    plt.title('cycle %d'%(i+1), color = 'white')
    plt.legend(['real result','univariateSpline','data point'])
    plt.show()
    
    ## iteration 
    list_y_uni = y_uni .tolist()
    list_y_uni_max_list = min(list_y_uni) #get the maxium value
    index = list_y_uni.index(min(list_y_uni)) # get the index of maxium value
    # add the min value of the function to the next loop list
    # extra layer of protection to stop equal x value to cause increasing error
    if index in x_cycle:
        index += 1
    x_cycle.append(x_normal[index]) 
    y_cycle.append(y_normal[index])
    # list_x_loop.extend(x_cycle)
    # list_y_loop.extend(y_cycle)
    list_x_loop.append(x_cycle.pop(0))
    list_y_loop.append(y_cycle.pop(0))
    # zip to sort both list
    list_loop =sorted(zip(list_x_loop,list_y_loop))
    # unzip(no really) and get each list from the zipped list
    list_x_loop = [x for x,y in list_loop]
    list_y_loop = [y for x,y in list_loop]
    i += 1
address = 'data/Data_SOLMAT.xlsx'
df = pd.read_excel(address,sheet_name = 'Trial',usecols = [0],names = None, nrows = 6)
df_li_Temp = df.values.tolist()
df = pd.read_excel(address,sheet_name = 'Trial',usecols = [1],names = None, nrows = 6)
df_li_s_HF = df.values.tolist()
X = np.array(df_li_Temp)
print(X)
y = np.array(df_li_s_HF).T
print(y)
y = y.reshape(6)
print(y)
rng = np.random.RandomState(0)
# First run
plt.figure()
kernel = 1.0 * RBF(length_scale=100.0, length_scale_bounds=(1e-2, 1e3)) \
    + WhiteKernel(noise_level=1, noise_level_bounds=(1e-10, 1e+1))
gp = GaussianProcessRegressor(kernel=kernel,alpha=0.0).fit(X, y)
X_ = np.linspace(0, 1, 100)
y_mean, y_cov = gp.predict(X_[:, np.newaxis], return_cov=True)
plt.plot(X_, y_mean, 'k', lw=3, zorder=9)
plt.fill_between(X_, y_mean - np.sqrt(np.diag(y_cov)),y_mean + np.sqrt(np.diag(y_cov)),alpha=0.5, color='k')
plt.scatter(X[:, 0], y, c='r', s=50, zorder=10, edgecolors=(0, 0, 0))
plt.title("Initial: %s\nOptimum: %s\nLog-Marginal-Likelihood: %s"
          % (kernel, gp.kernel_,
             gp.log_marginal_likelihood(gp.kernel_.theta)))
plt.tight_layout()<jupyter_output>[[0. ]
 [0.2]
 [0.4]
 [0.6]
 [0.8]
 [1. ]]
[[12.9998993  12.13923193 12.87751271 12.38016165 12.96285266 12.68707015]]
[12.9998993  12.13923193 12.87751271 12.38016165 12.96285266 12.68707015]
{'fraction_non_alphanumeric': 0.14845094664371772, 'fraction_numerical': 0.06002581755593804, 'mean_word_length': 4.669512195121952, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '249858', 'license': 'MIT', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1855, 'n_words': 391, 'path': 'Spline Regression/final spline regression loop version.ipynb', 'repo_name': 'michelleliu1027/Bayesian_PV', 'size': '4648'}