如何从CSV文件读取

时间:2018-12-29 16:26:48

标签: python-3.x pandas csv numpy kalman-filter

我试图了解非线性系统Kalman Filter的工作原理。在搜索示例时,我介绍了this个很好的基本示例。

import numpy as np
import pylab as pl
import pandas as pd
from pykalman import UnscentedKalmanFilter

# initialize parameters
def transition_function(state, noise):
    a = np.sin(state[0]) + state[1] * noise[0]
    b = state[1] + noise[1]
    return np.array([a, b])

def observation_function(state, noise):
    C = np.array([[-1, 0.5], [0.2, 0.1]])
    return np.dot(C, state) + noise

transition_covariance = np.eye(2)
random_state = np.random.RandomState(0)
observation_covariance = np.eye(2) + random_state.randn(2, 2) * 0.1
initial_state_mean = [0, 0]
initial_state_covariance = [[1, 0.1], [-0.1, 1]]

# sample from model
kf = UnscentedKalmanFilter(
    transition_function, observation_function,
    transition_covariance, observation_covariance,
    initial_state_mean, initial_state_covariance,
    random_state=random_state
)
states, observations = kf.sample(50, initial_state_mean)

# estimate state with filtering and smoothing
filtered_state_estimates = kf.filter(observations)[0]
smoothed_state_estimates = kf.smooth(observations)[0]

# draw estimates
pl.figure()
lines_true = pl.plot(states, color='b')
lines_filt = pl.plot(filtered_state_estimates, color='r', ls='-')
lines_smooth = pl.plot(smoothed_state_estimates, color='g', ls='-.')
pl.legend((lines_true[0], lines_filt[0], lines_smooth[0]),
          ('true', 'filt', 'smooth'),
          loc='lower left'
)
pl.show()

此代码产生以下图形。

enter image description here

但是,对于我的实验-我创建了一个非常小的时间序列数据,其中的三列格式如下。完整的数据集附在here上以提高可重复性。

  time        X      Y
 0.040662  1.041667  1
 0.139757  1.760417  2
 0.144357  1.190104  1
 0.145341  1.047526  1
 0.145401  1.011882  1
 0.148465  1.002970  1
 ....      .....     .

我们不使用代码中显示的随机值,而是如何从我附加的CSV文件中输入内容?这是我的方法,但似乎对我没有帮助,我将不胜感激。

df = pd.read_csv('testdata.csv')
pd.set_option('use_inf_as_null', True)

df.dropna(inplace=True)

X = df.drop('Y', axis=1)
y = df['Y']


d1= np.array(X)
d2 = np.array(y)

1 个答案:

答案 0 :(得分:0)

在我共享的链接中,这是将CSV数据导入Numpy数组的方法。

import numpy as np
import csv

with open('testdata.csv','r') as csvfile:
    r = csv.reader(csvfile, delimiter=',')
    data = [i for i in r]

headings = data.pop(0)
data = np.array([[np.float(j) for j in i] for i in data])

T = data.T[0] #Time
X = data.T[1] #X
Y = data.T[2] #Y

print(T)
print(X)
print(Y)