Im trying to get the mean, mode, standard deviation and median of a set of data from a text file. It works for all but the mode, when I include the mode I get the error of '
TypeError: unsupported format string passed to ModeResult.__format__
'.
It goes when I comment out the print format line for the mode so I know thats whats wrong but I dont know why its wrong
What I have tried:
import numpy as np
from scipy import stats
#---------------------------------ColorText-----------------------------------#
def ColorText( text, color):
"""
Description: This function allows colors to be used in output formatting
"""
CEND = '\033[0m'
CRED = '\033[1;31m'
if color == 'red':
return CRED + text + CEND
#------------------------------------------------------------------------------
data = np.loadtxt("sunspots.txt",float)
x = data[:,0]
y = data[:,1]
xyr = 1749 + (x/12)
title1= 'Mean'
title2= 'Median'
title3= 'Standard Deviation'
title4= 'Mode'
print(ColorText('\n''{0:>18}'.format(title1), 'red'),'|',\
ColorText('{0:>18}'.format(title2), 'red'), '|',\
ColorText('{0:>18}'.format(title3), 'red'),'|',\
ColorText('{0:>18}'.format(title4),'red'))
mean = np.mean(y)
#print('The mean of the data sunspots is',mean)
median= np.median(y)
#print('The median of the data sunspots is', median)
standard_dev= np.std(y)
#print('The standard deviation of the data sunspots is',standard_dev)
mode= stats.mode(y)
print('{0:>18.3f}'.format(mean), '|',\
'{0:>18.3f}'.format(median), '|',\
'{0:>18.3f}'.format(standard_dev),'|',\
'{0:>18.3f}'.format(mode),'|')