Q&A
実現したいこと
Rustの機械学習で分類器を作りたいです。
以下の記事を参考にしていますが、csvをDataFrameに返すところでエラーが起きてつまづいています。
https://dev.classmethod.jp/articles/smartcore-palmer
該当部のコード
Rust
1//CSVファイルを読み込んでDataFrameを返す 2fn read_csv_with_schema<P: AsRef<Path>>(path: P) -> PolarResult<DataFrame> { 3 let schema = Schema::new(vec![ 4 Field::new("species", DataType::Utf8), 5 Field::new("island", DataType::Utf8), 6 Field::new("culmen_length_mm", DataType::Float64), 7 Field::new("culmen_depth_mm", DataType::Float64), 8 Field::new("flipper_length_mm", DataType::Float64), 9 Field::new("body_mass_g", DataType::Float64), 10 Field::new("sex", DataType::Utf8) 11 ]); 12 let file = File::open(path).expect("Cannot open file."); 13 CsvReader::new(file) 14 .with_schema(Arc::new(schema)) 15 .has_header(true) 16 .with_ignore_parser_errors(true) //エラーが出ても処理継続 17 .finish() 18}
エラー
rust
1 Compiling smartcore-cnn321 v0.1.0 (/Users/meme/smartcore-cnn321) 2error[E0061]: this function takes 0 arguments but 1 argument was supplied 3 --> src/main.rs:22:18 4 | 522 | let schema = Schema::new(vec![ 6 | __________________^^^^^^^^^^^_- 7 | | | 8 | | expected 0 arguments 923 | | Field::new("species", DataType::Utf8), 1024 | | Field::new("island", DataType::Utf8), 1125 | | Field::new("culmen_length_mm", DataType::Float64), 12... | 1329 | | Field::new("sex", DataType::Utf8) 1430 | | ]); 15 | |_____- supplied 1 argument 16 | 17note: associated function defined here 18 --> /Users/meme/.cargo/registry/src/github.com-1ecc6299db9ec823/polars-core-0.20.0/src/schema.rs:53:12 19 | 2053 | pub fn new() -> Self { 21 | ^^^ 22 23error[E0308]: mismatched types 24 --> src/main.rs:33:22 25 | 2633 | .with_schema(Arc::new(schema)) 27 | ^^^^^^^^^^^^^^^^ 28 | | 29 | expected `&polars::prelude::Schema`, found struct `std::sync::Arc` 30 | help: consider borrowing here: `&Arc::new(schema)` 31 | 32 = note: expected reference `&polars::prelude::Schema` 33 found struct `std::sync::Arc<polars::prelude::Schema>` 34 35error[E0277]: `&str` is not an iterator 36 --> src/main.rs:47:28 37 | 3847 | let target = df.select("species"); 39 | ------ ^^^^^^^^^ `&str` is not an iterator; try calling `.chars()` or `.bytes()` 40 | | 41 | required by a bound introduced by this call 42 | 43 = help: the trait `Iterator` is not implemented for `&str` 44 = note: required because of the requirements on the impl of `IntoIterator` for `&str` 45note: required by a bound in `DataFrame::select` 46 --> /Users/meme/.cargo/registry/src/github.com-1ecc6299db9ec823/polars-core-0.20.0/src/frame/mod.rs:1235:12 47 | 481235 | I: IntoIterator<Item = S>, 49 | ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `DataFrame::select` 50 51Some errors have detailed explanations: E0061, E0277, E0308. 52For more information about an error, try `rustc --explain E0061`. 53error: could not compile `smartcore-cnn321` due to 3 previous errors
心当たり
polarsのバージョンが0.20.0に変更しなければいけなかった
Rust
1polars = "0.14.7" 2polars-core = {version = "0.14.7", features=["ndarray"]}
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/03/31 07:37