# plot histograms for multiple subjects from the har dataset
from matplotlib import pyplot
from numpy import dstack
from numpy import unique
from pandas import read_csv


# load a single file as a numpy array
def load_file(filepath):
    dataframe = read_csv(filepath, header=None, delim_whitespace=True)
    return dataframe.values


# load a list of files, such as x, y, z data for a given variable
def load_group(filenames, prefix=''):
    loaded = list()
    for name in filenames:
        data = load_file(prefix + name)
        loaded.append(data)
    # stack group so that features are the 3rd dimension
    loaded = dstack(loaded)
    return loaded


# load a dataset group, such as train or test
def load_dataset(group, prefix=''):
    filepath = prefix + group + '/Inertial Signals/'
    # load all 9 files as a single array
    filenames = list()
    # total acceleration
    filenames += ['total_acc_x_' + group + '.txt', 'total_acc_y_' + group + '.txt', 'total_acc_z_' + group + '.txt']
    # body acceleration
    filenames += ['body_acc_x_' + group + '.txt', 'body_acc_y_' + group + '.txt', 'body_acc_z_' + group + '.txt']
    # body gyroscope
    filenames += ['body_gyro_x_' + group + '.txt', 'body_gyro_y_' + group + '.txt', 'body_gyro_z_' + group + '.txt']
    # load input data
    X = load_group(filenames, filepath)
    # load class output
    y = load_file(prefix + group + '/y_' + group + '.txt')
    return X, y


# get all data for one subject
def data_for_subject(X, y, sub_map, sub_id):
    # get row indexes for the subject id
    ix = [i for i in range(len(sub_map)) if sub_map[i] == sub_id]
    # return the selected samples
    return X[ix, :, :], y[ix]


# convert a series of windows to a 1D list
def to_series(windows):
    series = list()
    for window in windows:
        # remove the overlap from the window
        half = int(len(window) / 2) - 1
        for value in window[-half:]:
            series.append(value)
    return series


# plot histograms for multiple subjects
def plot_subject_histograms(X, y, sub_map, offset, n=10):
    pyplot.figure()
    # get unique subjects
    subject_ids = unique(sub_map[:, 0])
    # enumerate subjects
    for k in range(n):
        sub_id = subject_ids[k]
        # get data for one subject
        subX, _ = data_for_subject(X, y, sub_map, sub_id)
        # total acc
        for i in range(3):
            ax = pyplot.subplot(n, 1, k + 1)
            ax.set_xlim(-1, 1)
            ax.hist(to_series(subX[:, :, offset + i]), bins=100)
            pyplot.yticks([])
            pyplot.xticks([-1, 0, 1])
    pyplot.show()


# load training dataset
X, y = load_dataset('train', 'HARDataset/')
# load mapping of rows to subjects
sub_map = load_file('HARDataset/train/subject_train.txt')
# plot total acceleration histograms for subjects
plot_subject_histograms(X, y, sub_map, 0)
# plot body acceleration histograms for subjects
plot_subject_histograms(X, y, sub_map, 3)
# plot gyroscopic histograms for subjects
plot_subject_histograms(X, y, sub_map, 6)

1fcb76e265da487892cdabcedef1ff82cdb52fe4
<mask token>


def load_file(filepath):
    dataframe = read_csv(filepath, header=None, delim_whitespace=True)
    return dataframe.values


<mask token>


def load_dataset(group, prefix=''):
    filepath = prefix + group + '/Inertial Signals/'
    filenames = list()
    filenames += ['total_acc_x_' + group + '.txt', 'total_acc_y_' + group +
        '.txt', 'total_acc_z_' + group + '.txt']
    filenames += ['body_acc_x_' + group + '.txt', 'body_acc_y_' + group +
        '.txt', 'body_acc_z_' + group + '.txt']
    filenames += ['body_gyro_x_' + group + '.txt', 'body_gyro_y_' + group +
        '.txt', 'body_gyro_z_' + group + '.txt']
    X = load_group(filenames, filepath)
    y = load_file(prefix + group + '/y_' + group + '.txt')
    return X, y


def data_for_subject(X, y, sub_map, sub_id):
    ix = [i for i in range(len(sub_map)) if sub_map[i] == sub_id]
    return X[ix, :, :], y[ix]


def to_series(windows):
    series = list()
    for window in windows:
        half = int(len(window) / 2) - 1
        for value in window[-half:]:
            series.append(value)
    return series


def plot_subject_histograms(X, y, sub_map, offset, n=10):
    pyplot.figure()
    subject_ids = unique(sub_map[:, 0])
    for k in range(n):
        sub_id = subject_ids[k]
        subX, _ = data_for_subject(X, y, sub_map, sub_id)
        for i in range(3):
            ax = pyplot.subplot(n, 1, k + 1)
            ax.set_xlim(-1, 1)
            ax.hist(to_series(subX[:, :, offset + i]), bins=100)
            pyplot.yticks([])
            pyplot.xticks([-1, 0, 1])
    pyplot.show()


