Pakete verwalten
Julia hat einen eingebauten Paketmanager (Pkg). Pakete liegen in der General-Registry (juliahub.com). Jedes Projekt hat eigene Abhängigkeiten in Project.toml und Manifest.toml:
# In der REPL: ] drücken, dann
# pkg> activate . Projektumgebung aktivieren
# pkg> add Plots DataFrames CSV
# pkg> status
# pkg> update
# pkg> test
# Oder im Code:
# using Pkg; Pkg.add("DataFrames")| Gebiet | Beliebte Pakete |
|---|---|
| Daten | DataFrames.jl, CSV.jl, Arrow.jl, JSON3.jl, Query.jl |
| Grafik | Plots.jl, Makie.jl (GPU), StatsPlots.jl, Gadfly.jl |
| Statistik | Statistics (Standard), StatsBase.jl, Distributions.jl, HypothesisTests.jl, GLM.jl |
| Wissenschaft | DifferentialEquations.jl, Optim.jl, JuMP.jl, Unitful.jl, Symbolics.jl |
| Machine Learning | Flux.jl, Lux.jl, MLJ.jl, Turing.jl (Bayes), Zygote.jl |
| Web/Apps | HTTP.jl, Genie.jl, Pluto.jl (reaktive Notebooks) |
| Python-Brücke | PythonCall.jl, PyCall.jl; ccall für C-Bibliotheken |
using DataFrames, CSV, Statistics, Plots
df = CSV.read("messwerte.csv", DataFrame)
first(df, 5)
describe(df)
gruppiert = combine(groupby(df, :stadt), :temperatur => mean => :mittel, nrow => :anzahl)
sort!(gruppiert, :mittel, rev = true)
plot(df.tag, df.temperatur, label = "Temperatur", xlabel = "Tag", lw = 2)
scatter!(df.tag, df.temperatur, label = "")
savefig("temperatur.png")Projekt 1: Mini-Statistikprogramm
using Statistics
daten = [23.5, 25.1, 22.8, 30.2, 27.4, 24.9, 26.3, 28.8]
function bericht(x)
return (
n = length(x),
mittel = round(mean(x), digits = 2),
median = median(x),
abw = round(std(x), digits = 2),
spanne = extrema(x),
quartile = quantile(x, [0.25, 0.5, 0.75]) .|> q -> round(q, digits = 2),
)
end
b = bericht(daten)
for (name, wert) in pairs(b)
println(rpad(string(name), 9), wert)
end
# Korrelation und Regression
x = collect(1.0:8.0)
println("Korrelation: ", round(cor(x, daten), digits = 3))
steigung = cov(x, daten) / var(x)
achsenabschnitt = mean(daten) - steigung * mean(x)
println("Trend: y = ", round(achsenabschnitt, digits = 2), " + ", round(steigung, digits = 3), "x")Ausgabe
n 8 mittel 26.13 median 25.700000000000003 abw 2.56 spanne (22.8, 30.2) quartile [24.55, 25.7, 27.75] Korrelation: 0.531 Trend: y = 23.63 + 0.555x
Projekt 2: Spiel des Lebens
function schritt(G)
n, m = size(G)
neu = falses(n, m)
for i in 1:n, j in 1:m
nachbarn = 0
for di in -1:1, dj in -1:1
(di == 0 && dj == 0) && continue
ii, jj = i + di, j + dj
if 1 <= ii <= n && 1 <= jj <= m && G[ii, jj]
nachbarn += 1
end
end
neu[i, j] = G[i, j] ? nachbarn in (2, 3) : nachbarn == 3
end
return neu
end
zeige(G) = foreach(i -> println(join(G[i, :] .|> b -> b ? '#' : '.')), 1:size(G, 1))
function simuliere(generationen)
G = falses(6, 6)
for (i, j) in [(1, 2), (2, 3), (3, 1), (3, 2), (3, 3)] # ein Gleiter
G[i, j] = true
end
for t in 0:generationen
println("Generation $t:")
zeige(G)
G = schritt(G)
end
end
simuliere(2)Ausgabe
Generation 0: .#.... ..#... ###... ...... ...... ...... Generation 1: ...... #.#... .##... .#.... ...... ...... Generation 2: ...... ..#... #.#... .##... ...... ......
Projekt 3: Monte-Carlo und Zufall
using Random, Statistics
rng = MersenneTwister(2025) # reproduzierbarer Zufallsgenerator
function pi_schaetzung(n, rng)
treffer = 0
for _ in 1:n
x, y = rand(rng), rand(rng)
treffer += (x^2 + y^2 <= 1)
end
return 4treffer / n
end
schaetzung = pi_schaetzung(200_000, rng)
println(abs(schaetzung - π) < 0.02 ? "π ≈ 3.14 (Fehler < 0.02)" : "zu ungenau")
würfe = rand(rng, 1:6, 10_000)
println(length(würfe), " ", sort(unique(würfe)), " ", abs(mean(würfe) - 3.5) < 0.1)
println(abs(mean(randn(rng, 100_000))) < 0.02)
println(length(shuffle(MersenneTwister(1), 1:5)), " ", length(randperm(rng, 10)), " ", all(1 .<= rand(rng, 1:100, 50) .<= 100))Ausgabe
π ≈ 3.14 (Fehler < 0.02) 10000 [1, 2, 3, 4, 5, 6] true true 5 10 true
Projekt 4: Text analysieren
text = """
Julia ist eine Sprache für wissenschaftliches Rechnen. Julia ist schnell,
und Julia ist einfach. Wissenschaft braucht schnelle Sprachen.
"""
woerter = split(lowercase(replace(text, r"[^\p{L}\s]" => "")))
println(length(woerter), " Wörter, ", length(unique(woerter)), " verschiedene")
haeufigkeit = Dict{String, Int}()
for w in woerter
haeufigkeit[w] = get(haeufigkeit, w, 0) + 1
end
top = sort(collect(haeufigkeit), by = p -> (-p.second, p.first))[1:4]
for (w, n) in top
println(rpad(w, 10), "█"^n, " ", n)
end
println(maximum(length, woerter), " ", argmax(length.(woerter)), " ", woerter[argmax(length.(woerter))])Ausgabe
18 Wörter, 14 verschiedene ist ███ 3 julia ███ 3 braucht █ 1 eine █ 1 18 6 wissenschaftliches
Merke
Pkgverwaltet Pakete pro Projekt (Project.toml,Manifest.toml)- Ökosystem: DataFrames/CSV, Plots/Makie, DifferentialEquations, Flux, JuMP, Pluto
- Reproduzierbarer Zufall mit
MersenneTwister(seed) - Julia eignet sich für Simulationen, Statistik und Machine Learning
- Interop mit Python (
PythonCall.jl) und C (ccall)
Aufgabe
Simuliere 10.000 Würfe mit zwei Würfeln und zähle, wie oft die Summe 7 vorkommt.