python使用如下方法规范化数组_python - 归一化VS. numpy方式规范化? - 堆栈内存溢出...

有不同类型的规范化。 您正在使用min-max规范化。 来自scikit learn的min-max规范化如下。

import numpy as np

from sklearn.preprocessing import minmax_scale

# your function

def normalize_list(list_normal):

max_value = max(list_normal)

min_value = min(list_normal)

for i in range(len(list_normal)):

list_normal[i] = (list_normal[i] - min_value) / (max_value - min_value)

return list_normal

#Scikit learn version

def normalize_list_numpy(list_numpy):

normalized_list = minmax_scale(list_numpy)

return normalized_list

test_array = [1, 2, 3, 4, 5, 6, 7, 8, 9]

test_array_numpy = np.array(test_array)

print(normalize_list(test_array))

print(normalize_list_numpy(test_array_numpy))

输出:

[0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]

[0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]

MinMaxscaler完全使用您的公式进行规范化/缩放: http ://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.minmax_scale.html

@OuuGiii: 注意:使用Python内置函数名作为varibale名称不是一个好主意。 list()是一个Python内置函数,因此应该避免使用它作为变量。

import argparse import logging import re from multiprocessing import Process, Queue from pathlib import Path import numpy as np from skimage import exposure, filters from modules.config import logger from modules.volume import volume_loading_func, volume_saving_func def normalize_intensity( np_volume: np.ndarray, relative_path: Path, logger: logging.Logger ): logger.info(f"[processing start] {relative_path}") nstack = len(np_volume) stack: np.ndarray = np_volume[nstack // 2 - 16 : nstack // 2 + 16] hist_y, hist_x = exposure.histogram(stack[stack > 0]) thr = filters.threshold_otsu(stack[stack > 0]) peak_air = np.argmax(hist_y[hist_x < thr]) + hist_x[0] peak_soil = np.argmax(hist_y[hist_x > thr]) + (thr - hist_x[0]) + hist_x[0] np_volume = np_volume.astype(np.int64) for i in range(len(np_volume)): np_volume[i] = ( (np_volume[i] - peak_air).clip(0) / (peak_soil - peak_air) * 256 / 2 ) logger.info(f"[processing end] {relative_path}") return exposure.rescale_intensity( np_volume, in_range=(0, 255), out_range=(0, 255) ).astype(np.uint8) if name == "main": parser = argparse.ArgumentParser(description="Intensity Normalizer") parser.add_argument("-s", "--src", type=str, help="source directory.") parser.add_argument("-d", "--dst", type=str, help="destination directory.") parser.add_argument( "--mm_resolution", type=float, default=0.0, help="spatial resolution [mm].", ) parser.add_argument( "--depth", type=int, default=-1, help="depth of the maximum level to be explored. Defaults to unlimited.", ) args = parser.parse_args() if args.src is None: parser.print_help() exit(0) root_src_dir: Path = Path(args.src).resolve() if not root_src_dir.is_dir(): logger.error("Indicate valid virectory path.") exit() root_dst_dir = Path( args.dst or str(root_src_dir) + "_intensity_normalized" ) mm_resolution = float(args.mm_resolution) depth = int(args.depth) volume_loading_queue = Queue() volume_loading_process = Process( target=volume_loading_func, args=(root_src_dir, root_dst_dir, depth, volume_loading_queue, logger), ) volume_loading_process.start() volume_saving_queue = Queue() volume_saving_process = Process( target=volume_saving_func, args=(volume_saving_queue, logger), ) volume_saving_process.start() while True: ( volume_path, np_volume, volume_info, ) = volume_loading_queue.get() if volume_path is None: break relative_path = volume_path.relative_to(root_src_dir) np_volume = normalize_intensity(np_volume, relative_path, logger) if mm_resolution != 0: volume_info.update({"mm_resolution": mm_resolution}) while volume_saving_queue.qsize() == 1: pass dst_path = Path( root_dst_dir, re.sub(r"cb\d{3}$", "", str(relative_path)) ) volume_saving_queue.put( (dst_path, root_dst_dir, np_volume, volume_info) ) volume_saving_queue.put((None, None, None, None))完整详细的解释每一行的代码意思和作用
最新发布
04-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值