Packages utiles

Comme vu dans l'introduction JULIA possède une bibliotèque de package assez grande dont il n'est pas forcément aisé dans faire le tri...

Je vous propose ici la description et utilisation de quelques classiques

En premier lieu deux packages graphiques :

  • PyPlot
  • Gadfly

et deux packages plus "statistiques" :

  • DataFrmaes
  • Rdatasets

Graphiques

La gestion graphique ne fait pas pertie intégrante de JULIA il faut faire appel à des packages extérieurs et on plusieurs choix possible en voici quelques uns

PyPlot

PyPlot est basé sur une bibiliothèque éprouvée MatPlotLib (en Python). Sa syntaxe est identique à celle de MATLAB


In [1]:
using PyPlot # nécessite d'avoir déjà fait Pkg.add("PyPlot")


INFO: Loading help data...

In [21]:
x=linspace(0,pi,1000);
plot(x,sin(10*x),"r+",x,cos(10*x),"b^")
title("un titre")
xlabel(" en x ") #ylabel
xlim(-1,pi+1) #ylim
legend(["sinus","cosinus"])


Out[21]:
PyObject <matplotlib.legend.Legend object at 0x11e968750>

In [20]:
g=figure()
subplot(2,1,1)
plot(x,sin(x),"g",linewidth=2.0)
title("tracé 1")
subplot(2,1,2)
plot(x,x,"r")
grid(true)
title("tracé 2")


Out[20]:
PyObject <matplotlib.text.Text object at 0x11e8285d0>

In [23]:
n=1024
X=randn(n)
Y=randn(n)
scatter(X,Y)


Out[23]:
PyObject <matplotlib.collections.PathCollection object at 0x11ee23e50>

In [46]:
X=rand(5)
Y=-rand(5)
bar(1:5,X,facecolor="#9999ff", edgecolor="white")
bar(1:5,Y,facecolor="#ff9999", edgecolor="white")
for i=1:5
    txt=string(X[i])
    text(i+0.1,X[i]+0.01,txt[1:7])
    txt=string(Y[i])
    text(i+0.1,Y[i]-0.08,txt[1:7])
end
ylim(-1.2,1.2)


Out[46]:
(-1.2,1.2)

In [49]:
z=rand(20)
pie(z);



In [6]:
contour(rand(50,50))


Out[6]:
PyObject <matplotlib.contour.QuadContourSet instance at 0x11cf6e638>

In [67]:
surf(rand(50,50))


Out[67]:
PyObject <mpl_toolkits.mplot3d.art3d.Poly3DCollection object at 0x11fc76710>

In [59]:
X=[x for x=-1:0.2:1, y=-1:0.2:1]
Y=[y for x=-1:0.2:1, y=-1:0.2:1]
quiver(-1:0.2:1,-1:0.2:1,X,Y)


Out[59]:
PyObject <matplotlib.quiver.Quiver object at 0x12289d0d0>

Gadfly

Gadfly est basé sur la bibliothèque ggplot2 que l'on retrouve dans R.

L'utilisation de Gafly en même temps que PyPlot peut généré des conflits sur la commande plot qui devient Gadfly.plot ou alors en relançant un nouveau kernel...


In [1]:
using Gadfly

In [9]:
plot(sin,0,25)


Out[9]:
x -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 50 55 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 -25 0 25 50 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 -4 -2 0 2 4 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 f(x)

In [3]:
plot(x=randn(100), Geom.histogram(bincount=10))


Out[3]:
x -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 -9.0 -8.8 -8.6 -8.4 -8.2 -8.0 -7.8 -7.6 -7.4 -7.2 -7.0 -6.8 -6.6 -6.4 -6.2 -6.0 -5.8 -5.6 -5.4 -5.2 -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 8.2 8.4 8.6 8.8 9.0 -10 -5 0 5 10 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 50 55 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 -25 0 25 50 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50

