0

I have a numpy feature_matrix as below.

feature_matrix = [[0.1,0.3,0.5], [0.7,0.8,0.1]]

Now I want to add 0, 1, 2, so on at the beginning of this feature matrix as below.

altered_feature_matrix = [[0, 0.1,0.3,0.5], [1, 0.7,0.8,0.1]]

I tried to do it using np.insert. However, then I get a flat list.

for l in write_fw_feature_matrix:)
    l=np.insert(l,[0],pos)
    altered_feature_matrix = np.insert(altered_feature_matrix, l)
    #print(l)
    pos = pos+1
print(altered_feature_matrix)

Please help me to resolve my problem.

4
  • No, I don't know the number of elements in the feature_matrix. so it depends on my situation. Please open this question Commented Sep 6, 2017 at 1:25
  • np.insert(feature_matrix,0,np.arange(len(feature_matrix)),axis=1) Commented Sep 6, 2017 at 1:27
  • @hpaulj Thank you for the comment. Can you please tell me what happens here? Commented Sep 6, 2017 at 1:32
  • column_stack and concatenate can also be used. The same concept, just slight differences in how they handle the inputs. Commented Sep 6, 2017 at 3:31

0