Turn Pandas Dataframe into LSTM input tensor
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
add a comment |
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
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:]).valuesX = pad_sequences(features, maxlen=seq_length)
– Digital-Thinking
Dec 28 '18 at 17:24
add a comment |
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
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
python-3.x pandas keras neural-network lstm
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:]).valuesX = pad_sequences(features, maxlen=seq_length)
– Digital-Thinking
Dec 28 '18 at 17:24
add a comment |
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:]).valuesX = 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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:]).valuesX = pad_sequences(features, maxlen=seq_length)– Digital-Thinking
Dec 28 '18 at 17:24