Как читать из файла CSV - PullRequest
       28

Как читать из файла CSV

0 голосов
/ 29 декабря 2018

Я пытаюсь понять, как Фильтр Калмана для нелинейной системы работает.В поисках примера я наткнулся на этот хороший базовый пример.

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

Однако для моего эксперимента - я создал очень маленькое времяДанные серии готовы с тремя столбцами, отформатированными следующим образом.Полный набор данных прилагается здесь для воспроизводимости.

  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 голосов
/ 29 декабря 2018

По ссылке, которой я поделился, вот как вы получаете данные CSV в Numpy Arrays.

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)
...