LoginSignup
0
0

More than 1 year has passed since last update.

[py2rb] Ruby#Array -> Python#Tuple

Last updated at Posted at 2022-02-04

はじめに

移植やってます。
( from python 3.7 to ruby 2.7 )

どうすRuby

Rubyの配列から、Numpy.arrayを作成します。

dtype = [['score', Numpy.int8], ['label', Numpy.str_, 1], ['pep', Numpy.float64], ['is decoy', Numpy.bool_]]
psms_ = Numpy.array(PSMS.map{ |s, l, p| [s, l, p, @is_decoy.call([s, l, p])] }, dtype: dtype)

# error
RuntimeError: Wrapped undumpable exception for: PyCall::PyError: <class 'TypeError'>: Field elements must be 2- or 3-tuples, got '['score', <class 'numpy.int8'>]'

tubleで渡せと。

先駆者がいて助かる。

dtype = [PyCall::Tuple.(['score', Numpy.int8]), PyCall::Tuple.(['label', Numpy.str_, 1]), PyCall::Tuple.(['pep', Numpy.float64]), PyCall::Tuple.(['is decoy', Numpy.bool_])]

# error
RuntimeError: Wrapped undumpable exception for: PyCall::PyError: <class 'ValueError'>: invalid literal for int() with base 10: 'A'

ぐぬぬ。

PSMS  [[0, "A", 0.01], [1, "B", 0.011], [2, "C", 0.012],........,[51, "z", 0.061]]

'A'が整数として無効です。

psms_ = Numpy.array(PSMS.map{ |s, l, p| PyCall::Tuple.([s, l, p, @is_decoy.call([s, l, p])]) }, dtype: dtype)

こちらも、tupleで渡さないといけないのね。

メモ

  • Python の tuple を学習した
  • 百里を行く者は九十里を半ばとす
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0