示例#1
0
def data_normalize(input_data, method='max', data_range=None, sum=1, warning=True, debug=True):
	'''
	this function normalizes N-d data in different ways: 1) normalize the data from a range to [0, 1]; 2) normalize the data which sums to a value

	parameters:
		input_data:			a list or a numpy N-d data to normalize
		method:				max:	normalize the data from a range to [0, 1], when the range is not given, the max and min are obtained from the data
							sum:	normalize the data such that all elements are summed to a value, the default value is 1
		data_range:			None or 2-element tuple, list or array
		sum:				a scalar

	outputs:
		normalized_data:	a float32 numpy array with same shape as the input data
	'''
	np_data = safe_npdata(input_data, warning=warning, debug=debug).astype('float32')
	if debug: 
		assert isnparray(np_data), 'the input data is not a numpy data'
		assert method in ['max', 'sum'], 'the method for normalization is not correct'

	if method == 'max':
		if data_range is None: max_value, min_value = np.max(np_data), np.min(np_data)
		else:	
			if debug: assert isrange(data_range), 'data range is not correct'
			max_value, min_value = data_range[1], data_range[0]
	elif method == 'sum':
		if debug: assert isscalar(sum), 'the sum is not correct'
		max_value, min_value = np.sum(np_data) / sum, 0

	normalized_data = (np_data - min_value) / (max_value - min_value)	# normalization

	return normalized_data
示例#2
0
def data_unnormalize(data, data_range, debug=True):
	'''
	this function unnormalizes 1-d label to normal scale based on range of data
	'''
	np_data = safe_npdata(input_data, warning=warning, debug=debug).astype('float32')
	if debug: 
		assert isnparray(np_data), 'the input data is not a numpy data'
		assert isrange(data_range), 'data range is not correct'

	max_value = data_range[1]
	min_value = data_range[0]
	unnormalized = np_data * (max_value - min_value) + min_value

	# if debug:
		# normalized = normalize_data(data=unnormalized, data_range=data_range, debug=False)
		# assert_almost_equal(data, normalized, decimal=6, err_msg='data is not correct: %f vs %f' % (data, normalized))
	return unnormalized