Turn Pandas Dataframe into LSTM input tensor












1















I want to turn my pandas dataframe into an input tensor which is then going to be feed to an LSTM network.



The sequence is inside one of the columns in the dataframe in a list form. Additionally, there are two values that are associated with the sequence, namely the duration and user_cluster_id.



Dataframe looks like this:
Dataframe



I managed to extract the individual values of the sequence at each timestep and turn each of them into an input tensor, but I am guessing that it is wrong since I am breaking the sequence.



The pandas dataframe name is travel_history below is what I did:



input_seqs = 
output_seqs =
for i in range(travel_history.shape[0]):
user_cluster = travel_history.iloc[i,:]
cluster_id = user_cluster['sequence'].tolist()
user_class = user_cluster['user_cluster_id']
duration = int(user_cluster['duration'])

for i,cluster in enumerate(cluster_id):
if i < len(cluster_id)-1:
input_seqs.append([user_class,i, duration,cluster])
output_seqs.append([cluster_id[i+1]])


I expect an output that sort of looks like this: [sequence#1[cluster, duration,user_cluster_id].



Instead, the actual output looks like this [2, 0, 8, 87] where each value corresponds to the [user_cluster_id, timestep, duration, cluster]










share|improve this question























  • What about not iterating through the data and just map your column and to an np.array and after that use pad_sequences to prepare you sequences for the LSTM? e.g. features = df['sequence'].map(lambda x: np.array(x)[-seq_length:]).values X = pad_sequences(features, maxlen=seq_length)

    – Digital-Thinking
    Dec 28 '18 at 17:24


















1















I want to turn my pandas dataframe into an input tensor which is then going to be feed to an LSTM network.



The sequence is inside one of the columns in the dataframe in a list form. Additionally, there are two values that are associated with the sequence, namely the duration and user_cluster_id.



Dataframe looks like this:
Dataframe



I managed to extract the individual values of the sequence at each timestep and turn each of them into an input tensor, but I am guessing that it is wrong since I am breaking the sequence.



The pandas dataframe name is travel_history below is what I did:



input_seqs = 
output_seqs =
for i in range(travel_history.shape[0]):
user_cluster = travel_history.iloc[i,:]
cluster_id = user_cluster['sequence'].tolist()
user_class = user_cluster['user_cluster_id']
duration = int(user_cluster['duration'])

for i,cluster in enumerate(cluster_id):
if i < len(cluster_id)-1:
input_seqs.append([user_class,i, duration,cluster])
output_seqs.append([cluster_id[i+1]])


I expect an output that sort of looks like this: [sequence#1[cluster, duration,user_cluster_id].



Instead, the actual output looks like this [2, 0, 8, 87] where each value corresponds to the [user_cluster_id, timestep, duration, cluster]










share|improve this question























  • What about not iterating through the data and just map your column and to an np.array and after that use pad_sequences to prepare you sequences for the LSTM? e.g. features = df['sequence'].map(lambda x: np.array(x)[-seq_length:]).values X = pad_sequences(features, maxlen=seq_length)

    – Digital-Thinking
    Dec 28 '18 at 17:24
















1












1








1








I want to turn my pandas dataframe into an input tensor which is then going to be feed to an LSTM network.



The sequence is inside one of the columns in the dataframe in a list form. Additionally, there are two values that are associated with the sequence, namely the duration and user_cluster_id.



Dataframe looks like this:
Dataframe



I managed to extract the individual values of the sequence at each timestep and turn each of them into an input tensor, but I am guessing that it is wrong since I am breaking the sequence.



The pandas dataframe name is travel_history below is what I did:



input_seqs = 
output_seqs =
for i in range(travel_history.shape[0]):
user_cluster = travel_history.iloc[i,:]
cluster_id = user_cluster['sequence'].tolist()
user_class = user_cluster['user_cluster_id']
duration = int(user_cluster['duration'])

for i,cluster in enumerate(cluster_id):
if i < len(cluster_id)-1:
input_seqs.append([user_class,i, duration,cluster])
output_seqs.append([cluster_id[i+1]])


I expect an output that sort of looks like this: [sequence#1[cluster, duration,user_cluster_id].



Instead, the actual output looks like this [2, 0, 8, 87] where each value corresponds to the [user_cluster_id, timestep, duration, cluster]










share|improve this question














I want to turn my pandas dataframe into an input tensor which is then going to be feed to an LSTM network.



The sequence is inside one of the columns in the dataframe in a list form. Additionally, there are two values that are associated with the sequence, namely the duration and user_cluster_id.



Dataframe looks like this:
Dataframe



I managed to extract the individual values of the sequence at each timestep and turn each of them into an input tensor, but I am guessing that it is wrong since I am breaking the sequence.



The pandas dataframe name is travel_history below is what I did:



input_seqs = 
output_seqs =
for i in range(travel_history.shape[0]):
user_cluster = travel_history.iloc[i,:]
cluster_id = user_cluster['sequence'].tolist()
user_class = user_cluster['user_cluster_id']
duration = int(user_cluster['duration'])

for i,cluster in enumerate(cluster_id):
if i < len(cluster_id)-1:
input_seqs.append([user_class,i, duration,cluster])
output_seqs.append([cluster_id[i+1]])


I expect an output that sort of looks like this: [sequence#1[cluster, duration,user_cluster_id].



Instead, the actual output looks like this [2, 0, 8, 87] where each value corresponds to the [user_cluster_id, timestep, duration, cluster]







python-3.x pandas keras neural-network lstm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '18 at 14:06









midnight_ravermidnight_raver

135




135













  • What about not iterating through the data and just map your column and to an np.array and after that use pad_sequences to prepare you sequences for the LSTM? e.g. features = df['sequence'].map(lambda x: np.array(x)[-seq_length:]).values X = pad_sequences(features, maxlen=seq_length)

    – Digital-Thinking
    Dec 28 '18 at 17:24





















  • What about not iterating through the data and just map your column and to an np.array and after that use pad_sequences to prepare you sequences for the LSTM? e.g. features = df['sequence'].map(lambda x: np.array(x)[-seq_length:]).values X = pad_sequences(features, maxlen=seq_length)

    – Digital-Thinking
    Dec 28 '18 at 17:24



















What about not iterating through the data and just map your column and to an np.array and after that use pad_sequences to prepare you sequences for the LSTM? e.g. features = df['sequence'].map(lambda x: np.array(x)[-seq_length:]).values X = pad_sequences(features, maxlen=seq_length)

– Digital-Thinking
Dec 28 '18 at 17:24







What about not iterating through the data and just map your column and to an np.array and after that use pad_sequences to prepare you sequences for the LSTM? e.g. features = df['sequence'].map(lambda x: np.array(x)[-seq_length:]).values X = pad_sequences(features, maxlen=seq_length)

– Digital-Thinking
Dec 28 '18 at 17:24














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53959801%2fturn-pandas-dataframe-into-lstm-input-tensor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53959801%2fturn-pandas-dataframe-into-lstm-input-tensor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'