In [11]:
plot(layer(sin,0,25),layer(cos,0,25))


The following aesthetics are required by Geom.point but are not defined: x, y


 in error at error.jl:21
 in assert_aesthetics_defined at /Users/clope/.julia/v0.3/Gadfly/src/aesthetics.jl:159
 in render at /Users/clope/.julia/v0.3/Gadfly/src/geom/point.jl:31
 in render at /Users/clope/.julia/v0.3/Gadfly/src/geometry.jl:47
 in render_prepared at /Users/clope/.julia/v0.3/Gadfly/src/Gadfly.jl:782
 in render at /Users/clope/.julia/v0.3/Gadfly/src/Gadfly.jl:721
 in writemime at /Users/clope/.julia/v0.3/Gadfly/src/Gadfly.jl:854
 in sprint at iostream.jl:229
 in display_dict at /Users/clope/.julia/v0.3/IJulia/src/execute_request.jl:29

In [14]:
plot(x=1:10, y=2.^rand(10),
     Scale.y_sqrt, Geom.point, Geom.smooth,
Guide.xlabel("X"), Guide.ylabel("Y"), Guide.title("Un titre"))


Out[14]:
X -12.5 -10.0 -7.5 -5.0 -2.5 0.0 2.5 5.0 7.5 10.0 12.5 15.0 17.5 20.0 22.5 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 -10 0 10 20 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 0.52 0.62 0.72 0.82 0.92 1.02 1.12 1.22 1.32 1.42 1.52 1.62 1.72 1.82 1.92 0.002 0.022 0.042 0.062 0.082 0.102 0.122 0.142 0.162 0.182 0.202 0.222 0.242 0.262 0.282 0.302 0.322 0.342 0.362 0.382 0.402 0.422 0.442 0.462 0.482 0.502 0.522 0.542 0.562 0.582 0.602 0.622 0.642 0.662 0.682 0.702 0.722 0.742 0.762 0.782 0.802 0.822 0.842 0.862 0.882 0.902 0.922 0.942 0.962 0.982 1.002 1.022 1.042 1.062 1.082 1.102 1.122 1.142 1.162 1.182 1.202 1.222 1.242 1.262 1.282 1.302 1.322 1.342 1.362 1.382 1.402 1.422 1.442 1.462 1.482 1.502 1.522 1.542 1.562 1.582 1.602 1.622 1.642 1.662 1.682 1.702 1.722 1.742 1.762 1.782 1.802 1.822 0.02 0.52 1.02 1.52 2.02 0.002 0.052 0.102 0.152 0.202 0.252 0.302 0.352 0.402 0.452 0.502 0.552 0.602 0.652 0.702 0.752 0.802 0.852 0.902 0.952 1.002 1.052 1.102 1.152 1.202 1.252 1.302 1.352 1.402 1.452 1.502 1.552 1.602 1.652 1.702 1.752 1.802 1.852 Y Un titre

In [12]:
plot(
                  layer(x=[1:2500]/100,y=sin([1:2500]/100),
                  Geom.line,Geom.point,
                  Theme(default_color=color("red"))),

                  layer(x=[1:2500]/100,y=cos([1:2500]/100),
                  Geom.line,
                  Theme(default_color=color("blue")))
    )


Out[12]:
x -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 50 55 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 -25 0 25 50 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 -4 -2 0 2 4 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 y

D'autres packages sont disponible Winston, AsciiPlot, GLVisualize...

DataFrames

Ce package permet de faire de la programmation en statistique.

Il introduit en autres :

  • Les valeurs manquantes NA
  • Le type DataArray (des Array qui peuvent contenir des NA)
  • Le type DataFrame

Type DataArray


In [19]:
using DataFrames

Création d'un DataArray incluant un NA,


In [21]:
myda = @data([NA, 1, 2, 3, 4])


Out[21]:
5-element DataArray{Int64,1}:
  NA
 1  
 2  
 3  
 4  

