0

I'm wondering if you can me figure out how to assign value to string in np array, and convert to int, then calculate weighted average. For example,

test_np = array(['HARD', 'HARD', 'EASY', 'MODERATE', 'MODERATE', 'EASY', 'MODERATE', 'MODERATE', 'EASY', 'MODERATE', 'MODERATE', 'HARD', 'EASY'])

where 'HARD'=10, 'MODERATE'=5, and 'EASY'=1 so the results should be something like this so I can calculate weighted average with np.average()

test_np = array([10, 10, 1, 5, 5, 1, 5, 5, 1, 5, 5, 10, 1])

Thanks so much in advance!

Mr. T
  • 11,960
  • 10
  • 32
  • 54
january28
  • 3
  • 2
  • Use `HARD`,`EASY` and `MODERATE` as variables instead of strings. – Space Impact Sep 26 '18 at 05:23
  • 'HARD', 'EASY', 'MODERATE' are given strings from the table so I'm looking for way to convert them to numbers so that I can make further calculation – january28 Sep 26 '18 at 05:25
  • Possible duplicate of [Fast replacement of values in a numpy array](https://stackoverflow.com/questions/3403973/fast-replacement-of-values-in-a-numpy-array) – Mr. T Sep 26 '18 at 05:28

2 Answers2

0

You can convert your test_np to a pandas series, and use map to convert your strings to integers. Then you could take the average of that series

Input:

df = pd.Series(test_np)
test_dict = { 'HARD':10, 'MODERATE':5,'EASY':1}
df = df.map(test_dict)
df.mean()

Output for df:

0     10
1     10
2      1
3      5
4      5
5      1
6      5
7      5
8      1
9      5
10     5
11    10
12     1
dtype: int64

Output for average:

4.923076923076923
Joe Patten
  • 1,664
  • 1
  • 9
  • 15
0

A simple dictionary with list comprehension solution:

In [45]: test_np = np.array(['HARD', 'HARD', 'EASY', 'MODERATE', 'MODERATE', 'EA
    ...: SY', 'MODERATE', 'MODERATE', 'EASY', 'MODERATE', 'MODERATE', 'HARD', 'E
    ...: ASY'])
In [46]: 
In [46]: adict = {'HARD':10, 'MODERATE':5, 'EASY':1}
In [47]: np.array([adict[i] for i in test_np])
Out[47]: array([10, 10,  1,  5,  5,  1,  5,  5,  1,  5,  5, 10,  1])
hpaulj
  • 221,503
  • 14
  • 230
  • 353