I have a numpy array that looks like this:
[[0.67058825 0.43529415 0.33725491]
[0.01568628 0.30980393 0.96862751]
[0.24705884 0.63529414 0.29411766]
[0.27843139 0.63137257 0.37647063]
[0.26274511 0.627451 0.33333334]
[0.25098041 0.61960787 0.30980393]]
I want to add a 1 to every row like this:
[[0.67058825 0.43529415 0.33725491 1]
[0.01568628 0.30980393 0.96862751 1]
[0.24705884 0.63529414 0.29411766 1]
[0.27843139 0.63137257 0.37647063 1]
[0.26274511 0.627451 0.33333334 1]
[0.25098041 0.61960787 0.30980393 1]]
Simply with numpy.insert to insert the needed value into required position along the given axis:
arr = np.insert(arr, arr.shape[1], 1, axis=1)
[[0.67058825 0.43529415 0.33725491 1. ]
[0.01568628 0.30980393 0.96862751 1. ]
[0.24705884 0.63529414 0.29411766 1. ]
[0.27843139 0.63137257 0.37647063 1. ]
[0.26274511 0.627451 0.33333334 1. ]
[0.25098041 0.61960787 0.30980393 1. ]]
Assuming a the input, you can try:
out = np.c_[a, np.ones((a.shape[0], 1))]
Or:
out = np.hstack([a, np.ones((a.shape[0], 1))])
Output:
array([[0.67058825, 0.43529415, 0.33725491, 1. ],
[0.01568628, 0.30980393, 0.96862751, 1. ],
[0.24705884, 0.63529414, 0.29411766, 1. ],
[0.27843139, 0.63137257, 0.37647063, 1. ],
[0.26274511, 0.627451 , 0.33333334, 1. ],
[0.25098041, 0.61960787, 0.30980393, 1. ]])
You can do this:
arr = [ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] ]
to_add = [1 for i in range(0, len(arr)) #Making array with all 1s
x = np.column_stack((arr, to_add)) #Add columns together
print(x)
Returns:
[[0 0 0 0 1]
[0 0 0 0 1]
[0 0 0 0 1]]
Related
I have to find the minimum for each column in a matrix, but there are two rules;
Each column will start from the "index+1" of the previous column, except for the first column
If one of the columns has exactly the index equal to the total number of rows of the matrix, then the rest indices for all the columns will be equal to the number of rows
As an example;
[[ "-1". -11. 0. 8. 1. ]
[ 2. 1. 0. 5. 1. ]
[ 4. 1. -2. 6. 7. ]
[ 8. 3. 1. 3. 0. ]
[ 5. "0". 1. 0. 8. ]
[ 9. 3. "-1". -1. 6.5]
[ 5. 3. 2. 5. 3. ]
[ 10. 3. 7. "1". "-1". ]]
The indices are inside quotations, [0,4,5,7,7]
Another example;
[[ 1. 1. 0. 0. 1.]
[ 2. 1. 0. 5. 1.]
[-4. -1. 2. 6. 7.]
['-5' 3. 1. 1. 0.]
[ 5. '0'. 1. 0. 8.]
[ 5. 3. '-1'. -1. 0.]
[ 5. 3. 1. '1'. 0.]
[ 5. 3. 1. 1. 0.]]
The indices here are [3,4,5,6,7]
I tried to do the following, but I am having errors. Could you please tell me how to do so?
def lst_min(matrix, columnindex, minIndex):
if minIndex == matrix.shape[0]:
return matrix.shape[0]
else:
return np.argmin(matrix[minIndex:, columnindex]) + minIndex
currentMinIndex = 0
lst = []
for i in range(a.shape[1]):
w = lst_min(matrix=a, columnindex=i, minIndex=currentMinIndex)
if w > a.shape[0]:
w = a.shape[0]
lst.append(w)
if w == 0:
c = 1
currentMinIndex = w + c
if currentMinIndex > a.shape[0]:
currentMinIndex = a.shape[0]
Your code use lst.append(w) with some strange logic...
You use lst_min to find minimum index but you simply return matrix.shape[0] when currentMinIndex = 0 (minIndex == matrix.shape[0]) at start.
FIY,
# post source code next time if you can,
# it will be really helpful to others to run your question easily
# and focus on the main problem quickly.
a = np.array(
[[ -1, -11, 0, 8, 1. ],
[ 2, 1, 0, 5, 1. ],
[ 4, 1, -2, 6, 7. ],
[ 8, 3, 1, 3, 0. ],
[ 5, 0, 1, 0, 8. ],
[ 9, 3, -1, -1, 6.5],
[ 5, 3, 2, 5, 3. ],
[ 10, 3, 7, 1, -1. ]]
)
b = np.array(
[[ 1, 1, 0, 0, 1],
[ 2, 1, 0, 5, 1],
[-4, -1, 2, 6, 7],
[-5, 3, 1, 1, 0],
[ 5, 0, 1, 0, 8],
[ 5, 3, -1, -1, 0],
[ 5, 3, 1, 1, 0],
[ 5, 3, 1, 1, 0]]
)
lst = []
start_idx = 0
for vec in a.T: # forloop column-wise
if start_idx >= vec.shape[0]-1: # index compare shape, should -1
lst.append(vec.shape[0]-1) # put the last index
else: # find minimum index
min_idx = np.argmin(vec[start_idx:]) # slice it
start_idx += min_idx # add back the true index
lst.append(start_idx) # append to result
start_idx += 1 # next column, use index + 1 (your rule 1)
if start_idx >= vec.shape[0]-1: # but if it is larger or equal, fit it back for next column use
start_idx = vec.shape[0]-1
The results should be:
>>>lst
[0, 4, 5, 7, 7]
# code change to b.T
>>>lst
[3, 4, 5, 6, 7]
so first of all here is my code I have questions about, I feel like the answer will be simple, but I am just too blind to see it
dic = {}
for i in range(0,9):
dic['rand_num{0}'.format(i)] = np.sort(random.sample(range(0,8),5))
mat = np.zeros([8,8])
for rand_num in dic.values():
print(rand_num)
for i in range(0,5):
matrixval = rand_num[i]
#print(matrixval)
for j in range(0,8):
mat[matrixval,j] = 1
print(mat)
I created 8 different lists which will help me determine, where to set a 1 in the matrix.
so eg. if my first list looks like this:
rand_List = [0,3,5,6,7]
the matrix of the first row or column should look like this
matrix = [1, 0, 0, 1, 0, 1, 1, 1]
My position for the matrix value is set in the second loop: for i in range(0,5): matrixval = rand_num[i] . However, as soon I run my code, my matrix just gives me ones instead of ones and zeros. I feel like I have an iteration problem in my last for loop, and I don't know how to fix it.
If anyone could help me, or just give me a hint, how to actually solve this hopefully small mistake of mine, I would really appreciate it.
Hopefully this provides the expected result:
dic = {}
for i in range(0,8):
dic['rand_num{0}'.format(i)] = np.sort(random.sample(range(0,8),5))
mat = np.zeros([8,8])
for j, rand_num in enumerate(dic.values()):
print(rand_num)
for i in range(0,5):
matrixval = rand_num[i]
mat[j,matrixval] = 1
print(mat)
Output:
[1 2 3 5 7]
[0 1 2 5 6]
[0 1 2 3 5]
[1 2 3 4 6]
[0 1 3 4 7]
[1 2 4 5 7]
[0 1 3 4 7]
[1 2 3 4 6]
[[0. 1. 1. 1. 0. 1. 0. 1.]
[1. 1. 1. 0. 0. 1. 1. 0.]
[1. 1. 1. 1. 0. 1. 0. 0.]
[0. 1. 1. 1. 1. 0. 1. 0.]
[1. 1. 0. 1. 1. 0. 0. 1.]
[0. 1. 1. 0. 1. 1. 0. 1.]
[1. 1. 0. 1. 1. 0. 0. 1.]
[0. 1. 1. 1. 1. 0. 1. 0.]]
The dic you are using is not needed. Here is a solution without it.
import random
lists = []
for i in range(0, 9):
lists.append(sorted(random.sample(range(0, 8), 5)))
mat = []
for lst in lists:
arr = [0] * 8
for index in lst:
arr[index] = 1
mat.append(arr)
print(mat)
Outputs:
[[0, 1, 1, 0, 0, 1, 1, 1],
[1, 0, 1, 1, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 1],
[0, 1, 1, 1, 0, 1, 1, 0],
[1, 1, 0, 1, 1, 0, 1, 0],
[1, 0, 1, 1, 0, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 1, 0],
[1, 0, 1, 0, 1, 0, 1, 1]]
You can also use numpy.random.randint to generate your binary 2d array.
import numpy as np
arr = np.random.randint(2, size=(8, 8))
print(arr)
Outputs:
[[1 0 1 0 1 0 1 1]
[0 0 0 0 1 0 1 1]
[1 0 1 1 0 0 1 1]
[1 1 0 1 0 1 1 0]
[0 0 1 0 0 1 1 1]
[1 0 0 0 1 1 1 1]
[1 1 0 1 0 0 1 1]
[1 0 1 0 0 1 0 0]]
I have an array n of count data, and I want to transform it into a matrix x in which each row contains a number of ones equal to the corresponding count number, padded by zeroes, e.g:
n = [0 1 3 0 1]
x = [[ 0. 0. 0.]
[ 1. 0. 0.]
[ 1. 1. 1.]
[ 0. 0. 0.]
[ 1. 0. 0.]]
My solution is the following, and is very slow. Is it possible to do better?
n = np.random.poisson(2,5)
max_n = max(n)
def f(y):
return np.concatenate((np.ones(y), np.zeros(max_n-y)))
x = np.vstack(map(f,n))
Here's one way to vectorize it:
>>> n = np.array([0,2,1,0,3])
>>> width = 4
>>> (np.arange(width) < n[:,None]).astype(int)
array([[0, 0, 0, 0],
[1, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 0]])
where if you liked, width could be max(n) or anything else you chose.
import numpy as np
n = np.array([0, 1, 3, 0, 1])
max_n = max(n)
np.vstack(n > i for i in range(max_n)).T.astype(int) # xrange(max_n) for python 2.x
Output:
array([[0, 0, 0],
[1, 0, 0],
[1, 1, 1],
[0, 0, 0],
[1, 0, 0]])
I have a 2d-Numpy array containing basically a label-value pair. I have combined several of these matricies, but I'm hoping to round the label to 4 decimal places and sum the values, such that:
[[70.00103, 1],
[70.02474, 1],
[70.02474, 1],
[70.024751, 1],
[71.009100, 1],
[79.0152, 1],
[79.0152633, 1],
[79.0152634, 1]]
becomes
[[70.001, 1],
[70.0247, 2],
[70.0248, 1],
[71.0091, 1],
[79.0152, 1],
[79.0153, 2]]
Any thoughts on how one might accomplish this in a speedy manner, using either numpy or pandas? Thanks!
In [10]:
import numpy as np
x=np.array([[70.00103, 1],[70.02474, 1],[70.02474, 1],[70.024751, 1],[71.009100, 1],[79.0152, 1],[79.0152633, 1],[79.0152634,1]])
x[:,0]=x[:,0].round(4)
x
Out[10]:
array([[ 70.001 , 1. ],
[ 70.0247, 1. ],
[ 70.0247, 1. ],
[ 70.0248, 1. ],
[ 71.0091, 1. ],
[ 79.0152, 1. ],
[ 79.0153, 1. ],
[ 79.0153, 1. ]])
In [14]:
import pandas as pd
pd.DataFrame(x).groupby(0).sum()
Out[14]:
70.0010 1
70.0247 2
70.0248 1
71.0091 1
79.0152 1
79.0153 2
It's what that np.around is for :
>>> A=np.array([[70.00103, 1],
... [70.02474, 1],
... [70.02474, 1],
... [70.024751, 1],
... [71.009100, 1],
... [79.0152, 1],
... [79.0152633, 1],
... [79.0152634, 1]])
>>>
>>> np.around(A, decimals=4)
array([[ 70.001 , 1. ],
[ 70.0247, 1. ],
[ 70.0247, 1. ],
[ 70.0248, 1. ],
[ 71.0091, 1. ],
[ 79.0152, 1. ],
[ 79.0153, 1. ],
[ 79.0153, 1. ]])
I have 2 arrays containing zeros & ones. I want to perform hstack() on them but not getting the desired output.
Python Code..
import numpy as np
zeros = np.zeros(8)
ones = np.ones(8)
zerosThenOnes = np.hstack((zeros, ones)) # A 1 by 16 array
Current Output..
[ 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 1. 1.]
Expected Output..
[ 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 ]
I can't understand what silly mistake I'm doing.
You must tell numpy to return the values as integers
import numpy as np
zeros = np.zeros((8,), dtype=np.int)
ones = np.ones((8,), dtype=np.int)
zerosThenOnes = np.hstack((zeros, ones))
To print out zerosThenOnes like this [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
Use:
print([x for x in zerosThenOnes])
Numpy Zeros
np.hstack((np.zeros(8), np.ones(8))).astype(int)
for np.array output, or
map( int, np.hstack((np.zeros(8), np.ones(8))) )
for list output