質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Rust

Rustは、MoFoが支援するプログラミング言語。高速性を維持しつつも、メモリ管理を安全に行うことが可能な言語です。同じコンパイル言語であるC言語やC++では困難だったマルチスレッドを実装しやすく、並行性という点においても優れています。

Q&A

解決済

1回答

1811閲覧

RustでCSVファイルを読み込んで、DataFrameを返したい

gongongo

総合スコア1

Rust

Rustは、MoFoが支援するプログラミング言語。高速性を維持しつつも、メモリ管理を安全に行うことが可能な言語です。同じコンパイル言語であるC言語やC++では困難だったマルチスレッドを実装しやすく、並行性という点においても優れています。

1グッド

0クリップ

投稿2022/03/21 04:29

実現したいこと

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"]}
tatsuya6502👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

polarsのバージョンが0.20.0に変更しなければいけなかった

Polarsは現在活発に開発されているということですので、バージョンが上がる間にAPIも変更されたようです。

以下のドキュメントを見ながら最新のAPIに合うようにコードを修正したところ、エラーを解消することができました。

変更後のコードは以下のようになります。(関係する部分のみ掲載します)

rust

1fn read_csv_with_schema<P: AsRef<Path>>(path: P) -> PolarResult<DataFrame> { 2 // Schemaの定義方法を変更 3 let mut schema = Schema::new(); 4 schema.with_column("species".to_string(), DataType::Utf8); 5 schema.with_column("island".to_string(), DataType::Utf8); 6 schema.with_column("culmen_length_mm".to_string(), DataType::Float64); 7 schema.with_column("culmen_depth_mm".to_string(), DataType::Float64); 8 schema.with_column("flipper_length_mm".to_string(), DataType::Float64); 9 schema.with_column("body_mass_g".to_string(), DataType::Float64); 10 schema.with_column("sex".to_string(), DataType::Utf8); 11 12 let file = File::open(path).expect("Cannot open file."); 13 CsvReader::new(file) 14 .with_dtypes(Some(&schema)) // with_dtypesメソッドへ変更 15 .has_header(true) 16 .with_ignore_parser_errors(true) 17 .finish() 18} 19 20fn get_feature_target(df: &DataFrame) -> (PolarResult<DataFrame>, PolarResult<DataFrame>) { 21 let features = df.select(vec![ 22 "culmen_length_mm", 23 "culmen_depth_mm", 24 "flipper_length_mm", 25 "body_mass_g", 26 ]); 27 let target = df.select(["species"]); // [ ] を追加 28 (features, target) 29}

個人的な感想ですが、PolarsのAPIリファレンスには、まだサンプルコードがほとんど載っていないので、ちょっと分かりにくいと感じました。

今回は該当しなかったのですが、内容によっては、ユーザーガイド、サンプルコード、ソースコード中のテストケースなどにヒントがあることもあります。今後また何か分からないことが出てきたら、それらも見てみるといいでしょう。

投稿2022/03/30 23:47

tatsuya6502

総合スコア2035

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gongongo

2022/03/31 07:37

ご丁寧にありがとうございます。 次からはガイドやリファレンスに目を通すようにしたいと思います。 エラー原因だけでなく、デバッグの方法まで教えていただき感謝申しあげます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問