[PYTHON] How to use numpy.vectorize


I'm addicted to how to use the python scientific calculation module numpy function vectorize, so a memo for myself By the way, this time I posted this article using the histogram creation of the input image as an example in image processing.

Purpose of numpy.vectorize

Vectorize is a function that transforms a python function so that a list can be inserted into a function that takes a value as an argument. Each value of the input array will be calculated as an argument, and the return value will be vectorized.

How to use numpy.vectorize

First, prepare a function whose return value is not a list.


def myfunc(a,b):
      return a+b

print myfunc("hoge","Hoge")

The output looks like this:


Consider plunging a vector into this myfunc.


def myfunc(a,b):
      return a+b

list = ["hoge","fuga"]

print myfunc(list,"Hoge")

I want the output to look like this:


But in reality

TypeError: can only concatenate list (not "str") to list

Will be. This is because it was not originally defined to take a list as an argument. However, you can get the expected output by vectorizing myfunc with numpy.vectorize.

print vfunc(list,"Hoge")

The output is


The list is returned. Now the vectorization of the function has been achieved.

Practical example

I will leave a more practical example. Create a function that creates a histogram of the input image by image processing.

import numpy as np
import cv2
from matplotlib import pyplot as plt

#Function for creating a histogram
def img_hist(src,bins_array):
        x = np.where(src==bins_array,1,0)
        count = np.count_nonzero(x)
        return count
vhist = np.vectorize(img_hist)
vhist.excluded.add(0) #The 0th argument is passed to the function as it is as a fixed vector

#Prepare source file and histogram bin
src = cv2.imread("origin/LENNA.pgm",flags=0)
bins_array = np.arange(256)

#Create a histogram

#Visualize the histogram

The input image is a classic one

As a result, the following histogram is obtained.


Recommended Posts

How to use numpy.vectorize
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use Pandas 2
How to use Virtualenv
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv
How to use redis-py Dictionaries
Python: How to use pydub
[Python] How to use checkio
[Go] How to use "... (3 periods)"
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory
How to use Python bytes
How to use cron (personal memo)
Python: How to use async with