In [1]:
%matplotlib widget
In [2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import imageio
In [3]:
X_train = pd.read_csv("MLchallenge2_training.csv")
# There are 150 columns. Let's just see a few
X_train[['x', 'y', 'z', 'px', 'py', 'pz',
'x1', 'y1', 'z1', 'px1', 'py1', 'pz1']].head()
Out[3]:
In [4]:
def plot_quiver_track(df, track_id, elev=None,
azim=None, dist=None):
# Extract the track row
track = df.loc[track_id].values
# Get all the values of each type of feature
x = [track[(6*i)] for i in range(0, 25)]
y = [track[1+(6*i)] for i in range(0, 25)]
z = [track[2+(6*i)] for i in range(0, 25)]
px = [track[3+(6*i)] for i in range(0, 25)]
py = [track[4+(6*i)] for i in range(0, 25)]
pz = [track[5+(6*i)] for i in range(0, 25)]
# I ideally would like to link the magnitude
# of the momentum to the color, but my results
# were buggy...
p_tot = np.sqrt(np.square(px) +
np.square(py) +
np.square(pz))
# Create our 3D figure
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.xaxis.set_pane_color((1,1,1,1))
ax.yaxis.set_pane_color((1,1,1,1))
ax.zaxis.set_pane_color((1,1,1,1))
# Set the three 3D plot viewing attributes
if elev is not None:
ax.elev = elev
if azim is not None:
ax.azim = azim
if dist is not None:
ax.dist = dist
# Create our quiver plot
ax.quiver(z, x, y, pz, px, py, length=14)
# Labels for clarity
ax.set_title("Track {}".format(track_id))
ax.set_xlabel("z", fontweight="bold")
ax.set_ylabel("x", fontweight="bold")
ax.set_zlabel("y", fontweight="bold")
plt.tight_layout()
return fig, ax
In [5]:
fig, ax = plot_quiver_track(X_train, 2)
fig.show()
In [6]:
gif_filename = "track-2-anim"
ax.elev = 50.
ax.azim = 90.
ax.dist = 9.
img_files = []
for n in range(0, 100):
ax.elev = ax.elev-0.4
ax.azim = ax.azim+1.5
filename = f'images/{gif_filename}/img{str(n).zfill(3)}.png'
img_files.append(filename)
plt.savefig(filename, bbox_inches='tight')
In [7]:
images = []
for filename in img_files:
images.append(imageio.imread(filename))
imageio.mimsave('images/track-2.gif', images)
In [8]:
X_test = pd.read_csv("test_in.csv", names=X_train.columns)
X_test[['x', 'y', 'z', 'x15', 'y15', 'z15', 'x23', 'y23', 'z23']].head()
Out[8]:
In [9]:
import missingno as mno
ax = mno.matrix(X_test.head(100))
In [84]:
import re
from io import StringIO
In [93]:
with open('test_in.csv', 'r') as f:
data_str = f.read()
In [94]:
data_str_io = StringIO(
re.sub(r"([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\n)", r",,\1", data_str)
)
In [95]:
X_test = pd.read_csv(data_str_io, names=X_train.columns)
In [96]:
X_test.head()
Out[96]:
In [97]:
import re
from io import StringIO
def load_test_data(filename):
with open(filename, 'r') as f:
data_str = f.read()
data_str_io = StringIO(
re.sub(r"([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\n)", r",,\1", data_str)
)
X_test = pd.read_csv(data_str_io, names=X_train.columns)
return X_test
In [ ]: