Random positioning of pawns on chessboard - python

I want to (pseudo) randomly position a number points on a grid. Think of it as 10 x 10 chessboard with 100 squares) And think of these points as pawns on the chessboard, that occupy one square each. What I want is for the pawns to be "evenly" distributed over the board. The middle-square method is fine for generating the random numbers.
00 01 02 03 04 05 06 07 08 09
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 ...
My problem that is that if I store the grid squares as list, and draw them from top left to bottom right, then square 9 is not next to 10, as you would expect. 10, is next to 0 & 20. What's the best way around this?

Element numbering in lists starts with zero, not with 1. So your first square has number 0 (you do see it, right?) and the 10th square has number 9. That is why the 1st square in the 2nd row has number 10 and not 11. It is because 0 + 10 = 10. If you want to print the numbers starting from 1 you can do it adding 1 to a square number in print:
print (square[number] + 1)
As I do not see your code I cannot provide a better code example which would perfectly pass to your code.

Just because you store the final squares as a list, that doesn't mean you have to generate random numbers in a linear range. If you know you have, say, a 100x100 grid, then choose points by choosing X and Y coordinates separately, and then fill in square number 100 * X + Y.
If you want the points distributed randomly, then just make sure you choose both X and Y from a uniform random distribution 0..99. If you want the points distributed evenly, use something like a Halton sequence.

Related

Plotting of dot points based on np.where condition

I have a lot of data points (in .CSV form) that I am trying to visualize, what I would like to do is to read the csv and read the "result" column, if the value in the corresponding column is positive(I was trying to use np.where condition), I would like to plot the A B C D E F G parameters corresponding to it in such a way that the y-axis is the value of the parameters and x-axis is the name of the parameter.(Something like a dot/scatter plot) I would like to plot all the values in the same graph, Furthermore, if the number of points are more than 20 I would like to use the first 20 points for the plotting.
An example of the type of dataset is below. (Mine contains around 12000 rows)
A B C D E F G result
23 -54 36 27 98 39 80 -0.86
14 44 -16 47 28 29 26 1.65
67 84 26 67 -88 29 10 0.5
-45 14 76 37 68 59 90 0
24 34 56 27 38 79 48 -1.65
Any help in guiding for this would be appreciated !
From your question I assume that your data is a pandas dataframe. In this case you can do the selection with pandas and use its built-in plotting function:
df.loc[df.result>0, df.columns[:-1]].T.plot(ls='', marker='o')
If you want to plot the first 20 rows only, just add [:20] (or better .iloc[:20]) to df.loc.

how to sequentially assign two numbers in an array?

I try to assign two numbers diagonally to each other in the matrix according to certain procedures.
At first the first 1st number in the penultimate line of the line with the 2nd number in the last line, then the first number in the line up with the 2nd number in the penultimate line, etc..This sequence is shown in the example below. The matrix does not always have to be the same size.
Example
a=np.array([[11,12,13],
[21,22,23],
[31,32,33]])
required output:
21 32
11 22
11 33
22 33
12 23
or
a=np.array([[11,12,13,14],
[21,22,23,24],
[31,32,33,34],
[41,42,43,44]])
required output:
31 42
21 32
21 43
32 43
11 22
11 33
11 44
22 33
22 44
12 23
12 34
23 34
13 24
It is possible?
Here's an iterative solution, assuming a square matrix. Modifying this for non-square matrices shouldn't be hard.
import numpy as np
a=np.array([[11,12,13,14],
[21,22,23,24],
[31,32,33,34],
[41,42,43,44]])
w,h = a.shape
for y0 in range(1,h):
y = h-y0-1
for x in range(h-y-1):
print( a[y+x,x], a[y+x+1,x+1] )
for x in range(1,w-1):
for y in range(w-x-1):
print( a[y,x+y], a[y+1,x+y+1] )

How do I sort columns of numerical file data in python

I'm trying to write a piece of code in python to graph some data from a tab separated file with numerical data.
I'm very new to Python so I would appreciate it if any help could be dumbed down a little bit.
Basically, I have this file and I would like to take two columns from it, sort them each in ascending order, and then graph those sorted columns against each other.
First of all, you should not put code as images, since there is a functionality to insert and format here in the editor.
It's as simple as calling x.sort() and y.sort() since both of them are slices from data so that should work fine (assuming they are 1 dimensional arrays).
Here is an example:
import numpy as np
array = np.random.randint(0,100, size=50)
print(array)
Output:
[89 47 4 10 29 21 91 95 32 12 97 66 59 70 20 20 36 79 23 4]
So if we use the method mentioned before:
print(array.sort())
Output:
[ 4 4 10 12 20 20 21 23 29 32 36 47 59 66 70 79 89 91 95 97]
Easy as that :)

Select/Group rows from a data frame with the nearest values for a specific column(s)

