import pandas as pd
import numpy as np
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import plotly.graph_objects as go
import seaborn as sns
from pylab import *
def create_columns(data, params):
length = data.shape[1]
data_new = np.zeros((params,int(length/params)))
for i in range(params):
data_new[i]=data[0][i:length-params+i+1:params]
return data_new.T
data = pd.read_csv('TuningParameters.csv',header=None).to_numpy()
parameters = create_columns(data, 5)
df = pd.DataFrame(data=parameters,columns=['kp','ki','kd','tau_f','error'])
df.sort_values(ascending=True,by='error')
kp | ki | kd | tau_f | error | |
---|---|---|---|---|---|
870 | -0.047343 | -0.593767 | 0.003466 | 0.126500 | 1.478965e+02 |
467 | -0.047343 | -0.593767 | 0.003466 | 0.126500 | 1.551808e+02 |
2127 | -0.048320 | -0.577825 | 0.004069 | 0.501500 | 1.568579e+02 |
1350 | -0.039249 | -0.656267 | 0.003466 | 0.126500 | 1.573178e+02 |
2137 | -0.049541 | -0.593767 | 0.004137 | 0.142125 | 1.582779e+02 |
... | ... | ... | ... | ... | ... |
326 | -0.014377 | 0.406233 | 0.003466 | 0.126500 | 8.722700e+125 |
93 | 0.009469 | 0.432424 | -0.006251 | 0.170432 | 1.552122e+129 |
748 | 0.085751 | 0.331792 | 0.003466 | 0.126500 | 1.750783e+147 |
49 | 0.131027 | 0.464718 | -0.000965 | 0.381345 | 1.875552e+150 |
77 | -0.001503 | -0.117725 | -0.007754 | 0.073995 | inf |
2600 rows × 5 columns
sort(list(df['error'].unique()))
df[df > 1000] = 1000 #
import itertools
def param_grid(*lists):
return list(itertools.product(*lists))
grid = param_grid(np.linspace(df['kp'].min(),df['kp'].max(),20),np.linspace(df['ki'].min(),df['ki'].max(),20),np.linspace(df['kd'].min(),df['kd'].max(),20))
df_ave = pd.DataFrame(data=grid,columns=['kp','ki','kd'])
# Determine the bounds of the kp, ki, and kd data in df
kp_min, kp_max = df['kp'].min(), df['kp'].max()
ki_min, ki_max = df['ki'].min(), df['ki'].max()
kd_min, kd_max = df['kd'].min(), df['kd'].max()
# Create a linearly spaced array between the upper and lower bounds of the kp, ki, and kd data
kp_values = np.linspace(kp_min, kp_max, 50)
ki_values = np.linspace(ki_min, ki_max, 50)
kd_values = np.linspace(kd_min, kd_max, 50)
# Create a dataframe that contains all possible combinations of the linearly spaced array values
grid = pd.DataFrame({'kp': np.repeat(kp_values, len(ki_values) * len(kd_values)),
'ki': np.tile(np.repeat(ki_values, len(kd_values)), len(kp_values)),
'kd': np.tile(kd_values, len(kp_values) * len(ki_values))})
# Merge the grid dataframe with the error values from df
merged = pd.merge(df, grid, on=['kp', 'ki', 'kd'], how='right')
# Interpolate the error values for missing points in the merged dataframe
interpolated = merged.interpolate(method='linear', limit_direction='both')
# Merge the interpolated error values back into the original df_ave dataframe
df_ave = pd.merge(df_ave, interpolated[['kp', 'ki', 'kd', 'error']], on=['kp', 'ki', 'kd'], how='left')
# Fill in any missing error values with the mean error value from df
df_ave['error'] = df_ave['error'].fillna(df['error'].mean())
# Print the result
print(df_ave)
kp ki kd error 0 -0.049541 -0.687596 -0.009255 287.311433 1 -0.049541 -0.687596 -0.008242 287.311433 2 -0.049541 -0.687596 -0.007228 287.311433 3 -0.049541 -0.687596 -0.006215 287.311433 4 -0.049541 -0.687596 -0.005201 287.311433 ... ... ... ... ... 7995 0.150000 0.464718 0.005946 287.311433 7996 0.150000 0.464718 0.006960 287.311433 7997 0.150000 0.464718 0.007973 287.311433 7998 0.150000 0.464718 0.008987 287.311433 7999 0.150000 0.464718 0.010000 287.311433 [8000 rows x 4 columns]
import plotly.express as px
import plotly.io as pio
pio.renderers.default='notebook'
fig = px.scatter_3d(df, x='kp', y='ki', z='kd',color='error',
height=500,width=1000,color_continuous_scale=px.colors.sequential.Inferno_r).update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.show()