How to shuffle rows of 3 dimensional numpy array? - python-2.7

I have 3 dimensional array having 3 rows(sample), 2 columns and 4 features. I want to shuffle three sample. But the following command gives error that only size 1 arrays can be converted to python lists. How can I do that?
`x= np.arange(3*2*4).reshape(3,2,4)
perm = np.arange(x[0])`

As the other answer has mentioned, you need to use shuffle, and it doesn't touch anything other than the first axis (your samples) according to the documentation.
This function only shuffles the array along the first axis of a multi-dimensional array.

Let's take the following example:
x = np.arange(2 * 3).reshape([3, 2])
perm = np.arange(x.shape[0])
As well you can print x.shape and see the result tuple (3, 2), so its zero's element is 3.

Related

how do I add 3 points randomly to a list?

I have a list below community_card_suit, and I want to randomly add 3 points to any of the indexes, so for example the outcome looks like [0,3,0,0] or [0,1,1,1] or [1,2,1,0], etc.
community_cards_suit = [0,0,0,0]
Any idea how I would do this randomly?
Thanks

how python print me each element when i use random?

I need a list of 8 elements and i want that python randomly import that elements in a list. But i need each of them elements in a list. Like this:
I need numbers of 0 to 4 in a list, but if I write:
s = []
for i in range(8):
s.append(random.randint(0,4))
print("s:", s)
python doesn't print me each of number at least once. Python print me like that:
s = [1,0,2,2,1,0,1,3]- in this list is 4 missing but i want all 5 numbers at least once in a list.
Please help me.
If you want to have a list of eight items, consisting of at least one each of the elements 0,1,2,3,4, then what you really want is a list of [0,1,2,3,4] and three additional random elements, all in random order:
import random
# start a list with one each of the desired elements
s = [0,1,2,3,4]
# add three more elements
for i in range(3):
s.append(random.randint(0,4))
# randomize the order of the elements in the list
random.shuffle(s)
print("s:", s)

How to use the dimension of a python matrix in a loop

I am working with a matrix, lets call it X, in python.
I know how to get the dimension of the matrix using X.shape but I am interested specially on using the number of rows of the matrix in a for loop, and I dont know how to get this value in a datatype suitable for a loop.
For example, imagine tihs simple situation:
a = np.matrix([[1,2,3],[4,5,6]])
for i in 1:(number of rows of a)
print i
How can I get automatically that "number of rows of a"?
X.shape[0] == number of rows in X
A superficial search on numpy will lead you to shape. It returns a tuple of array dimensions.
In your case, the first dimension (axe) concerns the columns. You can access it as you access a tuple's element:
import numpy as np
a = np.matrix([[1,2,3],[4,5,6]])
# a. shape[1]: columns
for i in range(0,a.shape[1]):
print 'column '+format(i)
# a. shape[0]: rows
for i in range(0, a.shape[0]):
print 'row '+format(i)
This will print:
column 0
column 1
column 2
row 0
row 1

Prolog List Neighbour of a Element

I am having problems with list of prolog. I want to make this:
[1,2,3,4,5]
[5,6,9,12,10]
You take a number for example 3, and you do a plus operation with the neighbours so the operation is 2+3+4 = 9. For the first and the last element you pretend there is an imaginary 1 there.
I have this now:
sum_list([A,X,B|T], [Xs|Ts]):-
add(A,X,B,Xs),
sum_list([X,B|T], Ts).
I haven't consider the first and the last element. My problem is I don't know how to get the element before and the next and then how to move on.
Note: I not allow to use meta-predicates.
Thanks.
I'm not sure how you calculated the first 5. The last 10 would be 4 + 5 + implicit 1. But following that calculation, the first element of your result should be 4 instead of 5?
Anyways, that doesn't really matter in terms of writing this code. You are actually close to your desired result. There are of course multiple ways of tackling this problem, but I think the simplest one would be to write a small 'initial' case in which you already calculate the first sum and afterwards recursively calculate all of the other sums. We can then write a case in which only 2 elements are left to calculate the last 'special' sum:
% Initial case for easily distinguishing the first sum
initial([X,Y|T],[Sum|R]) :-
Sum is X+Y+1,
others([X,Y|T],R).
% Match on 2 last elements left
others([X,Y],[Sum|[]]) :-
Sum is X+Y+1.
% Recursively keep adding neighbours
others([X,Y,Z|T],[Sum|R]) :-
Sum is X+Y+Z,
others([Y,Z|T],R).
Execution:
?- initial([1,2],Result)
Result = [4,4]
?- initial([1,2,3,4,5],Result)
Result = [4, 6, 9, 12, 10]
Note that we now don't have any cases (yet) for an empty list or a list with just one element in it. This still needs to be covered if necessary.

Summing the first value in each tuple in a list over a specific range in Python

My question is based on an earlier question and I can't find to figure out (nor find) the subsequent step I need.
Say I do have the same list of tuples, namely:
[(0, 1), (2, 3), (5, 7), (2, 1)]
Also I wish to find the sum of the first values in each pair, which could be done by the simple pythonic:
sum([pair[0] for pair in list_of_pairs])
as provided by David Z. However, in my case I only wish to sum from the first first value up until a following first value, say the first value at index N = 2.
Thus, only calculate the sum of:
0 + 2 + 5
I have been trying things like:
sum([pair[0] for pair in list_of_pairs[:N])
but without success. Can anyone provide me with an elegant solution?
you were very close to the solution
This works for me :
N=2
sum([pair[0] for pair in list_of_pairs[0:N+1]])
If you wants from middle then you can do this :
N=3
M=1
print sum([pair[0] for pair in list_of_pairs[M:N+1]])