从文件中读取浮动数组并将其转换为整数

问题描述 投票:0回答:1

我已经看到

polars
可以用作rust等效的
pandas

,但是也许有一种更好的方法,因为没有数据框架操纵(我的其余我的python代码都用numpy数组运行) ,并且熊猫被用作解析TSV文件的方便方式)。

相关
如何使用Rust中的Porars编码为字符串的CSV列?

(未回答)
如何使用Rust读取本地CSV文件为ndarray(似乎是标准方式(另请参见Here) - 但似乎很冗长 - 也许提供的代码比我需要的要多得多。
从您的代码中的讨论中,我假设您使用以下输入文件: 0 1 2 1.0279113360087446 -1.2832284218816041 -0.9511599763983775 -1.1854242089984073 -0.008517913446124657 -1.3300888479137685 -0.17521484641409052 -0.12088194195850789 -0.08723124550308935 0.061450180456309234 0.6382691829655216 -0.3221706205270814 -0.17264583969234573 0.3906165503608199 -0.7023512952269605 -0.5688680458505405 0.7629597902952466 0.1591487223247267 -0.2866576739505336 0.8416529504197675 -0.21334731046185212 -0.3731653844853498 -0.03664374978977539 1.0659217203299267 0.2522037897994046 -1.2283963325279825 0.582406079711755 1.066724230889717 -0.630727285387302 0.9536582516683726 0.629243230148583 -0.6960460436000655 0.4521748684016147 -1.5540598822950011 0.9873241509921236 0.6415246342947979 -0.0284157295982256 -0.18702110687157966 1.7770271811904519 1.2382847672051143 -0.3760108519487906 -0.16110341746476323 -0.2808874342459878 0.6487504756926984 1.9778474878186199 -0.37522505843289716 1.7209367591622693 -0.19706519630516914 -0.33530410802770294 -0.04999186121022599 -0.675375947654844 -2.0252464624551956 -0.27944625298143044 1.385051832284722 1.2957606931360681 0.7365431841643268 1.3572525489892076 -1.3877762318274933 1.166958182611204 0.685506702653605 ,与此代码结合在一起:

rust rust-polars polars
1个回答
0
投票

提供以下输出:

[[1. 0. 0. 1. 0. 0. 0. 0. 1. 1. 1. 0. 0. 1. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 1. 1. 1. 1. 0. 0. 0. 0. 1. 0. 0. 1. 1. 0. 0. 1. 1.]
 [0. 0. 0. 0. 0. 1. 0. 1. 1. 1. 1. 1. 1. 0. 1. 0. 0. 1. 1. 1.]]

我认为aalgorithmithmithmithmithmithmithmith

您在忽略第一行的同时读取带有选项卡分离器的CSV
(这就是
pd.read_csv(.., sep='\t')

似乎是做的)

您将数据转换为
1.0

0.0

,具体取决于该值是否大于零(
    (X > 0).astype(np.float64)
  • 您将数据转置(
  • .T
  • 
    
    我认为您希望数据存储在某种程度上,因此并非每个行/列都是其自己的对象。
    
    都说,有很多方法可以在生锈中实现这一目标。但是,您在Python中使用
    numpy
  • pandas
    的事实向我表明,您可能希望将代码主要基于现有的高级数据操作库而不是自己实施。
    
    尽管出于表现原因,您可以在Python中这样做;通过循环通过数据迭代在Python中效率高。请注意,在Rust中,手动编写的用于操纵数据的循环的性能与使用库的性能相似,因为与Python相比,Rust物体和原语的开销很小。
  • 有几个高级生锈板条箱,它们具有与Python库相似的功能。对于数据进行序列化,我建议

serde


及其实现,在这种情况下,可能是

csv
到达数据表示,我建议

ndarray

您似乎已经找到了这两个自己,但是我只是想确认这两个是不错的选择。

在这里可能的生锈等效于您的Python代码。
依赖性(在Cargo.toml
):
[dependencies] csv = "1.3.1" ndarray = "0.16.1" ndarray-csv = "0.5.3"

代码:

use ndarray_csv::Array2Reader;

fn main() {
    let arr = csv::ReaderBuilder::new() // Configure your own CSV reader (required because tab separated)
        .delimiter(b'\t') // Specify tab separated
        .from_path("./myfile.tsv") // Open file
        .expect("Unable to open input file!") // Handle file open error
        .deserialize_array2_dynamic::<f64>() // Deserialize as f64 (f64 is the equivalent to Python floats, so I assume you want this)
        .expect("Unable to parse file content!") // Handle file parsing error
        .mapv_into(|val| if val > 0.0 { 1.0 } else { 0.0 }) // Perform the `X > 0` conversion
        .reversed_axes(); // Transpose in-place without actually copying any data

    println!("{:?}", arr); // Debug print
}
[[1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]], shape=[3, 20], strides=[1, 3], layout=Ff (0xa), const ndim=2


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.