dterg / confused-pie-plots Goto Github PK
View Code? Open in Web Editor NEWAlternative visualization to confusion matrices for hierarchical classification.
License: GNU General Public License v3.0
Alternative visualization to confusion matrices for hierarchical classification.
License: GNU General Public License v3.0
Hi,
Thank you for this alternative to a confusion matrix i think its great! I have implemented the code for a confusion matrix generate which contains only one row and column of class (age)..
I am getting a slightly odd plot and was wondering if you could point me in the right direction for correcting it.
The issues:
figure not plotting class name in the middle of the pie-plot. Would like to remove the titles of each plot and simply add them to the middle of the plot (similar to p60, but with the right class name [p3,p15 etc...]
And the for the full legend to plot! I am unsure as to where the error is in my code! any help would be much appreciated. thanks!
code
confMat = np.array(fullConfMat.ix[1:,1:])
ageNames = fullConfMat[0][1:].tolist()
nAges = len(ageNames)
nCols = np.shape(ageNames)[0]
nRows = 1
idx = []
parentDict = dict((el,0) for el in ageNames)
for iParent, parent in enumerate(ageNames):
for i, j in enumerate(ageNames):
if j == parent:
idx.append(i)
parentDict[parent] = idx
iUpperLvlClasses = [ageNames[i] for i in idx]
iLevelnClasses = np.shape(np.unique(iUpperLvlClasses))[0]
idx = []
if iLevelnClasses > nRows:
nRows = iLevelnClasses
legendRowsNeeded = int(np.ceil(float(nAges) / float(nCols)))
totalRows = nRows + legendRowsNeeded
heightRatios = []
[heightRatios.append(3) for x in range(0,nRows)]
[heightRatios.append(1) for x in range(0,legendRowsNeeded)]
the_grid = GridSpec(totalRows, nCols, height_ratios=heightRatios)
nColors = nAges
cmap = plt.cm.gist_ncar
colors = cmap(np.linspace(0.,1.,nColors))
fig = plt.figure(facecolor='white')
ax = fig.gca()
iCounter = 0
speciesCounter = 0
iRow = totalRows - legendRowsNeeded
for iParent, parent in enumerate(ageNames):
childrenIdx = parentDict[parent]
for jChild, child in enumerate(childrenIdx):
speciesCounter += 1
spName = ageNames[child]
plt.subplot(the_grid[jChild,iParent], aspect=1)
if jChild == 0:
plt.text(-1, 1.2, parent[0:9]+'.', fontsize=10)
sliceSize = confMat[child,]
if confMat[child, child] == 100:
predictedSlices = plt.pie([100],
colors = colors[[child,]],
shadow=False,
startangle=90,
radius=1)
else:
predictedSlices = plt.pie(sliceSize, # data
colors=colors, # array of colours
shadow=False, # disable shadow
startangle=90, # starting angle
radius=1)
for wedge in predictedSlices[0]:
wedge.set_linewidth(0.1)
actualSlices = plt.pie([100],
colors=colors[[child,]],
shadow=False,
startangle=90,
radius=0.4)
Abv = parent[0]
AbvName = Abv + '. ' + spName
# check if color is dark
totalColor = 0.299 * colors[child, 0] + 0.587 * colors[child, 1] + 0.114 * colors[child, 2]
if totalColor > 0.3:
plt.text(-0.2, -0.1, AbvName[0]+AbvName[3], color='black', fontsize=10)
else:
plt.text(-0.2, -0.1, AbvName[0] + AbvName[3], color='white', fontsize=10)
# draw legend
if iCounter > nCols-1:
iCounter = 1
iRow += 1
plt.subplot(the_grid[iRow, iCounter], aspect = 2)
legendDots = plt.pie([100],
colors=colors[[child, ]],
shadow=False,
startangle=90,
radius=0.8)
plt.text(1.2,-0.1,AbvName, fontsize=10) ### 4.5 adjust depending on resolution
iCounter += 0
mng = plt.get_current_fig_manager()
if you need further clarification or code please let me know! please note I have left some of the wording from the original code (such as parent etc) in the code for loops just for ease, but the relevant parts have been changed.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.