Apr-18-2021, 10:47 PM
(This post was last modified: Apr-21-2021, 10:35 AM by Larz60+.
Edit Reason: added problem text
)
Hi everyone, I've solved this problem #7 using pandas, but it specifically asks to do it by hand.
The solution should look like this : mean_attack_for_type(data) # {'water': 140.5, 'fire': 47.5}
Source: https://courses.cs.washington.edu/course...part0.html
----------------------------------------------
Output:Problem 7: mean_attack_per_type
Write a function called mean_attack_per_type that calculates the average attack for every type of Pokemon in the dataset. This function should return a dictionary that has keys that are the Pokemon types and values that are the average attack for that Pokemon type.
The order of the keys in the returned dictionary does not matter. In terms of efficiency, your solution should NOT iterate over the whole dataset once for each type of Pokemon since that would be overly inefficient.
For example, assuming we have parsed pokemon_test.csv and stored it in a variable called data:
mean_attack_for_type(data) # {'water': 140.5, 'fire': 47.5}
I'm getting stuck extracting the values I need to put it into groupby, and it gives me a type error that a tuple can't be converted into a numerator/denominator. If anyone can see a solution and guide me towards understanding it I would much appreciate it. The problem asks to group the pokemons into types, and then return the mean attack of the types. I've created a list called zipped that has the type, and attack of each pokemon. The solution should look like this : mean_attack_for_type(data) # {'water': 140.5, 'fire': 47.5}
Source: https://courses.cs.washington.edu/course...part0.html
----------------------------------------------
#Problem 7 - Mean attack per type from itertools import groupby def mean_attack_per_type(data): pokemon_type = [i['type'] for i in data] pokemon_attack = [i['atk'] for i in data] zipped = list(zip(pokemon_type, pokemon_attack)) zipped = sorted(zipped, reverse=True) print(zipped) grouped = {mean(attack) for pokemon, attack in groupby(zipped)} return(grouped)
mean_attack_per_type(data)
Output:[('water', 174), ('water', 107), ('fire', 50), ('fire', 45)]
Error:TypeError Traceback (most recent call last)
<ipython-input-180-139c559fd30d> in <module>
----> 1 mean_attack_per_type(data)
<ipython-input-179-ecccaa61dade> in mean_attack_per_type(data)
13
14
---> 15 grouped = {mean(attack) for pokemon, attack in groupby(zipped)}
16
17 return(grouped)
<ipython-input-179-ecccaa61dade> in <setcomp>(.0)
13
14
---> 15 grouped = {mean(attack) for pokemon, attack in groupby(zipped)}
16
17 return(grouped)
~\Anaconda3\lib\statistics.py in mean(data)
314 if n < 1:
315 raise StatisticsError('mean requires at least one data point')
--> 316 T, total, count = _sum(data)
317 assert count == n
318 return _convert(total/n, T)
~\Anaconda3\lib\statistics.py in _sum(data, start)
164 for typ, values in groupby(data, type):
165 T = _coerce(T, typ) # or raise TypeError
--> 166 for n,d in map(_exact_ratio, values):
167 count += 1
168 partials[d] = partials_get(d, 0) + n
~\Anaconda3\lib\statistics.py in _exact_ratio(x)
246 return (x, None)
247 msg = "can't convert type '{}' to numerator/denominator"
--> 248 raise TypeError(msg.format(type(x).__name__))
249
250
TypeError: can't convert type 'tuple' to numerator/denominator