0

I have an array to which i would like to add 1's to the last column of the first 16 rows and 0's to the last column of rows from 17 to 27. I searched everywhere however splitting seems to work only when we want to split the array into even parts, if i could cut the array at a specific row into two smaller arrays it would be easy because i would just append 1's and 0's than bring the arrays together again.

Another option would be to hard code where the specific numbers should go by indexing but that seems a bit complicated

frankly i don't have any more ideas for it, here are the arrays for visual representation of the problem (with shortened rows):

[[0.133987 0.359897 0.000000 0.527855 0.764706 0.212121]
[0.301506 0.407752 0.013970 0.553386 0.830450 0.272727]
[0.414438 0.415456 0.023283 0.597853 0.833910 0.333333]
[0.434815 0.415844 0.046566 0.604616 0.837370 0.363636]
[0.436664 0.500333 0.093132 0.607321 0.868512 0.366667]
[0.447560 0.559649 0.098952 0.626934 0.882353 0.393939]]

I would like to have it like so:`

[[0.133987 0.359897 0.000000 0.527855 0.764706 0.212121 1]
[0.301506 0.407752 0.013970 0.553386 0.830450 0.272727 1]
[0.414438 0.415456 0.023283 0.597853 0.833910 0.333333 1]
[0.434815 0.415844 0.046566 0.604616 0.837370 0.363636 1]
[0.436664 0.500333 0.093132 0.607321 0.868512 0.366667 0]
[0.447560 0.559649 0.098952 0.626934 0.882353 0.393939 0]]
3
  • 1
    Do you have an array, a list of lists, or a numpy array? Each has its own solutions. Commented May 2, 2017 at 2:17
  • 1
    You describe your attempts, but it may be helpful to see some actual code of your (best) attempt. Commented May 2, 2017 at 2:18
  • it's a numpy array, I didn't try anything because i haven't found any method for it so i don't even know where to start Commented May 2, 2017 at 2:25

3 Answers 3

2

There is probably a cleaner more efficient way to do this but I think this is what you are after.

import numpy as np

data = np.array([[0.133987, 0.359897, 0.000000, 0.527855, 0.764706, 0.212121], [0.301506, 0.407752, 0.013970, 0.553386, 0.830450, 0.272727], [0.414438, 0.415456, 0.023283, 0.597853, 0.833910, 0.333333], [0.434815, 0.415844, 0.046566, 0.604616, 0.837370, 0.363636], [0.436664, 0.500333, 0.093132, 0.607321, 0.868512, 0.366667], [0.447560, 0.559649, 0.098952, 0.626934, 0.882353, 0.393939]])

extra = []
for i in range(len(data)):
    if i < 3:
        extra.append([1])
    else:
        extra.append([0])

extra = np.array(extra)

result = np.hstack([data, extra])

print(result)

resulting in

[[ 0.133987  0.359897  0.        0.527855  0.764706  0.212121  1.      ]
 [ 0.301506  0.407752  0.01397   0.553386  0.83045   0.272727  1.      ]
 [ 0.414438  0.415456  0.023283  0.597853  0.83391   0.333333  1.      ]
 [ 0.434815  0.415844  0.046566  0.604616  0.83737   0.363636  0.      ]
 [ 0.436664  0.500333  0.093132  0.607321  0.868512  0.366667  0.      ]
 [ 0.44756   0.559649  0.098952  0.626934  0.882353  0.393939  0.      ]]
Sign up to request clarification or add additional context in comments.

1 Comment

extra = [[int(i < len(data) // 2)] for i in range(len(data))] would be nicer. And either // or / depending on which portion of odd half should have 1 or 0.
1

Here is an abridged version which takes an input with 4 rows and adds 1 to the first 2 rows and 0 to the remaining rows. You would only have to adjust the numbers to fit your requirements.

v = [[0.133987, 0.359897, 0.000000, 0.527855, 0.764706, 0.212121],
     [0.301506, 0.407752, 0.013970, 0.553386, 0.830450, 0.272727],
     [0.414438, 0.415456, 0.023283, 0.597853, 0.833910, 0.333333],
     [0.447560, 0.559649, 0.098952, 0.626934, 0.882353, 0.393939]]

print('before **********')
for line in v:
 print(line)

for i, x in enumerate(v):
    if i < 2:
        x.append(1) # did you want ints here or floats to be consistent?
    else:
        x.append(0)

print('after **********')
for line in v:
    print(line)

output:

before **********
[0.133987, 0.359897, 0.0, 0.527855, 0.764706, 0.212121]
[0.301506, 0.407752, 0.01397, 0.553386, 0.83045, 0.272727]
[0.414438, 0.415456, 0.023283, 0.597853, 0.83391, 0.333333]
[0.44756, 0.559649, 0.098952, 0.626934, 0.882353, 0.393939]
after **********
[0.133987, 0.359897, 0.0, 0.527855, 0.764706, 0.212121, 1]
[0.301506, 0.407752, 0.01397, 0.553386, 0.83045, 0.272727, 1]
[0.414438, 0.415456, 0.023283, 0.597853, 0.83391, 0.333333, 0]
[0.44756, 0.559649, 0.098952, 0.626934, 0.882353, 0.393939, 0]

You could also shorten the append line to

x.append(1 if i < 2 else 0)

If you so wished.

Comments

0

Just make the last column and concatenate them:

arr = np.random.random(size=(27,6))
flag = [0]*16 + [1]*11
flag = np.array([flag])
arr_ = np.concatenate([arr, flag.T], axis=1)

The elements of a numpy array must have uniform dtypes, so you actually get 1. and 0. in the last column

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.