字符类型不能归一化...

数据处理经常要用到归一化处理,使各项指数的尺度达到统一,python中实现数据归一化处理的代码如下:

#准备工作:安装numpy、pandas包,csv文件转为数值型,之后修改出入输出地址即可。本文测试数据是点shp提取多种遥感指数导出的csv,第一行为列名

import numpy as np
import pandas as pd

data_value = np.loadtxt(open("G:/Download/maps/wangyd_data/ice2_wyd.csv"),delimiter=",",skiprows=1)
data_origin = pd.read_csv(r'G:/Download/maps/wangyd_data/ice2_wyd.csv',sep=',',header=None)#字符型列名与数值分开处理,最后再拼接
data_origin = np.array(data_origin)

def max_min_normalization(data_value):
    """
    函数主体,归一化处理
    Data normalization using max value and min value
    Args:
        data_value: The data to be normalized
    """
    data_shape = data_value.shape
    print(data_value.shape)
    data_rows = data_shape[0]
    data_cols = data_shape[1]
    new_data=np.zeros(shape=(data_rows+1,data_cols))
    origin = data_origin[0,:]
    for i in range(0, data_rows, 1):
        for j in range(0, data_cols, 1):
            data_col_min_values = min(data_value[:,j])
            data_col_max_values = max(data_value[:,j])
            new_data[i][j] = (data_value[i][j] - data_col_min_values) / (data_col_max_values - data_col_min_values)
    new_data = np.vstack((origin,new_data))
    np.savetxt("G:/Download/maps/wangyd_data/ice2_wyd_nor.csv", new_data, delimiter=',',fmt = '%s')#输出地址
    print('done')

max_min_normalization(data_value)

如果遇到问题或者提供建议,欢迎留言,也可联系QQ:1262840380

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