Pour pouvoir calculer une moyenne il faut régler le problèmes des NA en les enlevant :


In [18]:
dropna(myda) # on enlève les valeurs NA
mean(dropna(myda)) # on fait la moyenne


Out[18]:
2.5

ou en les remplaçant par une valeur (ici on choisit une valeur arbitrairement mais il existe des règles de statistiques quant au choix du remplacement du NA) :


In [23]:
myda = @data([NA, 1, 2, 3, 4])
mean(convert(Array,myda, 0))


Out[23]:
2.0

Les DataArray peuvent être vus comme des objets à N dimensions surlesquels on peut appliquer les mêmes opérations de base que sur les Array (package Base de JULIA) comme par exemple :


In [24]:
mydamult = @data([NA 0.0; 0.0 1.0])
mydamult * mydamult


Out[24]:
2x2 DataArray{Float64,2}:
 NA   NA
 NA  1.0

Type DataFrames

But : travailler avec des tables de données. Ce sont des tables dont les colonnes sont des DataArray. Voici deux façons de construire des DataFrames :


In [25]:
df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])


Out[25]:
AB
11M
22F
33F
44M

In [27]:
df = DataFrame()
df[:A] = 1:8;
df[:B] = ["M", "F", "F", "M", "F", "M", "M", "F"];
df


Out[27]:
AB
11M
22F
33F
44M
55F
66M
77M
88F

In [28]:
nrows = size(df, 1) # nombre de ligne
ncols = size(df, 2) # nombre de colonne


Out[28]:
2

In [34]:
head(df)#Voir le début du tableau


Out[34]:
AB
11M
22F
33F
44M
55F
66M

In [35]:
tail(df) #Voir la fin du tableau


Out[35]:
AB
13F
24M
35F
46M
57M
68F

In [36]:
df[1:3, :] #Voir les lignes 1,2,3 du tableau


Out[36]:
AB
11M
22F
33F

Nous avons l'analogie avec summarize du logiciel R :


In [37]:
describe(df)


A
Min      1.0
1st Qu.  2.75
Median   4.5
Mean     4.5
3rd Qu.  6.25
Max      8.0
NAs      0
NA%      0.0%

B
Length  8
Type    ASCIIString
NAs     0
NA%     0.0%
Unique  2


In [38]:
mean(df[:A])#Moyenne sur la colonne intitulée A (première colonne)


Out[38]:
4.5

In [39]:
median(df[:A])


Out[39]:
4.5

In [40]:
mean(df[1]) #Moyenne sur la première colonne


Out[40]:
4.5

Pour aller plus loin dans le travail statistique sur les Dataframes il nous faut le package RDatasets qui va fournir nombres exemples et on va y retrouver beaucoup de fonctionnalités communes au logiciel R.

RDatasets


In [44]:
using RDatasets, Gadfly

In [43]:
plot(dataset("datasets", "iris"), x="SepalLength", y="SepalWidth", Geom.point)


Out[43]:
SepalLength -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 8.2 8.4 8.6 8.8 9.0 9.2 9.4 9.6 9.8 10.0 10.2 10.4 10.6 10.8 11.0 11.2 11.4 11.6 11.8 12.0 0 5 10 15 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 -2.5 0.0 2.5 5.0 7.5 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 SepalWidth

In [45]:
plot(dataset("car", "SLID"), x="Wages", color="Language", Geom.histogram)


Out[45]:
Wages -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 -50 -48 -46 -44 -42 -40 -38 -36 -34 -32 -30 -28 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 -50 0 50 100 -50 -45 -40 -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 English Other French Language -250 -200 -150 -100 -50 0 50 100 150 200 250 300 350 400 450 -200 -190 -180 -170 -160 -150 -140 -130 -120 -110 -100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 -200 0 200 400 -200 -180 -160 -140 -120 -100 -80 -60 -40 -20 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400

In [ ]: