Background:
Not sure if I have all my terminology right, so I apologize if this happens to be a duplicate question (similar question 1, similar question 2). I've been reading this tutorial How to Iterate Through a Dictionary in Python and I guess what I want to do is something along the lines of "Doing Some Calculations: Revisited", but in the form of "tuple unpacking" (words used in the 3rd link).
Problem/Goal:
What I was hoping for is to create a new dictionary with the original key, a new value that is the mean of the old value's list and plot it.
My Attempt:
Below is full attempt as a for loop and also my attempt at making a "one liner". The closest I got was forming two different variables that take on the dictionaries keys and another variable that takes on the values and plot them as a (x,y).
k_to_accuracies = {1: [0.274, 0.274, 0.274, 0.274, 0.274],
2: [0.224, 0.224, 0.224, 0.224, 0.224],
3: [0.272, 0.272, 0.272, 0.272, 0.272],
5: [0.278, 0.278, 0.278, 0.278, 0.278],
7: [0.274, 0.274, 0.274, 0.274, 0.274],
10: [0.282, 0.282, 0.282, 0.282, 0.282],
15: [0.272, 0.272, 0.272, 0.272, 0.272],
20: [0.272, 0.272, 0.272, 0.272, 0.272],
25: [0.274, 0.274, 0.274, 0.274, 0.274],
30: [0.254, 0.254, 0.254, 0.254, 0.254]}
k_ave = {}
for key, value in k_to_accuracies.items():
#print(key, '->', value)
k_ave[key] = np.mean(value)
print(k_ave)
k_ave = {}
k_ave = [np.mean(value) for value in k_to_accuracies.values()]
print("\n",k_ave)
k_keys = [key for key in k_to_accuracies.keys()]
print("\n",k_keys)
plt.plot(k_keys, k_ave, '.')
plt.show()
Questions
If possible how would I write this as one line or what is the most efficient/fastest way to do this.
Also would it be correct to call this a vectorized/broadcast calculation? If it is possible can someone explain how I would vectorize/broadcast these lines of code? (also not sure if this is correct terminology or even applicable in this scenario). I have yet to find a solid tutorial on these concepts besides the standard scipy tutorial and also tutorialspoint.