How can I save file by using savetxt in the python? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 17 days ago.
Improve this question
I have lists of data such as
a = [1,2,3,4,5]
b = [6,7,8,9,0]
c = [0,0,0,0,0]
I want to save this data into a file in a form of
1 6 0
2 7 0
3 8 0
4 9 0
5 0 0
I can do this by using "open". But I want to go with savetxt.
How can I save the data into the above form by using savetxt?

A.T
import numpy as np
a = [1,2,3,4,5]
b = [6,7,8,9,0]
c = [0,0,0,0,0]
A = np.array([a,b,c])
B = A.T
np.savetxt("./demo1",B,fmt="%d")
we can use numpy let a,b,c become a
3x3 matrix
and use the transpose way of numpy
A:
[[1 2 3 4 5]
[6 7 8 9 0]
[0 0 0 0 0]]
A.T
[[1 6 0]
[2 7 0]
[3 8 0]
[4 9 0]
[5 0 0]]
then we can use
np.savetxt("./demo1",B,fmt="%d")
"./demo1": file name
B: data
fmt="%d": format

import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.array([6, 7, 8, 9, 0])
c = np.array([0, 0, 0, 0, 0])
all_data = np.vstack([a, b, c])
np.savetxt("data.txt", all_data.T, fmt="%d")
all_data stack the arrays rows by rows, so to print the data the way you want it you need to print the transpose, so all_data.T
You also need to format the output, so %d

Related

What is a good way to implement a function to make an array with inputted arguments? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I would like to make a function that accepts any number of arguments and returns an array using those arguments as parameters. Here's the code example - I would like to do something like this, except it should work:
import numpy as np
def getgoodarray(*args):
goodarray = np.round(np.arange(args)*10)
return goodarray
B = getgoodarray(2, 3) # this should make a 2d array, with two 1dimensional arrays, each of 3 elements
Do you have any idea on how this function could be implemented properly?
Use any method that builds an array, fill with zeros, ones or random
def getgoodarray(*args):
return np.ones(args)
# return np.zeros(args)
# return np.random.randint(0, 10, args)
x = getgoodarray(2, 3)
[[7 7 1]
[8 2 5]]
x = getgoodarray(2, 2, 2, 3)
[[[[6 8 3]
[7 0 0]]
[[5 8 9]
[6 8 5]]]
[[[6 0 5]
[8 7 0]]
[[3 4 3]
[0 2 4]]]]

Add two arrays with holes to fill the holes in the other array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have scans of data from Africa that are from the same day at different times. I'd like to consolidate the data into one one array. How can I do that?
np.add(x,y, where=some possbile function?)
Edit: In case it's unclear. I would like to have on image with all of the yellow from the two images.
You can simply use np.concatenate((x,y),axis=1) to consolidate data into one array.
axis=1 for column-wise concatenation and axis=0 for row-wise concatenation
Example with column-wise
x = np.array(([1,2,3],[4,5,6],[7,8,9]))
y = np.array(([10,11,12],[13,14,15],[16,17,18]))
result = np.concatenate((x,y),axis=1)
Result
[[ 1 2 3 10 11 12]
[ 4 5 6 13 14 15]
[ 7 8 9 16 17 18]]
Example with row-wise
x = np.array(([1,2,3],[4,5,6],[7,8,9]))
y = np.array(([10,11,12],[13,14,15],[16,17,18]))
result = np.concatenate((x,y),axis=0)
Result
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]
[13 14 15]
[16 17 18]]
If I understand you right you have one array with values and you want to put the other data in the same array below? Maybe np.vstack is the code you are looking for? The relevant part would be:
>>> a = np.array([1, 2, 3])
>>> b = np.array([4, 5, 6])
>>> np.vstack((a,b))
array([[1, 2, 3],
[4, 5, 6]])
I figured out a solution. You can use another array in the np.where() function. So the data here is split up in am and pm passes
both = np.where(data_am < 0, data_pm ,data_am)
combined = np.where(both < 0, 0 ,both)