<mask token>

<mask token>


def load_file(filepath):
    dataframe = read_csv(filepath, header=None, delim_whitespace=True)
    return dataframe.values


def load_group(filenames, prefix=''):
    loaded = list()
    for name in filenames:
        data = load_file(prefix + name)
        loaded.append(data)
    loaded = dstack(loaded)
    return loaded


def load_dataset(group, prefix=''):
    filepath = prefix + group + '/Inertial Signals/'
    filenames = list()
    filenames += ['total_acc_x_' + group + '.txt', 'total_acc_y_' + group +
        '.txt', 'total_acc_z_' + group + '.txt']
    filenames += ['body_acc_x_' + group + '.txt', 'body_acc_y_' + group +
        '.txt', 'body_acc_z_' + group + '.txt']
    filenames += ['body_gyro_x_' + group + '.txt', 'body_gyro_y_' + group +
        '.txt', 'body_gyro_z_' + group + '.txt']
    X = load_group(filenames, filepath)
    y = load_file(prefix + group + '/y_' + group + '.txt')
    return X, y


def data_for_subject(X, y, sub_map, sub_id):
    ix = [i for i in range(len(sub_map)) if sub_map[i] == sub_id]
    return X[ix, :, :], y[ix]


def to_series(windows):
    series = list()
    for window in windows:
        half = int(len(window) / 2) - 1
        for value in window[-half:]:
            series.append(value)
    return series


def plot_subject_histograms(X, y, sub_map, offset, n=10):
    pyplot.figure()
    subject_ids = unique(sub_map[:, 0])
    for k in range(n):
        sub_id = subject_ids[k]
        subX, _ = data_for_subject(X, y, sub_map, sub_id)
        for i in range(3):
            ax = pyplot.subplot(n, 1, k + 1)
            ax.set_xlim(-1, 1)
            ax.hist(to_series(subX[:, :, offset + i]), bins=100)
            pyplot.yticks([])
            pyplot.xticks([-1, 0, 1])
    pyplot.show()


<mask token>

<mask token>


def load_file(filepath):
    dataframe = read_csv(filepath, header=None, delim_whitespace=True)
    return dataframe.values


def load_group(filenames, prefix=''):
    loaded = list()
    for name in filenames:
        data = load_file(prefix + name)
        loaded.append(data)
    loaded = dstack(loaded)
    return loaded


def load_dataset(group, prefix=''):
    filepath = prefix + group + '/Inertial Signals/'
    filenames = list()
    filenames += ['total_acc_x_' + group + '.txt', 'total_acc_y_' + group +
        '.txt', 'total_acc_z_' + group + '.txt']
    filenames += ['body_acc_x_' + group + '.txt', 'body_acc_y_' + group +
        '.txt', 'body_acc_z_' + group + '.txt']
    filenames += ['body_gyro_x_' + group + '.txt', 'body_gyro_y_' + group +
        '.txt', 'body_gyro_z_' + group + '.txt']
    X = load_group(filenames, filepath)
    y = load_file(prefix + group + '/y_' + group + '.txt')
    return X, y


def data_for_subject(X, y, sub_map, sub_id):
    ix = [i for i in range(len(sub_map)) if sub_map[i] == sub_id]
    return X[ix, :, :], y[ix]


def to_series(windows):
    series = list()
    for window in windows:
        half = int(len(window) / 2) - 1
        for value in window[-half:]:
            series.append(value)
    return series


def plot_subject_histograms(X, y, sub_map, offset, n=10):
    pyplot.figure()
    subject_ids = unique(sub_map[:, 0])
    for k in range(n):
        sub_id = subject_ids[k]
        subX, _ = data_for_subject(X, y, sub_map, sub_id)
        for i in range(3):
            ax = pyplot.subplot(n, 1, k + 1)
            ax.set_xlim(-1, 1)
            ax.hist(to_series(subX[:, :, offset + i]), bins=100)
            pyplot.yticks([])
            pyplot.xticks([-1, 0, 1])
    pyplot.show()


X, y = load_dataset('train', 'HARDataset/')
sub_map = load_file('HARDataset/train/subject_train.txt')
plot_subject_histograms(X, y, sub_map, 0)
plot_subject_histograms(X, y, sub_map, 3)
plot_subject_histograms(X, y, sub_map, 6)

from matplotlib import pyplot
from numpy import dstack
from numpy import unique
from pandas import read_csv


def load_file(filepath):
    dataframe = read_csv(filepath, header=None, delim_whitespace=True)
    return dataframe.values


def load_group(filenames, prefix=''):
    loaded = list()
    for name in filenames:
        data = load_file(prefix + name)
        loaded.append(data)
    loaded = dstack(loaded)
    return loaded


