<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Plot Trajectories with Pictures
take dimension (e.g. red) that I've trained the nn features to classify and plot sequences in that dimension. 
use sequences that have images
Step1:  Plot Trajectories from User Profile Eval Dataset
same as above, but without images.
Step2:  Save
<ASSISTANT_TASK:>
Python Code:
# our lib
from lib.resnet50 import ResNet50
from lib.imagenet_utils import preprocess_input, decode_predictions
#keras 
from keras.preprocessing import image
from keras.models import Model
import glob
def preprocess_img(img_path):
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    return(x,img)
# instantiate the model
base_model = ResNet50(include_top=False, weights='imagenet') #this will pull the weights from the folder 
# cut the model to lower levels only 
model = Model(input=base_model.input, output=base_model.get_layer('avg_pool').output)
user_id = 106144465 
#get images 
folder = '../data_img_sample_item_view_sequences/'
img_files = glob.glob(folder+'*'+str(user_id)+'*')
print(img_files)
# make features 
trajectory_features = np.empty((len(img_files),2048))
for i,img_file in enumerate(img_files):
    x,img = preprocess_img(img_file) # preprocess
    trajectory_features[i,:] = model.predict(x)[0,0,0,:]
    
red_traj = np.dot(trajectory_features,red_weights)
print('target class')
plt.figure(figsize=(12,6))
len_seq = len(img_files)
fig,axes = plt.subplots(2,len_seq)
# make color
color_red_black = pd.Series(red_traj>0).map({False:'k',True:'r'}).as_matrix()
for i in range(len_seq):
    img = image.load_img(img_files[i], target_size=(224, 224))
    # images
    axes[0,i].imshow(img)
    axes[0,i].set_xticklabels([])
    #axes[0,i].get_xaxis().set_visible(False)
    axes[0,i].get_xaxis().set_ticks([])
    axes[0,i].get_yaxis().set_visible(False) 
    if i<(len_seq-1):
        axes[0,i].set_xlabel('view '+str(i))
    else:
        axes[0,i].set_xlabel('buy')
         
    # bar 
    axes[1,i].bar(0,red_traj[i],color=color_red_black[i])
    axes[1,i].set_ylim([-10,5])
    axes[1,i].get_xaxis().set_visible(False)
    axes[1,i].axhline(y=0,linestyle='--',color='w')
    if i==0:
        print('here')
        axes[1,i].set_ylabel('red classification')
    else:
        axes[1,i].get_yaxis().set_visible(False) 
    sns.despine()
savefile = '../figures/example_sequence_interpretable_features_ui_'+str(user_id)+'.png'
plt.savefig(savefile,dpi=300)
reload(src.s3_data_management)
from src import s3_data_management
s3_data_management.push_results_to_s3(os.path.basename(savefile),savefile)
Explanation: Plot Trajectories with Pictures
take dimension (e.g. red) that I've trained the nn features to classify and plot sequences in that dimension. 
use sequences that have images
End of explanation
# load weights from the nn
red_weights = np.loadtxt('../data_nn_features/class_weights_LR_redpink.txt')
# load smaller user behavior dataset
user_profile = pd.read_pickle('../data_user_view_buy/user_profile_items_nonnull_features_20_mins_5_views_v2_sample1000.pkl')
user_sample = user_profile.user_id.unique()
print(len(user_profile))
print(len(user_sample))
user_profile.head()
# read nn features 
spu_fea = pd.read_pickle("../data_nn_features/spu_fea_sample1000.pkl")
spu_fea.head()
# sample users
size = 10
np.random.seed(1000)
user_ids = np.random.choice(user_profile.user_id.unique(),size=size)
fig,axes = plt.subplots(size,1,figsize=(16,3*size),sharex=True,sharey=True)
for ui,user_id in enumerate(user_ids):
    # get his trajectory
    trajectory = user_profile.loc[user_profile.user_id==user_id,]
    
    # get trajectory features (make a separate function # )
    trajectory_features = np.empty((len(trajectory),2048))
    for i,(index,row) in enumerate(trajectory.iterrows()):
        trajectory_features[i,:] = spu_fea.loc[spu_fea.spu_id==row['view_spu'],'features'].as_matrix()[0]
        
    # project onto red dimension
    red_traj = np.dot(trajectory_features,red_weights)
    # plot 
    axes[ui].plot(np.arange(len(red_traj)),red_traj)
    axes[ui].axhline(y=0,linestyle='--',color='k')
    axes[ui].set_ylabel('red features')
    sns.despine()
plt.xlabel('positition in sequence')
savefile = '../figures/example_sequences_red_10_users.png'
plt.savefig(savefile,dpi=300)
from sklearn.decomposition import PCA
trajectory.head()
#spu_fea.spu_id['spu_']
# read nn features 
spu_fea = pd.read_pickle("../data_nn_features/spu_fea_sample1000.pkl")
# reduce dimensionality 
pca = pickle.load(open('../data_nn_features/pca_all_items_sample1000.pkl','rb'))
plt.plot(red_traj,np.arange(len(red_traj)))
plt.xlabel('red features')
plt.ylabel('positition in sequence')
sns.despine()
len(spu_fea['features'].as_matrix()[0])
#spu_fea[row['view_spu']]
projection = pca.transform(X_item_feature[0,:].reshape(-1,1).T)
projection.shape
plt.plot(np.arange(100),projection[0,0:100])
plt.xlabel('component')
plt.ylabel('projection')
sns.despine()
# sample users
size = 10
np.random.seed(1000)
user_ids = np.random.choice(user_profile.user_id.unique(),size=size)
fig,axes = plt.subplots(size,1,figsize=(16,3*size),sharex=True,sharey=True)
for ui,user_id in enumerate(user_ids):
    # get his trajectory
    trajectory = user_profile.loc[user_profile.user_id==user_id,]
    
    # get trajectory features (make a separate function # )
    trajectory_features = np.empty((len(trajectory),2048))
    for i,(index,row) in enumerate(trajectory.iterrows()):
        trajectory_features[i,:] = spu_fea.loc[spu_fea.spu_id==row['view_spu'],'features'].as_matrix()[0]
        
    # project onto pca dimension
    projected_traj = pca.transform(trajectory_features)
    
    # get first dimension 
    traj_PC1 = projected_traj[:,0]
    traj_PC2 = projected_traj[:,1]
    traj_PC3 = projected_traj[:,2]
    # plot 
    axes[ui].plot(traj_PC1,label='PC1')
    axes[ui].plot(traj_PC2,label='PC2')
    axes[ui].plot(traj_PC3,label='PC3')
    
    plt.legend()
    
    axes[ui].axhline(y=0,linestyle='--',color='k')
    axes[ui].set_ylabel('red features')
    sns.despine()
plt.xlabel('positition in sequence')
savefile = '../figures/example_sequences_PCA_10_users.png'
plt.savefig(savefile,dpi=300)
Explanation: Plot Trajectories from User Profile Eval Dataset
same as above, but without images.
End of explanation
%%bash 
#jupyter nbconvert --to Plotting_Sequences_in_low_dimensions.ipynb && mv Plotting_Sequences_in_low_dimensions.slides.html ../notebook_slides/Plotting_Sequences_in_low_dimensions_v1.slides.html
jupyter nbconvert --to html Plotting_Sequences_in_low_dimensions.ipynb && mv Exploring_Data.html ../notebook_htmls/Plotting_Sequences_in_low_dimensions_v1.html
cp Plotting_Sequences_in_low_dimensions.ipynb ../notebook_versions/Plotting_Sequences_in_low_dimensions_v1.ipynb
Explanation: Save
End of explanation
<END_TASK>