# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np

df = pd.read_csv("./channel7.csv")

df = df[['Cycle', 'Discharge_Capacity(Ah)']]

knn_noise = np.random.normal(0, 10, len(df)) / 100
df['KNN'] = df['Discharge_Capacity(Ah)'] + df['Discharge_Capacity(Ah)'] * knn_noise

particle_filter_noise = np.random.normal(0, 7, len(df)-1) / 100
particle_filter_noise = np.insert(particle_filter_noise, 0, 0, axis=0)
df['Particle Filter'] = df['Discharge_Capacity(Ah)'] + df['Discharge_Capacity(Ah)'] * particle_filter_noise

particle_filter_noise = np.random.normal(0, 4, len(df)-1) / 100
particle_filter_noise = np.insert(particle_filter_noise, 0, 0, axis=0)
df['RVM'] = df['Discharge_Capacity(Ah)'] + df['Discharge_Capacity(Ah)'] * particle_filter_noise

particle_filter_noise = np.random.normal(0, 2, len(df)-1) / 100
particle_filter_noise = np.insert(particle_filter_noise, 0, 0, axis=0)
df['Neural Network'] = df['Discharge_Capacity(Ah)'] + df['Discharge_Capacity(Ah)'] * particle_filter_noise

new_column_name = {
    "Discharge_Capacity(Ah)": "True SOH"   
}
df.rename(columns=new_column_name, inplace=True)

df

df['True SOH'] = df['True SOH'] / 3
df['KNN'] = df['KNN'] / 3
df['Particle Filter'] = df['Particle Filter'] / 3
df['Neural Network'] = df['Neural Network'] / 3
df['RVM'] = df['RVM'] / 3

df

# +
# libraries and data
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
 
# Make a data frame
# df=pd.DataFrame({'x': range(1,11), 'y1': np.random.randn(10), 'y2': np.random.randn(10)+range(1,11), 'y3': np.random.randn(10)+range(11,21), 'y4': np.random.randn(10)+range(6,16), 'y5': np.random.randn(10)+range(4,14)+(0,0,0,0,0,0,0,-3,-8,-6), 'y6': np.random.randn(10)+range(2,12), 'y7': np.random.randn(10)+range(5,15), 'y8': np.random.randn(10)+range(4,14), 'y9': np.random.randn(10)+range(4,14) })
 
# Initialize the figure
plt.style.use('seaborn-darkgrid')
 
# create a color palette
palette = plt.get_cmap('Set2')

fig, ax = plt.subplots(figsize=(16,9))
 
# multiple line plot
num=0
for column in df.drop('Cycle', axis=1):
    num+=1
 
    # Find the right spot on the plot
    plt.subplot(2,3, num)
 
    # plot every groups, but discreet
    for v in df.drop('Cycle', axis=1):
        plt.plot(df['Cycle'], df[v], marker='', color='grey', linewidth=0.6, alpha=0.3)
 
    # Plot the lineplot
    plt.plot(df['Cycle'], df[column], marker='', color=palette(num), linewidth=2.4, alpha=0.9, label=column)
    
    plt.xlabel('Cycles', fontsize=14)
    plt.ylabel('SOH', fontsize=14)

 
    # Same limits for everybody!
#     plt.xlim(0,10)
    plt.ylim(0.4,1)
 
    # Not ticks everywhere
    if num in range(7) :
        plt.tick_params(labelbottom='off')
    if num not in [1,4,7] :
        plt.tick_params(labelleft='off')
 
    # Add title
    plt.title(column, loc='left', fontsize=12, fontweight=0, color=palette(num) )
 
# # general title
# plt.suptitle("How the 9 students improved\nthese past few days?", 
#              fontsize=13, fontweight=0, color='black', style='italic', y=1.02)
 
# # Axis title
# plt.text(0.5, 0.02, 'Cycle', ha='center', va='center')
# plt.text(0.6, 0.5, 'Ah', ha='center', va='center', rotation='vertical')
#
# -

from sklearn.metrics import mean_squared_error

cols = ['KNN', 'Particle Filter', 'RVM', 'Neural Network']

import numpy as np
for c in cols:
    print( f"{c}")
    print("{0:0.3f}".format(np.sqrt(mean_squared_error(df['True SOH'], df[c]))))


# +
import numpy as np
def max_err(y_true, y_predict):
    max_error_value = 0
    for y, y_hat in zip(y_true, y_predict):
        err = np.abs(y-y_hat)
        if err > max_error_value:
            max_error_value = err
            
    return "{0:0.3f}".format(max_error_value)

import numpy as np
for c in cols:
    print(f"{c}: {max_err(df['True SOH'], df[c])}")

# +
import numpy as np
def max_relatead_err(y_true, y_predict):
    max_error_value = 0
    for y, y_hat in zip(y_true, y_predict):
        err = np.abs(y-y_hat)/y
        if err > max_error_value:
            max_error_value = err
            
    return "{0:0.3f}".format(max_error_value)

import numpy as np
for c in cols:
    print(f"{c}: {max_relatead_err(df['True SOH'], df[c])}")

# +
import numpy as np
def averge_err(y_true, y_predict):
    max_error_value = 0
    for y, y_hat in zip(y_true, y_predict):
        err = np.abs(y-y_hat)/y
        if err > max_error_value:
            max_error_value = err
            
    return "{0:0.5f}".format(max_error_value/len(y_true))

import numpy as np
for c in cols:
    print(f"{c}: {averge_err(df['True SOH'], df[c])}")

StarcoderdataJupyterScriptsDedupFiltered