python实现区间转换、numpy图片数据转换
需求:
客户的需求是永无止境的,这不?前几天,用户提出了一个需求,需要将一组数据从一个区间缩放到另一区间?
思路:
先将数据归一化,再乘以对应区间的差加上对于区间下限。
数据归一化的公式:
# 区间变换
def unification_interval(data,interval_min,interval_max):
# data :需要变换的数据或矩阵
# interval_min :变换区间下限。
# interval_max :变换区间上限。
import numpy as np
data = np.array(data)
n,m = data.shape
minval = np.min(np.min(data))
maxval = np.max(np.max(data))
for i in range(n):
for j in range(m):
data[i,j] = (data[i,j]-minval)/(maxval-minval)
return data*(interval_max-interval_min)+interval_min
# 整形的转换
def Inter(data):
import numpy as np
data = np.array(data)
n,m = data.shape
for i in range(n):
for j in range(m):
data[i,j] = int(data[i,j])
return data
# coding:utf-8
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imresize # 区间变换
def unification_interval(data,interval_min,interval_max):
# data :需要变换的数据或矩阵
# interval_min :变换区间下限。
# interval_max :变换区间上限。
import numpy as np
data = np.array(data)
n,m = data.shape
minval = np.min(np.min(data))
maxval = np.max(np.max(data))
for i in range(n):
for j in range(m):
data[i,j] = (data[i,j]-minval)/(maxval-minval)
return data*(interval_max-interval_min)+interval_min def Inter(data):
import numpy as np
data = np.array(data)
n,m = data.shape
for i in range(n):
for j in range(m):
data[i,j] = int(data[i,j])
return data # 图片读入
img_data = pd.read_csv('./data/milliq.csv',header=0,index_col=0,sep=',')
# print(np.max(np.max(img_data)))
# print(np.min(np.min(img_data))) data = Inter(img_data) imgData = unification_interval(data,0,255) # print(np.max(np.max(imgData)),np.min(np.min(imgData))) data = imresize(data,[200,200]) plt.imshow(data,cmap='gray')
plt.show()