温馨提示×

如何在Debian上使用Rust进行数据科学计算

小樊
38
2025-12-09 02:30:05
栏目: 编程语言

在 Debian 上使用 Rust 进行数据科学计算

一 环境准备

  • 安装基础工具与构建链:sudo apt-get update && sudo apt-get install -y curl build-essential。这些工具用于下载安装器与编译本地依赖。随后通过 rustup 安装 Rust 工具链:执行 curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh,完成后执行 source $HOME/.cargo/env,并用 rustc --version 验证安装。为加速国内依赖下载,可在 ~/.cargo/config.toml 配置镜像,例如清华源: [source.crates-io] replace-with = ‘tuna’ [source.tuna] registry = “https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git”。

二 常用库与用途

  • 数据处理与数值计算:ndarray(多维数组)、polars(高性能 DataFrame)、DataFusion(查询/引擎)、Serde(序列化)。
  • 机器学习与深度学习:linfa(通用 ML,类 scikit-learn)、smartcore(易用的传统算法)、tch-rs(PyTorch 绑定,支持 GPU)、candle(轻量框架,CPU/GPU)。
  • 数值与科学计算扩展:ndarray-stats(统计方法)、GSL 绑定(rust-gsl)(如需调用 GNU 科学库的数值例程)。

三 快速上手示例

  • 示例一 数据处理与统计(Polars + ndarray-stats)
    1. 新建项目:cargo new data_analysis && cd data_analysis

    2. 编辑 Cargo.toml: [dependencies] polars = “0.36” ndarray = “0.16” ndarray-stats = “0.5” rand = “0.8” ndarray-rand = “0.15” noisy_float = “0.2”

    3. 示例代码 src/main.rs(生成数据、计算均值与直方图): use ndarray::{Array2, Axis}; use ndarray_rand::{rand_distr::{StandardNormal, Uniform}, RandomExt}; use ndarray_stats::{HistogramExt, histogram::{strategies::Sqrt, GridBuilder}}; use noisy_float::types::{N64, n64}; use polars::prelude::*;

      fn main() -> Result<(), PolarsError> { // 1) Polars: 构建 DataFrame 并计算均值 let df = DataFrame::new(vec![ Series::new(“x”, &[1, 2, 3, 4, 5]), Series::new(“y”, &[2.0, 4.0, 6.0, 8.0, 10.0]), ])?; let mean_y: f64 = df.column(“y”)?.f64()?.mean().unwrap(); println!(“Mean of y: {}”, mean_y);

      // 2) ndarray + ndarray-stats: 生成数据并绘制直方图(文本)
      let data: Array2<f64> = Array2::random((10_000, 2), StandardNormal);
      let data_n64 = data.mapv(|x| n64(x));
      let grid = GridBuilder::<Sqrt<N64>>::from_array(&data_n64).unwrap().build();
      let hist = data_n64.histogram(grid);
      println!("Histogram counts: {:?}", hist.counts());
      Ok(())
      

      }

    4. 运行:cargo run

  • 示例二 机器学习(linfa 线性回归)
    1. 新建项目:cargo new ml_linreg && cd ml_linreg

    2. 编辑 Cargo.toml: [dependencies] linfa = “0.6” ndarray = “0.15”

    3. 示例代码 src/main.rs: use linfa::prelude::*; use ndarray::array;

      fn main() { let x = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0], [5.0, 6.0]]; let y = array![3.0, 5.0, 7.0, 9.0, 11.0]; let model = linfa::linear_regression::LinearRegression::default(); let fitted = model.fit(&x, &y).unwrap(); let preds = fitted.predict(&x); println!(“Predictions: {:?}”, preds); }

    4. 运行:cargo run。

四 性能优化与 GPU 加速

  • 构建与运行优化:使用 cargo build --release 启用全部优化;在 Cargo.toml 的 [profile.release] 中可开启 lto = true 进一步减小体积/提升性能;若关注体积可设 opt-level = “z”;对高分配场景可尝试 jemallocator 作为全局分配器;利用 rayon 并行化计算密集型循环与聚合。
  • GPU 与深度学习:选择 tch-rs(需配置 CUDA 环境)或 candle(支持 CPU/GPU,易于部署推理);如需调用传统数值例程,可引入 rust-gsl 并在 Debian 上安装系统库:sudo apt-get install libgsl-dev

五 部署与交付

  • 将 Rust 数据科学应用打包为 .deb:安装 cargo-deb(cargo install cargo-deb),在项目根目录执行 cargo deb 生成 target/debian/<项目名><版本>-1<架构>.deb;使用 sudo dpkg -i 安装,若依赖缺失执行 sudo apt-get install -f 修复。如需保留调试符号,可在 Cargo.toml 设置 [profile.release] debug = true 并使用 cargo deb --separate-debug-symbols。

0