使用numpy读写多列

时间:2015-01-02 21:51:27

标签: python for-loop numpy curve-fitting

我有一个txt文件,其中每列是单个测量。我有一段代码使用高斯滤波器平滑这些数据,但它只在一列上执行此操作,然后使用单列写入文本文件。我想遍历每一列执行高斯滤波器并最终写一个新的txt文件,其中每列是原始文本文件中相应列的高斯滤波器。

这是我到目前为止所做的:

import numpy as np
import scipy.ndimage

shrinkage = np.loadtxt('dilato_sample_data.txt', skiprows=1).T
smoothed = scipy.ndimage.gaussian_filter(shrinkage, 10)

np.savetxt('smoothed_data.txt', np.c_[time, smoothed])

以下是我原始数据的示例:

Dilato 1 (Lateral)  Dilato 1 (Vertical) Dilato 2 (Lateral)  Dilato 2 (Vertical) 
1   1.01298701298701    1.02197802197802    1.02127659574468
0.987654320987654   1.01298701298701    1.02197802197802    1.03191489361702
0.975308641975309   1   1.02197802197802    1.03191489361702
0.975308641975309   1   1.02197802197802    1.02127659574468
0.962962962962963   1   1.02197802197802    1.02127659574468
0.962962962962963   0.987012987012987   1.02197802197802    1.02127659574468
0.950617283950617   0.987012987012987   1.02197802197802    1.02127659574468
0.938271604938272   0.974025974025974   1.02197802197802    1.02127659574468
0.938271604938272   0.974025974025974   1.02197802197802    1.03191489361702
0.925925925925926   0.974025974025974   1.02197802197802    1.02127659574468
0.91358024691358    0.961038961038961   1.02197802197802    1.02127659574468
0.91358024691358    0.961038961038961   1.02197802197802    1.02127659574468
0.91358024691358    0.961038961038961   1.02197802197802    1.02127659574468
0.901234567901235   0.948051948051948   1.02197802197802    1.02127659574468
0.91358024691358    0.961038961038961   1.02197802197802    1.03191489361702
0.91358024691358    0.948051948051948   1.02197802197802    1.02127659574468
0.888888888888889   0.948051948051948   1.01098901098901    1.02127659574468
0.888888888888889   0.948051948051948   1.02197802197802    1.02127659574468
0.888888888888889   0.935064935064935   1.01098901098901    1.02127659574468
0.888888888888889   0.948051948051948   1.02197802197802    1.02127659574468
0.888888888888889   0.935064935064935   1.01098901098901    1.02127659574468
0.901234567901235   0.935064935064935   1.01098901098901    1.02127659574468
0.888888888888889   0.922077922077922   1.01098901098901    1.01063829787234
0.888888888888889   0.922077922077922   1.01098901098901    1.01063829787234
0.888888888888889   0.922077922077922   1.01098901098901    1.01063829787234

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

你可以:

  • 使用Pandas作为DataFrame加载文件,并使用apply对每列执行高斯过滤操作,并写回csv或文本文件,或者(如果您不想使用熊猫)
  • 加载文本文件后,将所有文本拆分为不同的列表,并使用for循环分别对每个列表执行高斯滤波操作。这样效率会有点低,但这取决于您是否在资源有限的情况下进行一次性流程。

答案 1 :(得分:0)

感谢@ vk1011的建议。我能够找出使用pandasapply函数的方法:

import pandas as pd
import numpy as np
import scipy.ndimage
import sys
from pandas import DataFrame, read_csv

df = pd.read_csv('dilato_data_all.csv')

def gaussian(x):
    smoothed = scipy.ndimage.gaussian_filter(x, 5)
    return(smoothed)

result_gaussian = df.apply(gaussian, axis=0)
result_gaussian.to_csv('gaussian_result.csv', index=True, header=True)