Modularity calculation for weighted graphs in igraph - igraph

I used the fastgreedy algorithm in igraph for my community detection in a weighted, undirected graph. Afterwards I wanted to have a look at the modularity and I got different values for different methods and I am wondering why. I included a short example, which demonstrates my problem:
library(igraph)
d<-matrix(c(1, 0.2, 0.3, 0.9, 0.9,
0.2, 1, 0.6, 0.4, 0.5,
0.3, 0.6, 1, 0.1, 0.8,
0.9, 0.4, 0.1, 1, 0.5,
0.9, 0.5, 0.8, 0.5, 1), byrow=T, nrow=5)
g<-graph.adjacency(d, weighted=T, mode="lower",diag=FALSE, add.colnames=NA)
fc<-fastgreedy.community(g)
fc$modularity[3]
#[1] -0.05011095
modularity(g,membership=cutat(fc,steps=2),weights=get.adjacency(g,attr="weight"))
#[1] 0.07193047
I would expect both of the values to be identical and if I try the same with an unweighted graph, I get the same values.
d2<-round(d,digits=0)
g2<- graph.adjacency(d2, weighted=NULL, mode="lower",diag=FALSE, add.colnames=NA)
fc2<-fastgreedy.community(g2)
plot(fc2,g2)
fc2$modularity[3]
#[1] 0.15625
modularity(g2,membership=cutat(fc2,steps=2))
#[1] 0.15625
Another user had a similar problem, but I have the current version of igraph, so that should not be the problem. Can someone explain to me why there is a difference or is there a problem with my code I don't see?

The line
modularity(g,membership=cutat(fc,steps=2),weights=get.adjacency(g,attr="weight"))
is wrong. If you want to pass the weights of edges to modularity(), do it with E(g)$weight:
modularity(g, membership = cutat(fc, steps = 2), weights = E(g)$weight)
# [1] -0.05011095

Related

MALLET - How to pass the csv file which contains word count to näive bayes in mallet?

I have created the CSV file which contains label name and word frequency.
e.g.
0, 4.0, 0.0, 0.0, 1.0, 0.0
0, 0.0, 1.0, 2.0, 0.0, 0.0
1, 1.0, 0.0, 0.0, 0.0, 3.0
Where the index zero represents the label (0 and 1)
My question is, How to import this kind CSV file into mallet to generate instance list? How to pass this file to Näive Bayes Classifier?
I found the answer to my own question.
In mallet, there are some pipes which create CSV to feature vector.
pipeList.add(new Csv2Array());
pipeList.add(new Target2Label());
pipeList.add(new Array2FeatureVector());
Output for above example:
0 and 1: It takes as target name.
for the first line:
1(1)=4.0
2(2)=0.0
3(3)=0.0
4(4)=1.0
5(5)=0.0
same for other two lines.

Finding Median in 13 rows in CSV

