I am trying to write code for the Zachary karate club dataset. Now I am stuck at the line
y_true_val = list(y_true.values())
My code:
nmi_results = []
ars_results = []
y_true_val = list(y_true.values())
# Append the results into lists
for y_pred in results:
nmi_results.append(normalized_mutual_info_score(y_true_val, y_pred))
ars_results.append(adjusted_rand_score(y_true_val, y_pred))
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, sharey=True, figsize=(16, 5))
x = np.arange(len(y_pred))
avg = [sum(x) / 2 for x in zip(nmi_results, ars_results)]
xlabels = list(algorithms.keys())
sns.barplot(x, nmi_results, palette='Blues', ax=ax1)
sns.barplot(x, ars_results, palette='Reds', ax=ax2)
sns.barplot(x, avg, palette='Greens', ax=ax3)
ax1.set_ylabel('NMI Score')
ax2.set_ylabel('ARS Score')
ax3.set_ylabel('Average Score')
# # Add the xlabels to the chart
ax1.set_xticklabels(xlabels)
ax2.set_xticklabels(xlabels)
ax3.set_xticklabels(xlabels)
# Add the actual value on top of each bar
for i, v in enumerate(zip(nmi_results, ars_results, avg)):
ax1.text(i - 0.1, v[0] + 0.01, str(round(v[0], 2)))
ax2.text(i - 0.1, v[1] + 0.01, str(round(v[1], 2)))
ax3.text(i - 0.1, v[2] + 0.01, str(round(v[2], 2)))
# Show the final plot
plt.show()
Output:
y_true_val = list(y_true.values())
AttributeError: 'list' object has no attribute 'values'
Hope this helps!
First let's learn about the error:
As mentioned in the comments attribute
error happens when you try to access a method that is not defined in that object's call methods.
For example, in py string objects we have .lower()
available to generate a new string in lowercase(From ABC
to abc
). If you try to access this .lower()
for an integer object you will receive Attribute error.
How to fix this error?
Use python inbuilt type operator to determine what kind of datatype/object you have. If it's a list then you don't have to do any .values()
print(type(y_true))
Use python inbuilt dir operator to know which are operators available for this object. If you see the values
operator here, only then apply .values()
operation.
print(dir(y_true))
Still not sure, or ran into some other error try to print(y_true)
to see what you have.