in a pytext multi-class classifier, how do you know the order of the labels - pytext

I am using pytext for the first time. The training data has multiple labels. When running pytext predict, the result shows the word classifications in numerical label. How do you match the numbers to the original text labels?
The labels are like 'chronic disease', 'age'....
The prediction results are like 'prediction': tensor([[0, 0, 1, 0, 0, 0, 0, 1, 1, 1]])

Related

ggplot2/qplot ignores bins valued 0

So, I have something like this in R
f = factor(c(0,1,0,1,0), levels = c(0,1,2))
which, if plotted like
plot(f)
produces a nice chart showing that I have three 0, two 1, and zero 2. Nice. Until I use qplot, that is
qplot(f)
produces a supernice chart showing that I have three 0, and two 1. Not nice. I do need to retain that information about the third level. How do I do that?
Try this:
qplot(f) + scale_x_discrete(drop=FALSE)
Arguments:drop

How to manually create a dendrogram matrix for heatmap.2 in R?

Here is an example of my heatmap created by gplots::heatmap.2:
df<-data.frame(x1=rnorm(100),x2=rnorm(100,2,3),x3=rnorm(100,10,1),
y1=rnorm(100,5,1),y2=rnorm(100,20,5),y3=rnorm(100,30,2))
cor<-cor(df)
require(gplots)
heatmap.2(cor,trace="none",col=bluered)#fig1
heatmap.2(cor,Rowv=F,Colv=F,dendrogram="none",trace="none",col=bluered)#fig2
I would like create a dendrogram in fig2 where Xs are manually grouped together as well as Ys so that I can compare the correlation figures with fig1 where the dendrogram is computed using distance. Does anybody know how to make that?
You can create a dendrogram object by hand, but to me the question leaves it a bit unclear as to why this information is needed in the figure 2. If a dendrogram, that clusters all x's and y's together as separate groups, is added to the figure 2, the branches of the dendrogram will cross each other, and render the dendrogram (using real branch lenghts or heights) unreadable. If a dendrogram using some suitable heights is added, the dendrogram branch lengths do not have meaning (they do not correspond to the real correlation coefficients), but the dendrogram still tells the branching order of the tree.
Anyway, here's how to go about this technically (also, see Aniko's answer to a similar question, the details are available there).
An hclust object (a), that uses the "real" (I calculated them fast, so they might not be exactly correct, please use hierarchical clustering algorithm with average linkage to check, if needed) branch lenghts can be generated by hand:
a<-list()
a$merge<-matrix(c(-1, -2,
-4, -5,
-3, 1,
-6, 2,
3, 4), ncol=2, byrow=TRUE)
a$height<-c(0.1, 0.12, 0.12, 0.045, 0.1)
a$order<-1:6
a$labels<-c("x1", "x2", "x3", "y1", "y2", "y3")
class(a)<-"hclust"
Or by using just some suitable branch lenghts to even get a dendrogram:
b<-list()
b$merge<-matrix(c(-1, -2,
-4, -5,
-3, 1,
-6, 2,
3, 4), ncol=2, byrow=TRUE)
b$height<-c(0.1, 0.1, 0.1, 0.1, 0.2)
b$order<-1:6
b$labels<-c("x1", "x2", "x3", "y1", "y2", "y3")
class(b)<-"hclust"
After that, the final plot can be generated as:
heatmap.2(cor,Rowv=as.dendrogram(a),Colv=as.dendrogram(a),trace="none",col=bluered)#fig2
or:
heatmap.2(cor,Rowv=as.dendrogram(b),Colv=as.dendrogram(b),trace="none",col=bluered)#fig2

R: rescale labels

I have a plot in R with data on a range from 0-50 on the x axes.
If I use the default labeling, I get this tick labels: 0,10,20,30,40,50
As I know that these values are in reality divided by two, I would like to rescale the labels to: 0, 5, 10, 15, 20, 25.
Where the label now says 10, it should show 5 aso.
I tried different adjustments of:
axis(, at=c().. and ,labels())
but without any success.
As provied in the first comment, this works:
Plotting the data as
plot(XData/2, Ydata, ...)
works very well.

barplot two-sided data sets left and right of y axis

I am trying to figure out how to make barplot where I can show two data sets. Each at one side of the y-axis. I need the space for showing many data sets in few graphs. stacked or besides are other options but I would like to find out how to solve this task specially
I have started to play around a little
#creating data
names<-LETTERS[1:9]
data1<-c(8, 6, 3, 2, 0, 1, 1, 3, 1)
data2<-c(0, -1, 0, 0, 0, 0, 0, -2, -1)#negative to show them on the
#left side of yaxis
data1<-matrix(data1,ncol=9,nrow=1,byrow=F)
dimnames(data1)<-list(1,names)
data2<-matrix(data2,ncol=9,nrow=1,byrow=F)
dimnames(data2)<-list(1,names)
par(fig=c(0.5,1,0,1)) # making space for the "left" barplot
barplot(data1,horiz=T,axes=T,las=1)
par(fig=c(0.35,0.62,0,1), new=TRUE)#adjusting the "left" barplot
#because the labels would be negative
# use of axes=F
barplot(data2,axes=F,horiz=T,axisnames=FALSE)
#creating a new axis with desired labels
axis(side=1,at=seq(-8,0,2),labels=c(8,6,4,2,0))
But I have difficulties to understand the concept behind fig=c(...)
How can I ad a xaxis for my "left" barplot which has the same lenght i.e running from 0:8 like the other one
Thanks
Alex
As long as you know the axes before hand, this should work (adding in xlim argument).
I also edited your earlier code a bit, as how I think you want your output to look:
par(mfrow=c(1,2))
barplot(data2,axes=F,horiz=T,axisnames=FALSE,
xlim=c(-8,0))
#creating a new axis with desired labels
axis(side=1,at=seq(-8,0,2),labels=c(8,6,4,2,0))
barplot(data1,horiz=T,axes=T,las=1)

three axis graph in R

I have a question regarding generating a graph in R in a three dimension. Suppose, i have the following data in a csv format file;
CPU_Usage Power_Consumption(Watt) Bandwidth
50 59 20MB
Now i want to represent this on xyz axis where the x-axis represents cpu,y represents power & z represents bandwidth. Then i would want these values to be joined together (by a line) on the three axis graph to form a triangle.There is just a single row in this data. I would appreciate if someone could help me out!
You can accomplish this with scatterplot3d (among others):
library(scatterplot3d)
#first draw the lines of the triangle
#using type="l" Since we are drawing a
#shape, include the first point twice to
#close the polygon
q <- scatterplot3d(c(50, 0, 0, 50),
c(0, 59, 0, 0), c(0, 0, 20, 0),
xlim=c(0, 60), ylim=c(0, 60), zlim=c(0, 60), type="l",
xlab="CPU Usage", ylab="Power Consumption", zlab="Bandwidth",
box=FALSE)
#now add the points. scatterplot3d creates a list,
#one element of which is a function that operates
#on the existing chart, q, adding points:
q$points3d(c(50, 0, 0), c(0, 59, 0), c(0, 0, 20))
Of course, if you need to do more than one of these, you can pull the points from your data instead of hard-coding them. I thought hard-coding would make this a bit more readable.

Resources