Can't calculate mean!


#1

Hey fellows,

somehow, my code wont calculate the mean of my data.

That is my raw-data:
31

Here is my Code:

And there my Output:
[Running] python -u “/Users/anwender/Desktop/Machine Learning Projects/Project_1_CustomerLoyaltyPrediction/Project_1_ML_Code.py”
[[‘1,00’ ‘2,80’ ‘3,75’]
[‘2,00’ ‘5,00’ ‘4,00’]
[‘3,00’ ‘3,60’ ‘2,50’]
[‘4,00’ ‘5,00’ ‘4,25’]
[‘5,00’ ‘3,80’ ‘3,75’]
[‘6,00’ ‘5,00’ ‘4,63’]
[‘7,00’ ‘4,60’ ‘3,75’]
[‘8,00’ ‘5,00’ ‘4,88’]
[‘9,00’ ‘2,20’ ‘1,75’]
[‘10,00’ ‘2,80’ ‘3,00’]
[‘11,00’ ‘4,40’ ‘2,50’]
[‘12,00’ ‘3,60’ ‘3,88’]
[‘13,00’ ‘4,00’ ‘4,00’]
[‘14,00’ ‘4,40’ ‘3,88’]
[‘15,00’ ‘4,00’ ‘3,88’]
[‘16,00’ ‘5,00’ ‘4,50’]
[‘17,00’ ‘4,00’ ‘3,50’]
[‘18,00’ ‘2,80’ ‘3,13’]
[‘19,00’ ‘2,60’ ‘2,00’]
[‘20,00’ ‘5,00’ ‘2,75’]]
[‘2,80’ ‘5,00’ ‘3,60’ ‘5,00’ ‘3,80’ ‘5,00’ ‘4,60’ ‘5,00’ ‘2,20’ ‘2,80’
‘4,40’ ‘3,60’ ‘4,00’ ‘4,40’ ‘4,00’ ‘5,00’ ‘4,00’ ‘2,80’ ‘2,60’ ‘5,00’]
[‘3,75’ ‘4,00’ ‘2,50’ ‘4,25’ ‘3,75’ ‘4,63’ ‘3,75’ ‘4,88’ ‘1,75’ ‘3,00’
‘2,50’ ‘3,88’ ‘4,00’ ‘3,88’ ‘3,88’ ‘4,50’ ‘3,50’ ‘3,13’ ‘2,00’ ‘2,75’]
Traceback (most recent call last):
File “/Users/anwender/Desktop/Machine Learning Projects/Project_1_CustomerLoyaltyPrediction/Project_1_ML_Code.py”, line 16, in
cs_mean = np.mean(cs_data)
File “/Users/anwender/anaconda3/lib/python3.6/site-packages/numpy/core/fromnumeric.py”, line 2957, in mean
out=out, **kwargs)
File “/Users/anwender/anaconda3/lib/python3.6/site-packages/numpy/core/_methods.py”, line 82, in _mean
ret = ret / rcount
TypeError: unsupported operand type(s) for /: ‘str’ and 'int’

[Done] exited with code=1 in 0.777 seconds

–> Can please someone explain my mistake (TypeError).

Thank you so much!

Regards!


Please Help!
#2

I’ve never really used numpy, but I don’t think you can use an array of arrays of strings to calculate with numpy.mean(). Try some floats! Also, you should use points (".") instead of commas (",") for floating point numbers in Python.


#3

How would you import the data and calculate the mean of each column ?
Because i tried everything with numpy - but it just wont work :frowning:

EDIT:
And what do you mean by saying: "Use “.” not “,” - are you referring to the data-set in excel ?


#4

Ok, here’s some data:
data_calc
The output would be (if you set the column separator char to semicolon(";")(which according to your code you did(sep = ";")):

3,03;5,2;1;5.2
0,92;3,00;6;4.47
5,20;1,97;3;0.12

The same data with commas(",") as separators would be:

"3,03","5,2",1,5.2
"0,92","3,00",6,4.47
"5,20","1,97",3,0.12

The information above was just extra stuff that may be nice to know if needed.

Your data seems to use commas as decimal separators. Python uses dots as decimal separators. Problems may arise(pandas.read_csv()'s default for decimal separator is the dot).

According to this link, you can use decimal="," when calling pandas.read_csv() to continue using your preferred decimal separator in your project.

:slight_smile:


#5

You are indeed awesome!

Thank you so much - you don’t know how much time i spend on solving this problem.

Have a great day!