私はrecarray
csvファイルを読んで得たものを持っています。列のサブセットを連続浮動小数点配列に変換することに興味があります。それらをリストに変換したり、1つずつ積み重ねたりすることは避けたいです。https://stackoverflow.com/a/11792956とhttps://stackoverflow.com/a/7842620で提案を試しましたが、
ValueError: 新しい型は配列と互換性がありません。
これが私のコードです:
a = np.recfromcsv(r"myfile.csv")
#a has many columns of type int, float or string. I want to extract those called coeff*
coeffs_columns = [n for n in a.dtype.names if n.startswith('coeff')]
coeffs_recarray = a[coeffs_columns]
newtype=[(n,'<f8') for n in coeffs_columns]
b = coeffs_recarray.astype(newtype)
#b is:
#array((0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), dtype=[('coefficients00', '<f8'), ('coefficients1', '<f8'), ('coefficients2', '<f8'), ('coefficients3', '<f8'), ('coefficients4', '<f8'), ('coefficients5', '<f8'), ('coefficients6', '<f8'), ('coefficients7', '<f8'), ('coefficients8', '<f8'), ('coefficients9', '<f8'), ('coefficients100', '<f8'), ('coefficients11', '<f8'), ('coefficients12', '<f8'), ('coefficients13', '<f8'), ('coefficients14', '<f8')])
coeffs = b.view('<f8')
「面白い」ことは、1つの列のみを抽出する場合、またはrecarray
作成された列を使用する場合です
x = np.array([(1.0, 2,7.0), (3.0, 4, 9.9)],
dtype=[('x', '<f8'), ('y', '<f8'), ('z', '<f8')])
変換が機能します。