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

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

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

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1903閲覧

Scipyのargrelmaxを使って極値を算出し、再度リストに入れたいが上手くいかない。

ShibaSamo

総合スコア15

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/01/15 22:16

前提・実現したいこと

python3.6のScipyライブラリを使って、あるデータの極値を求めようと考えています。
argrelmax()、argrelmin()という関数を使えば、Numpy配列から作ったグラフの極値のインデックスを取得できることを知ったのですが、そこから極値に該当するデータのみをNumpy配列やリストに収める方法がわからず、頭を抱えています。
ソースコードの
e_center.txt
には、改行で区切られたグラフの値(x-y座標でいうところのyの値のみ)が入っています。

発生している問題・エラーメッセージ

C:\Users\USER\python\FDTD\シン・ELECTROMAGNETIC SIMULATION USING THE FDTD(202201 10より)\テストコード>python for_envelope.py Traceback (most recent call last): File "for_envelope.py", line 22, in <module> maxid = signal.argrelmax(e_center_raw_n, order=1) File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\s cipy\signal\_peak_finding.py", line 191, in argrelmax return argrelextrema(data, np.greater, axis, order, mode) File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\s cipy\signal\_peak_finding.py", line 246, in argrelextrema axis, order, mode) File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\s cipy\signal\_peak_finding.py", line 74, in _boolrelextrema results &= comparator(main, plus) numpy.core._exceptions.UFuncTypeError: ufunc 'greater' did not contain a loop wi th signature matching types (dtype('<U23'), dtype('<U23')) -> dtype('bool')

該当のソースコード

python3.6

1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4""" 5包絡線を書く用 6""" 7 8import numpy as np 9import scipy.constants as sc 10from scipy import signal 11 12 13with open('e_center.txt') as f: 14 e_center_raw = [s.strip() for s in f.readlines()] 15 16e_center_raw_n = np.array(e_center_raw) 17 18maxid = signal.argrelmax(e_center_raw_n, order=1) 19minid = signal.argrelmin(e_center_raw_n, order=1) 20 21e_center_env = e_center_raw_n[maxid] 22 23f = open('e_center_envelope.txt', 'w') 24for x in e_center_env: 25 f.write(str(x) + "\n") 26f.close()

補足情報(FW/ツールのバージョンなど)

scipy 1.5.4
numpy 1.19.5

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

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

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

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

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

guest

回答1

0

ベストアンサー

データの中身がわかりませんが、'12345678901234567890123'のようなものが入っているとそうなります。

python

1>>> e_center_raw = ['12345678901234567890123', '12345678901234567890125', '12345678901234567890120'] 2>>> e_center_raw_n = np.array(e_center_raw) 3>>> maxid = signal.argrelmax(e_center_raw_n, order=1) 4Traceback (most recent call last): 5 File "<stdin>", line 1, in <module> 6 File "C:\Users\shinp\anaconda3\envs\sandbox\lib\site-packages\scipy\signal\_peak_finding.py", line 191, in argrelmax 7 return argrelextrema(data, np.greater, axis, order, mode) 8 File "C:\Users\shinp\anaconda3\envs\sandbox\lib\site-packages\scipy\signal\_peak_finding.py", line 245, in argrelextrema 9 results = _boolrelextrema(data, comparator, 10 File "C:\Users\shinp\anaconda3\envs\sandbox\lib\site-packages\scipy\signal\_peak_finding.py", line 74, in _boolrelextrema 11 results &= comparator(main, plus) 12numpy.core._exceptions._UFuncNoLoopError: ufunc 'greater' did not contain a loop with signature matching types (dtype('<U23'), dtype('<U23')) -> dtype('bool')

その場合は

python

1with open('e_center.txt') as f: 2 e_center_raw = [s.strip() for s in f.readlines()]

python

1with open('e_center.txt') as f: 2 e_center_raw = [int(s.strip()) for s in f.readlines()]

とすれば動きます。

データに
'1234 67890 234567 90123'のようなものが入っているなら、さらに変更が必要です。

投稿2022/01/15 22:54

ppaul

総合スコア24666

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

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

ShibaSamo

2022/01/16 12:53

int(s.strip())の部分をfloat(s.strip())とすることで求めていたことができました! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問