def load_dataset(group, prefix=''):
    filepath = prefix + group + '/Inertial Signals/'
    filenames = list()
    filenames += ['total_acc_x_' + group + '.txt', 'total_acc_y_' + group +
        '.txt', 'total_acc_z_' + group + '.txt']
    filenames += ['body_acc_x_' + group + '.txt', 'body_acc_y_' + group +
        '.txt', 'body_acc_z_' + group + '.txt']
    filenames += ['body_gyro_x_' + group + '.txt', 'body_gyro_y_' + group +
        '.txt', 'body_gyro_z_' + group + '.txt']
    X = load_group(filenames, filepath)
    y = load_file(prefix + group + '/y_' + group + '.txt')
    return X, y


def data_for_subject(X, y, sub_map, sub_id):
    ix = [i for i in range(len(sub_map)) if sub_map[i] == sub_id]
    return X[ix, :, :], y[ix]


def to_series(windows):
    series = list()
    for window in windows:
        half = int(len(window) / 2) - 1
        for value in window[-half:]:
            series.append(value)
    return series


def plot_subject_histograms(X, y, sub_map, offset, n=10):
    pyplot.figure()
    subject_ids = unique(sub_map[:, 0])
    for k in range(n):
        sub_id = subject_ids[k]
        subX, _ = data_for_subject(X, y, sub_map, sub_id)
        for i in range(3):
            ax = pyplot.subplot(n, 1, k + 1)
            ax.set_xlim(-1, 1)
            ax.hist(to_series(subX[:, :, offset + i]), bins=100)
            pyplot.yticks([])
            pyplot.xticks([-1, 0, 1])
    pyplot.show()


X, y = load_dataset('train', 'HARDataset/')
sub_map = load_file('HARDataset/train/subject_train.txt')
plot_subject_histograms(X, y, sub_map, 0)
plot_subject_histograms(X, y, sub_map, 3)
plot_subject_histograms(X, y, sub_map, 6)

# plot histograms for multiple subjects from the har dataset
from matplotlib import pyplot
from numpy import dstack
from numpy import unique
from pandas import read_csv


# load a single file as a numpy array
def load_file(filepath):
    dataframe = read_csv(filepath, header=None, delim_whitespace=True)
    return dataframe.values


# load a list of files, such as x, y, z data for a given variable
def load_group(filenames, prefix=''):
    loaded = list()
    for name in filenames:
        data = load_file(prefix + name)
        loaded.append(data)
    # stack group so that features are the 3rd dimension
    loaded = dstack(loaded)
    return loaded


# load a dataset group, such as train or test
def load_dataset(group, prefix=''):
    filepath = prefix + group + '/Inertial Signals/'
    # load all 9 files as a single array
    filenames = list()
    # total acceleration
    filenames += ['total_acc_x_' + group + '.txt', 'total_acc_y_' + group + '.txt', 'total_acc_z_' + group + '.txt']
    # body acceleration
    filenames += ['body_acc_x_' + group + '.txt', 'body_acc_y_' + group + '.txt', 'body_acc_z_' + group + '.txt']
    # body gyroscope
    filenames += ['body_gyro_x_' + group + '.txt', 'body_gyro_y_' + group + '.txt', 'body_gyro_z_' + group + '.txt']
    # load input data
    X = load_group(filenames, filepath)
    # load class output
    y = load_file(prefix + group + '/y_' + group + '.txt')
    return X, y


# get all data for one subject
def data_for_subject(X, y, sub_map, sub_id):
    # get row indexes for the subject id
    ix = [i for i in range(len(sub_map)) if sub_map[i] == sub_id]
    # return the selected samples
    return X[ix, :, :], y[ix]


# convert a series of windows to a 1D list
def to_series(windows):
    series = list()
    for window in windows:
        # remove the overlap from the window
        half = int(len(window) / 2) - 1
        for value in window[-half:]:
            series.append(value)
    return series


# plot histograms for multiple subjects
def plot_subject_histograms(X, y, sub_map, offset, n=10):
    pyplot.figure()
    # get unique subjects
    subject_ids = unique(sub_map[:, 0])
    # enumerate subjects
    for k in range(n):
        sub_id = subject_ids[k]
        # get data for one subject
        subX, _ = data_for_subject(X, y, sub_map, sub_id)
        # total acc
        for i in range(3):
            ax = pyplot.subplot(n, 1, k + 1)
            ax.set_xlim(-1, 1)
            ax.hist(to_series(subX[:, :, offset + i]), bins=100)
            pyplot.yticks([])
            pyplot.xticks([-1, 0, 1])
    pyplot.show()


# load training dataset
X, y = load_dataset('train', 'HARDataset/')
# load mapping of rows to subjects
sub_map = load_file('HARDataset/train/subject_train.txt')
# plot total acceleration histograms for subjects
plot_subject_histograms(X, y, sub_map, 0)
# plot body acceleration histograms for subjects
plot_subject_histograms(X, y, sub_map, 3)
# plot gyroscopic histograms for subjects
plot_subject_histograms(X, y, sub_map, 6)
