LoginSignup
0
1

More than 5 years have passed since last update.

Python | Numpy > list > xの値が閾値未満の(x,y)の値を取り除いたリストを返す | link: ValueError: not enough values to unpack | 教えていただいた事項: Numpy.numpy.greater_equal()を使ったコード | Boolean indexing

Last updated at Posted at 2018-01-13
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6

処理概要

xs = [0, 1, 2, 3, 4]
ys = [3, 1, 4, 1, 5]
  • 上記のような(x,y)の組合せが与えられている
  • xは昇順にソートされている
  • xに対して閾値未満の(x,y)を除去した組合せを得たい
    • 例: x < 2

code

import numpy as np

xs = [0, 1, 2, 3, 4]
ys = [3, 1, 4, 1, 5]


def remove_lessThan(xs, ys, xmin):
    xns = np.array(xs)
    yns = np.array(ys)

    minidx = -1
    for idx, (ax, ay) in enumerate(zip(xns, yns)):
        if ax < xmin:
            minidx = idx
    return xns[minidx+1:], yns[minidx+1:]

wrkx, wrky = remove_lessThan(xs, ys, xmin=2)
print(xs, ys)
print(wrkx, wrky)

run
$ python3 test_remove_lessThan_180113.py 
[0, 1, 2, 3, 4] [3, 1, 4, 1, 5]
[2 3 4] [4 1 5]

builtin関数でこういう処理はあるかもしれない。

Error対処

for idx, ax, ay in enumerate(zip(xns, yns)):において「ValueError: not enough values to unpack」が発生していました。
@Sylba2050 さんの下記の記事に対処方法が書かれていました。
Python zipとenumerateの使い方

for i,(ai,bi) in enumerate(zip(a,b)): #zipのところを()で囲った

情報感謝です。

other link

教えていただいた事項1 > Numpy Logic functions

(追記 2018/01/14)

@carnage さんのコメントにおいてNumpyのLogic functionsであるgreater_equal()
を使ったコード例を教えていただきました。

情報感謝です。
使わせていただきます。

教えたいただいた事項2 > Boolean indexing

(追記 2018/01/14)

@skotaro さんのコメントにおいてBoolean indexingの使用例を教えていただきました。

情報感謝です。
使わせていただきます。

0
1
4

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
1