I have the two columns in a data frame (you can see a sample down below)
Usually in columns A & B I get 10 to 12 rows with similar values.
So for example: from index 1 to 10 and then from index 11 to 21.
I would like to group these values and get the mean and standard deviation of each group.
I found this following line code where I can get the index of the nearest value. but I don't know how to do this repetitively:
Index = df['A'].sub(df['A'][0]).abs().idxmin()
Anyone has any ideas on how to approach this problem?
A B
1 3652.194531 -1859.805238
2 3739.026566 -1881.965576
3 3742.095325 -1878.707674
4 3747.016899 -1878.728626
5 3746.214554 -1881.270329
6 3750.325368 -1882.915532
7 3748.086576 -1882.406672
8 3751.786422 -1886.489485
9 3755.448968 -1885.695822
10 3753.714126 -1883.504098
11 -337.969554 24.070990
12 -343.019575 23.438956
13 -344.788697 22.250254
14 -346.433460 21.912217
15 -343.228579 22.178519
16 -345.722368 23.037441
17 -345.923108 23.317620
18 -345.526633 21.416528
19 -347.555162 21.315934
20 -347.229210 21.565183
21 -344.575181 22.963298
22 23.611677 -8.499528
23 26.320500 -8.744512
24 24.374874 -10.717384
25 25.885272 -8.982414
26 24.448127 -9.002646
27 23.808744 -9.568390
28 24.717935 -8.491659
29 25.811393 -8.773649
30 25.084683 -8.245354
31 25.345618 -7.508419
32 23.286342 -10.695104
33 -3184.426285 -2533.374402
34 -3209.584366 -2553.310934
35 -3210.898611 -2555.938332
36 -3214.234899 -2558.244347
37 -3216.453616 -2561.863807
38 -3219.326197 -2558.739058
39 -3214.893325 -2560.505207
40 -3194.421934 -2550.186647
41 -3219.728445 -2562.472566
42 -3217.630380 -2562.132186
43 234.800448 -75.157523
44 236.661235 -72.617806
45 238.300501 -71.963103
46 239.127539 -72.797922
47 232.305335 -70.634125
48 238.452197 -73.914015
49 239.091210 -71.035163
50 239.855953 -73.961841
51 238.936811 -73.887023
52 238.621490 -73.171441
53 240.771812 -73.847028
54 -16.798565 4.421919
55 -15.952454 3.911043
56 -14.337879 4.236691
57 -17.465204 3.610884
58 -17.270147 4.407737
59 -15.347879 3.256489
60 -18.197750 3.906086
A simpler approach consist in grouping the values where the percentage change is not greater than a given threshold (let's say 0.5):
df['Group'] = (df.A.pct_change().abs()>0.5).cumsum()
df.groupby('Group').agg(['mean', 'std'])
Output:
A B
mean std mean std
Group
0 3738.590934 30.769420 -1880.148905 7.582856
1 -344.724684 2.666137 22.496995 0.921008
2 24.790470 0.994361 -9.020824 0.977809
3 -3210.159806 11.646589 -2555.676749 8.810481
4 237.902230 2.439297 -72.998817 1.366350
5 -16.481411 1.341379 3.964407 0.430576
Note: I have only used the "A" column, since the "B" column appears to follow the same pattern of consecutive nearest values. You can check if the identified groups are the same between columns with:
grps = (df[['A','B']].pct_change().abs()>1).cumsum()
grps.A.eq(grps.B).all()
I would say that if you know the length of each group/index set you want then you can first subset the column and row with :
df['A'].iloc[0:11].mean()
Then figure out a way to find standard deviation.

Extract data from line and add to specific numbered boxes

Sorry, but I need your help if possible. I am a complete beginner in Python and I am completely stuck. I would like to know what would be the first steps to solve my problem.
I have (many pages) of lines with the following structure:
pg10_65 * 3.2200 * 22 24 28 30 33 34 36 37
pg10_116 * 3.2420 * 24 28 30 33 34 37
pg10_118 * 3.1500 * 19 24 28 30 33 34 36
pg10_120 * 3.1230 * 24 28 30 33 34 36 37
pg74_32 * 3.0350 * 17 28 30 33 34 36 37 38
For each line and in between the * symbols I have a value (digit dot four decimals) and after the last * symbol I have a series of numbers, from 1 to 68 but not all of them.
I have 68 boxes.
In this example, and for the first line, I want to add 3.2200 to boxes 22, 24, ..., 36, 37. If there is a 0 add 3.2200 to 0, if there is another value, add to that value.
For the second line, I want to add the values 3.2420 to boxes 24, 28, ..., 34, 37. If there is a 0 add to 0, if there is another value, add 3.2420 to that value.
And so on for each of the lines.
In the end I would have 60 boxes with all values corresponding to that boxes added.
I am completely stuck on this.
Thanks a lot to everyone for your advice.
José
I came up with a solution to your problem.
def add_to_boxes(input_string, all_boxes):
'''
Takes a list of boxes and your string input and does the additions you requested
'''
_, float_value, box_list = input_string.split('*')
float_value = float(float_value) #convert from string to float
box_list = [int (s) for s in box_list.split(' ')[1:]] #convert from string to list of integers
print("Adding ", float(float_value), " to ", box_list)
for box in box_list:
all_boxes[box-1] += float_value #boxes are numbered from 0 to 67
boxes = [0 for i in range(68)] # 68 boxes with the value 0
input_str = "pg10_65 * 3.2200 * 22 24 28 30 33 34 36 37"
add_to_boxes(input_str, boxes)
print(boxes)
Please let me know if you have any questions regarding how it works.

Categories