龙空技术网

Python实现最大值最小值归一化

大数据工程师张工 165

前言:

眼前兄弟们对“python编程求最大值”可能比较关心,你们都需要剖析一些“python编程求最大值”的相关知识。那么小编也在网上网罗了一些关于“python编程求最大值””的相关知识,希望大家能喜欢,大家快快来了解一下吧!

一.代码实现

# -*- coding: utf-8 -*-

"""

Created on Fri Sep 7 16:28:20 2018

@author: zhen

"""

# 最大值最小值归一化:(X-Xmin)/(Xmax-Xmin)

import numpy as np

import matplotlib.pyplot as plt

x = np.random.rand(100)

x = x.reshape(-1, 1)

rand = np.random.rand(100) * np.random.randint(10)

rand = rand.reshape(-1, 1)

# 获取Xmax,Xmin

x_max = np.max(rand)

x_min = np.min(rand)

result = []

# 查找数组的索引:np.where(rand == i)

for i in rand:

result.append((float(i[0]) - x_min)/(x_max - x_min))

result = np.array(result, dtype = float)

result = result.reshape(-1, 1)

# 可视化

plt.plot(x, rand, "r.", label="native")

plt.plot(x, result, "b.", linewidth=2, label="normalized")

plt.legend(loc="upper left")

plt.grid()

plt.show()

二.执行结果

红点表示归一化之前的原始数据,蓝点表示归一化后的数据!

标签: #python编程求最大值