Ok So I have looked at a couple of different questions on here but I haven't been able to find anything to help to solve this problem. I split up 303 lines with 13 rows in them between healthy patients and sick patients. I was able to get the averages of both but now I need to get the median of those 2 averages (to make things clear this is what the output should look like).
Averages of Healthy Patients:
[52.59, 0.56, 2.79, 129.25, 242.64, 0.14, 0.84, 158.38, 0.14, 0.59, 1.41, 0.27, 3.77, 0.00]
Averages of Ill Patients:
[56.63, 0.82, 3.59, 134.57, 251.47, 0.16, 1.17, 139.26, 0.55, 1.57, 1.83, 1.13, 5.80, 2.04]
Seperation Values are:
[54.61, 0.69, 3.19, 131.91, 247.06, 0.15, 1.00, 148.82, 0.34, 1.08, 1.62, 0.70, 4.79, 1.02]
I have tried different methods for trying to get the median but I have failed on all my attempts so I've officially run out of ideas of how to get it. So if you can look and see if maybe I was on the right track and just missed something small or am completely way off I would appreciate any insight on this problem.
ill_avg = [ill / len(iList) for ill in iList_sum]
hlt_avg = [ hlt / len(hList) for hlt in hList_sum]
median = [(b / len(bList) for b in bList_sum) //2 ]
print('Total of lines Processed: ' + str(numline))
print("Total Healthy Count: " + str(HPcounter))
print("Total Ill Count: " + str(IPcounter))
print("Averages of Healthy Patients:")
print(str(hlt_avg))
print("Averages of Ill Patients ")
print('[' + ', '.join(['{:.2f}'.format(number) for number in ill_avg]) + ']')
print("Seperation Values are:")
print(median)
tried to get the median by adding both averages but I couldn't get it to work and my latest try was to makes a solo average(bList which is all patients) and get the median in that. If I can make the first way work without the bList I would prefer it that way since it will make the code less redundant and hopefully smaller.
I apologize I forgot to mention I am not suppose to use numpy or panda since we have not gone over those 2 in class yet.
Use numpy:
import numpy
a = numpy.array([[52.59, 0.56, 2.79, 129.25, 242.64, 0.14, 0.84, 158.38, 0.14, 0.59, 1.41, 0.27, 3.77, 0.00],
[56.63, 0.82, 3.59, 134.57, 251.47, 0.16, 1.17, 139.26, 0.55, 1.57, 1.83, 1.13, 5.80, 2.04]])
print numpy.mean(a, axis=0)
or use pure Python if you have to avoid numpy:
from __future__ import division
def mean(a):
return sum(a) / len(a)
a = [[52.59, 0.56, 2.79, 129.25, 242.64, 0.14, 0.84, 158.38, 0.14, 0.59, 1.41, 0.27, 3.77, 0.00],
[56.63, 0.82, 3.59, 134.57, 251.47, 0.16, 1.17, 139.26, 0.55, 1.57, 1.83, 1.13, 5.80, 2.04]]
print map(mean, zip(*a))

Maple Plot functions

does anybody know how to plot the Dirac function in top left corner and the sinus function given in the picture. I managed to plot the second and third easily. Would you do this with a piecewise function or is there an easy way for Plot #1 and #4 ? Thank you very much!
Arbitrary Excitations
If this is homework then I suppose that for the sinus example you are being asked to investigate the effects of scale and shift.
Consider these plots, one after another. Observe how they differ. First I scale the magnitude (y-direction) by 10. Then I scale in the x-direction. And finally I shift in the x-direction.
plots:-setoptions(size=[300,0.6],tickmarks=[decimalticks,default]):
plot( 10*sin( x ), x=0 .. Pi, view=[0..Pi, 0..18] );
plot( 10*sin( x*Pi ), x=0 .. 1, view=[0..1, 0..18] );
plot( 10*sin( x*Pi/0.3 ), x=0 .. 0.3, view=[0..1, 0..18] );
plot( 10*sin( (x-0.1)*Pi/0.3 ), x=0.1 .. 0.4, view=[0..1, 0..18] );
plots:-setoptions();
It can be made to look similar to the 4th plot in the image to which you linked quite easily. You can try it with and without the various options.
P:= plot( 10*sin( (x-0.1)*Pi/0.3 ), x=0.1 .. 0.4
, axes=none
, color=black
, size=[300,0.7]
, thickness=2
):
plots:-display( P
, view=[0.0 .. 0.6, 0..18]
, tickmarks=[[0.1,0.4],[10=10*N]]
, axes=normal, labels=[`t[s]`,`F(t)`]
, size=[300,0.6]
);
And with more effort it can be made a closer match, visually.
plots:-display( P
, plottools:-arrow([0,0],[0.6,0], 0.05, 0.9, 0.05)
, plottools:-arrow([0,0],[0,16], 0.001, 0.02, 0.08)
, plots:-textplot([0.6, -3.5, `t[s]`, font=["courier",16]])
, plots:-textplot([0.0, 18, `F(t)`, font=["courier",16]])
, seq(plots:-textplot([X, -1.5, X]), X=[0,0.1,0.4])
, plots:-textplot([-0.05, 10, "10 N"])
);
For your other plot you could simply plot a sequence of lines.
T:=table([1=60,2=100,3=0,4=20]):
P2:=seq(plottools:-line([i,0],[i,T[i]],thickness=2,color=black),
i=1..4):
And you could adjust the look and feel of the final plot as above. I'll leave that to you.
plots:-display(P2
, size=[300,0.7]
, view=[0..6, 0..120]
, labels=["",""]
);

Plotting a function as a function of another function in Mathematica

I wasn't entirely sure what to search for, so if this question has been asked before, I apologize in advance:
I have two functions
R := Rref*mx(mx^(4/3) - C0)^(-1)
M := Mref*(mx + C1*mx^(-1)*((1 - C0*mx^(-4/3))^(-3) - 1))
where Rref, Mref, C0 and C1 are constants and mx is the variable. I need to plot R as a function of M. Surely there must be something available in Mathematica to do such a plot - I just can't seem to find it.
The comment is correct, in that what you have is a set of two "parametric equations". You would use the ParametricPlot command. However, the syntax of functions with parameters is sometimes tricky, so let me give you my best recommendation:
R[Rref_, C0_, C1_][mx_] = Rref*mx (mx^(4/3) - C0)^(-1);
M[Mref_, C0_, C1_][mx_] = Mref*(mx + C1*mx^(-1)*((1 - C0*mx^(-4/3))^(-3) - 1));
I like that notation better because you can do things like derivatives:
R[Rref,C0,C1]'[mx]
(* Output: -((4 mx^(4/3) Rref)/(3 (-C0 + mx^(4/3))^2)) + Rref/(-C0 + mx^(4/3)) *)
Then you just plot the functions parametrically:
ParametricPlot[
{R[0.6, 0.3, 0.25][mx], M[0.2, 0.3, 0.25][mx]},
{mx, -10, 10},
PlotRange -> {{-10, 10}, {-10, 10}}
]
You can box this up in a Manipulate command to play with the parameters:
Manipulate[
ParametricPlot[
{R[Rref, C0, C1][mx], M[Mref, C0, C1][mx]},
{mx, -mmax, mmax},
PlotRange -> {{-10, 10}, {-10, 10}}
],
{Rref, 0, 1},
{Mref, 0, 1},
{C0, 0, 1},
{C1, 0, 1},
{mmax, 1, 10}
]
That should do it, I think.

How do I plot a function and data in Mathematica?

Simple question but I can't find the answer.
I want to combine a ListLinePlot and a regular Plot (of a function) onto one plot. How do I do this?
Thanks.
Use Show, e.g.
Show[Plot[x^2, {x, 0, 3.5}], ListPlot[{1, 4, 9}]]
Note, if plot options conflict Show uses the first plot's option, unless the option is specified in Show. I.e.
Show[Plot[x^2, {x, 0, 3.5}, ImageSize -> 100],
ListPlot[{1, 4, 9}, ImageSize -> 400]]
shows a combined plot of size 100.
Show[Plot[x^2, {x, 0, 3.5}, ImageSize -> 100],
ListPlot[{1, 4, 9}, ImageSize -> 400], ImageSize -> 300]
Shows a combined plot of size 300.
An alternative to using Show and combining two separate plots, is to use Epilog to add the data points to the main plot. For example:
data = Table[{i, Sin[i] + .1 RandomReal[]}, {i, 0, 10, .5}];
Plot[Sin[x], {x, 0, 10}, Epilog -> Point[data], PlotRange -> All]
or
Plot[Sin[x], {x, 0, 10}, Epilog -> Line[data], PlotRange -> All]