标准化

本文介绍了一种使用Python的sklearn库中的StandardScaler函数对Excel表格数据进行标准化处理的方法。通过读取Excel文件,将数据转化为矩阵,并应用StandardScaler进行标准化,最后将处理后的数据保存到新的Excel文件中。

可以用StandardScaler函数进行标准化,好处是可以保存训练集中的参数(均值、方差)直接使用其对象转换测试集数据

import numpy as np
import pandas as pd
import xlrd 
from sklearn import preprocessing
from pandas import DataFrame
def standardScaler(path):
    table = xlrd.open_workbook(path).sheets()[0]#获取第一个sheet表
    row = table.nrows  # 行数
    col = table.ncols  # 列数
    datamatrix = np.zeros((row, col))#生成一个nrows行ncols列,且元素均为0的初始矩阵
    for x in range(col):
        cols = np.matrix(table.col_values(x))  # 把list转换为矩阵进行矩阵操作
        datamatrix[:, x] = cols # 按列把数据存进矩阵中
    #标准化
    scaler=preprocessing.StandardScaler().fit(datamatrix)
    return (scaler.transform(datamatrix))
    #返回的就是标准化好的矩阵了
path = r'c:\Users\Liugengxin\Desktop\test.xlsx'
data = standardScaler(path) #标准化好的矩阵存在了data中
DataFrame(data).to_excel(r'c:\Users\Liugengxin\Desktop\test_end.xlsx')#写入test_end中

 

转载于:https://www.cnblogs.com/Liu269393/p/10259128.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值