analysis-better.py v 1.0.9.000

changelog:
    - refactored
    - numpyed everything
    - removed stats in favor of numpy functions
This commit is contained in:
ltcptgeneral 2019-04-09 09:43:42 -05:00
parent 84483d36f2
commit 4bfb4ba238

View File

@ -14,6 +14,7 @@ __changelog__ = """changelog:
1.0.9.000: 1.0.9.000:
- refactored - refactored
- numpyed everything - numpyed everything
- removed stats in favor of numpy functions
1.0.8.005: 1.0.8.005:
- minor fixes - minor fixes
1.0.8.004: 1.0.8.004:
@ -160,7 +161,6 @@ import torch
class error(ValueError): class error(ValueError):
pass pass
def _init_device(setting, arg): # initiates computation device for ANNs def _init_device(setting, arg): # initiates computation device for ANNs
if setting == "cuda": if setting == "cuda":
try: try:
@ -177,11 +177,10 @@ def _init_device(setting, arg): # initiates computation device for ANNs
def load_csv(filepath): def load_csv(filepath):
with open(filepath, newline='') as csvfile: with open(filepath, newline='') as csvfile:
file_array = list(csv.reader(csvfile)) file_array = np.array(list(csv.reader(csvfile)))
csvfile.close() csvfile.close()
return file_array return file_array
# data=array, mode = ['1d':1d_basic_stats, 'column':c_basic_stats, 'row':r_basic_stats], arg for mode 1 or mode 2 for column or row # data=array, mode = ['1d':1d_basic_stats, 'column':c_basic_stats, 'row':r_basic_stats], arg for mode 1 or mode 2 for column or row
def basic_stats(data, method, arg): def basic_stats(data, method, arg):
@ -190,10 +189,7 @@ def basic_stats(data, method, arg):
if method == "1d" or method == 0: if method == "1d" or method == 0:
data_t = [] data_t = np.array(data).astype(float)
for i in range(0, len(data), 1):
data_t.append(float(data[i]))
_mean = mean(data_t) _mean = mean(data_t)
_median = median(data_t) _median = median(data_t)
@ -211,7 +207,7 @@ def basic_stats(data, method, arg):
_variance = None _variance = None
return _mean, _median, _mode, _stdev, _variance return _mean, _median, _mode, _stdev, _variance
"""
elif method == "column" or method == 1: elif method == "column" or method == 1:
c_data = [] c_data = []
@ -266,6 +262,7 @@ def basic_stats(data, method, arg):
else: else:
raise error("method error") raise error("method error")
"""
# returns z score with inputs of point, mean and standard deviation of spread # returns z score with inputs of point, mean and standard deviation of spread
@ -277,8 +274,8 @@ def z_score(point, mean, stdev):
# mode is either 'x' or 'y' or 'both' depending on the variable(s) to be normalized # mode is either 'x' or 'y' or 'both' depending on the variable(s) to be normalized
def z_normalize(x, y, mode): def z_normalize(x, y, mode):
x_norm = [] x_norm = np.array().astype(float)
y_norm = [] y_norm = np.array().astype(float)
mean = 0 mean = 0
stdev = 0 stdev = 0
@ -320,7 +317,7 @@ def z_normalize(x, y, mode):
# returns n-th percentile of spread given mean, standard deviation, lower z-score, and upper z-score # returns n-th percentile of spread given mean, standard deviation, lower z-score, and upper z-score
def stdev_z_split(mean, stdev, delta, low_bound, high_bound): def stdev_z_split(mean, stdev, delta, low_bound, high_bound):
z_split = [] z_split = np.array().astype(float)
i = low_bound i = low_bound
while True: while True:
@ -715,6 +712,27 @@ def generate_data(filename, x, y, low, high):
temp = temp + str(random.uniform(low, high)) temp = temp + str(random.uniform(low, high))
file.write(temp + "\n") file.write(temp + "\n")
def mean(data):
return np.mean(data)
def median(data):
return np.median(data)
def mode(data):
return np.argmax(np.bincount(data))
def stdev(data):
return np.std(data)
def variance(data):
return np.var(data)
"""
class StatisticsError(ValueError): class StatisticsError(ValueError):
pass pass
@ -856,8 +874,6 @@ def _fail_neg(values, errmsg='negative value'):
if x < 0: if x < 0:
raise StatisticsError(errmsg) raise StatisticsError(errmsg)
yield x yield x
def mean(data): def mean(data):
if iter(data) is data: if iter(data) is data:
@ -927,3 +943,4 @@ def stdev(data, xbar=None):
return var.sqrt() return var.sqrt()
except AttributeError: except AttributeError:
return math.sqrt(var) return math.sqrt(var)
"""