Mean values inside matrix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Suppose we have a 6x6 matrix (numpy ndarray) like:
[1 1 2 2 3 3
1 1 2 2 3 3
4 4 5 5 6 6
4 4 5 5 6 6
7 7 8 8 9 9
7 7 8 8 9 9]
How would I calculate the mean value of each of the 2x2 submatrices containing the same number, and get as an output a 3x3 matrix?
Not sure whether this is what you want
import numpy as np
x = np.arange(1,10).reshape(3,3)
y = np.ones((2,2),dtype=np.int32)
z = np.kron(x,y)
z
def meanpool2d(x):
h,w = x.shape
out = np.zeros((h//2,w//2))
for i in range(h//2):
for j in range(w//2):
out[i][j] = np.mean(x[2*i:2*i+2,2*j:2*j+2])
return out
meanpool2d(z)
Construct the input array
import numpy as np
x = np.arange(1,10).reshape(3,3)
x = np.repeat(x, 2, 0)
x = np.repeat(x, 2, 1)
print(x)
Out:
[[1 1 2 2 3 3]
[1 1 2 2 3 3]
[4 4 5 5 6 6]
[4 4 5 5 6 6]
[7 7 8 8 9 9]
[7 7 8 8 9 9]]
Compute the means of all 2x2 blocks
m = (x.reshape(3, 2, -1, 2)
.swapaxes(1,2)
.mean((2,3)))
print(m)
Out:
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]

(Inverse-) Sorting 2d numpy array column-wise

The following code sorts an 2d numpy array column-wise forth and back
import numpy as np
#Column-wise sort and inverse sort of image (2d array)
nrows = 10
ncols = 5
a = np.random.randint(nrows, size=(nrows, ncols))
a_sorted = np.sort(a, axis=0)
ori_indices = np.zeros_like(a)
for c in range(ncols):
ori_indices[:,c] = np.argsort(np.argsort(a[:,c]))
#Do some work on sorted array, like e.g row-wise filtering
#After processing sorted array, move it back to original order
a_backsorted = np.zeros_like(a)
for c in range(ncols):
a_backsorted[:,c] = a_sorted[:,c][ori_indices[:,c]]
print (a); print ()
print (a_backsorted); print ()
print (a_sorted); print ()
The code work as is but I guess there is a more efficient implementation without for loop (using fancy indexing)
You can try a_sorted[::-1] to reverse the array
print (a_sorted); print ()
print (a_sorted[::-1])
[[0 0 0 2 0]
[2 0 0 2 2]
[4 0 2 6 4]
[4 2 3 7 5]
[4 4 4 7 6]
[5 5 4 8 7]
[6 5 4 8 7]
[7 6 8 9 8]
[8 7 9 9 9]
[8 8 9 9 9]]
[[8 8 9 9 9]
[8 7 9 9 9]
[7 6 8 9 8]
[6 5 4 8 7]
[5 5 4 8 7]
[4 4 4 7 6]
[4 2 3 7 5]
[4 0 2 6 4]
[2 0 0 2 2]
[0 0 0 2 0]]
#Column-wise sort and inverse sort of image (2d array)
import numpy as np
#Define random array and sort it
nrows = 10
ncols = 5
a = np.random.randint(nrows, size=(nrows, ncols))
a_sorted = np.sort(a, axis=0)
#Save original order of columns
ori_indices = np.argsort(np.argsort(a, axis=0), axis=0)
#Do some work on sorted array, like e.g row-wise filtering.
#....
#After processing sorted array, move it back to original order:
c=np.array([[i] for i in range(ncols)]).T
a_backsorted = a_sorted[ori_indices, c]
#Check results
print (a); print ()
print (a_backsorted); print ()
print (a_sorted); print ()
import numpy as np
nrows = 10; ncols = 5
a = np.random.randint(nrows, size=(nrows, ncols))
a_sorted = np.sort(a, axis=0)
a_backsorted = np.zeros_like(a)
c = np.array([[i] for i in range(ncols)]).T
a_backsorted[np.argsort(a, axis=0), c] = a_sorted
The reverting of the column-wise sorting is done by inserting the values of the sorted array at the argsorted positions in the backsorted array. Since this is done columnwise, the argsorted positions are paired with the columns represented in the c array

repeat indices with different repeat values in numpy

I'm looking for an efficient way to do the following with Numpy:
Given a array counts of positive integers containing for instance:
[3, 1, 0, 6, 3, 2]
I would like to generate another array containing the indices of the first one, where the index i is repeated counts[i] times:
[0 0 0 1 3 3 3 3 3 3 4 4 4 5 5]
My problem is that this array is potentially very large and I'm looking for a vectorial (or fast) way to do this.
You can do it with numpy.repeat:
import numpy as np
arr = np.array([3, 1, 0, 6, 3, 2])
repix = np.repeat(np.arange(arr.size), arr)
print(repix)
Output:
[0 0 0 1 3 3 3 3 3 3 4 4 4 5 5]

Categories