t - table and degrees of freedom I am interested if someone can review this process and give me some tips. (I don't have any data science coworkers or friends to collaborate with...) When the script (.py file) below runs, it will output/print:

The total savings are 9793 kWh + - 192 kWh
The total savings are 9793 kWh + - 2.0 %


Ultimately I am attempting calculate "precision" with my results (+ - 192 kWh AND + - 2.0 %) ..

For starters, I cant find this info online so here's a snip from a book I am referencing on calculating data set standard error, then absolute precision, relative precision.



For retrieving my critical t value, I am following these steps from machinelearningmastery.com. This is where the scipy.stats import t package is required.

I cant remember where degrees of freedom comes into play. The machinelearningmastery example, the author uses df = 10 So until I can figure out this, I left that as a default value in my code below. If someone wanted to copy and paste the code below it should run...

I cant remember from college where degrees of freedom comes into play and how to utilize it. The length of the data when its converted into df2 Pandas dataframe is 31, which represents one months data 31 days. Any tips greatly appreciated..

import os
import numpy as np
import pandas as pd
import math
from scipy.stats import t

#actual electrical energy in kWh
actual = 40000

#calculated savings in kWh, output from Keras regression
data = [np.array([[1083.8748]], dtype='float32'), np.array([[998.98773]], dtype='float32'), np.array([[1137.0487]], dtype='float32'), np.array([[1077.2798]], dtype='float32'), np.array([[926.41284]], dtype='float32'),
np.array([[1030.7125]], dtype='float32'), np.array([[1028.0048]], dtype='float32'), np.array([[523.9799]], dtype='float32'), np.array([[1125.092]], dtype='float32'), np.array([[1119.7738]], dtype='float32'),
np.array([[918.6966]], dtype='float32'), np.array([[1112.5186]], dtype='float32'), np.array([[555.6942]], dtype='float32'), np.array([[1096.5643]], dtype='float32'), np.array([[826.35657]], dtype='float32'),
np.array([[1014.35406]], dtype='float32'), np.array([[1027.6962]], dtype='float32'), np.array([[924.20087]], dtype='float32'), np.array([[1035.217]], dtype='float32'), np.array([[1008.9658]], dtype='float32'),
np.array([[970.54047]], dtype='float32'), np.array([[847.0671]], dtype='float32'), np.array([[913.5519]], dtype='float32'), np.array([[1047.0747]], dtype='float32'), np.array([[920.0606]], dtype='float32'),
np.array([[994.2266]], dtype='float32'), np.array([[991.4501]], dtype='float32'), np.array([[972.43256]], dtype='float32'), np.array([[934.8802]], dtype='float32'), np.array([[912.04004]], dtype='float32'), np.array([[1131.297]], dtype='float32')]

#convert data to pandas series then DataFrame
df = pd.Series(data)
df2 = pd.DataFrame(df)

#define sum, standard deviation, mean of calculated savings data
total = df2.sum()
totalStd = np.std(df2.values)
totalMean = df2.mean()

#calculate saings
diff = int(actual - total)

#compute precision of results, starting with standard error
stdErr = totalStd / math.sqrt(len(total.index))

# define probability &amp; degrees of freedom
p = 0.90
degf = 10

# retrieve t value &lt;= probability
t = t.ppf(p, degf)

#calculate absolute precision
absPrec = t * stdErr 

#calculate relative precision
relPrec = (100 * (absPrec / diff)).round(decimals=1)


#print results
absSavings = f'The total savings are {int(diff)} kWh + - {int(absPrec)} kWh'
print(absSavings)

relSavings = f'The total savings are {int(diff)} kWh + - {float(relPrec)} %'
print(relSavings)
See this post on machinelearningmastery to use scipy to find critical t value. This code below seemed to work.

from scipy.stats import t

#savings calc
diff = actualKwh - total

#compute precision of results, starting with standard error
stdErr = totalStd / math.sqrt(len(total.index))


# define probability &amp; degrees of freedom
p = 0.95
degf = len(data.index) - 1

#print("dfLen=", len(data.index))

#print("degf=", degf)


# retrieve t value
t = t.ppf(p, degf)
#print("t =", t)


absPrec = t * stdErr 
#print("absPrec =", int(absPrec))


relPrec = (100 * (absPrec / diff)).round(decimals=1)
#print("relPrec =", relPrec)