I am trying to fit a sinc function with gnuplot but it fails with the message:
'Undefined value during function evaluation'.
First my data:
27 9.3
27.2 9.3
27.8 9.3
29 9.4
32 9.5
34 9.6
34.2 9.7
34.4 9.7
34.6 9.8
34.8 10.1
35 10.9
35.2 12.9
35.4 16.1
35.6 21.1
35.8 26.5
36 31.8
36.2 34.7
36.4 36.6
36.6 36.3
36.8 32.3
37 26.4
37.2 20.6
37.4 15.4
37.6 11.6
37.8 9.9
38 9.6
38.5 10
39 9.5
39.5 9.5
40 9.6
What I am trying to do in Gnuplot:
sinc(x)=sin(pi*x)/pi/x
f(x)=a*(sinc((b*(x-c))))**2+d
fit f(x) '4_temp.txt' via a,b,c,d
I set a,b,c,d close to the values that are needed (see picture) but it wont fit.
Somebody can help?
Thanks in advance.
I can reproduce your error message. You are trying to fit a sin(x)/x function. For x=0 you will get 0/0, although, gnuplot has no problems to plot sin(x)/x, apparently, fitting has a problem with this.
Only if you add a little offset, e.g. 1e-9, it seems to work and it will find some reasonable parameters.
As #Ethan says, you need to choose some starting values which should not be too far away from the final values.
You will get the fitted values:
Final set of parameters Asymptotic Standard Error
======================= ==========================
a = 27.5271 +/- 0.2822 (1.025%)
b = 0.608263 +/- 0.006576 (1.081%)
c = 36.3954 +/- 0.00657 (0.01805%)
d = 9.21346 +/- 0.127 (1.379%)
Code:
### fitting type of sin(x)/x function
reset session
$Data <<EOD
27 9.3
27.2 9.3
27.8 9.3
29 9.4
32 9.5
34 9.6
34.2 9.7
34.4 9.7
34.6 9.8
34.8 10.1
35 10.9
35.2 12.9
35.4 16.1
35.6 21.1
35.8 26.5
36 31.8
36.2 34.7
36.4 36.6
36.6 36.3
36.8 32.3
37 26.4
37.2 20.6
37.4 15.4
37.6 11.6
37.8 9.9
38 9.6
38.5 10
39 9.5
39.5 9.5
40 9.6
EOD
a=25
b=1
c=36
d=10
sinc(x)=sin(pi*x)/pi/(x)
f(x)=a*(sinc((b*(x-c+1e-9))))**2+d
set fit nolog
fit f(x) $Data via a,b,c,d
plot $Data u 1:2 w p pt 7, f(x) w l lc rgb "red"
### end of code
Result:
I have two data frames storing tracking data of offensive and defensive players during an nfl game. My goal is to calculate the maximum distance between an offensive player and the nearest defender during the course of the play.
As a simple example, I've made up some data where there are only three offensive players and two defensive players. Here is the data:
Defense
GameTime PlayId PlayerId x-coord y-coord
0 1 1 117 20.2 20.0
1 2 1 117 21.0 19.1
2 3 1 117 21.3 18.3
3 4 1 117 22.0 17.5
4 5 1 117 22.5 17.2
5 6 1 117 23.0 16.9
6 7 1 117 23.6 16.7
7 8 2 117 25.1 34.1
8 9 2 117 25.9 34.2
9 10 2 117 24.1 34.5
10 11 2 117 22.7 34.2
11 12 2 117 21.5 34.5
12 13 2 117 21.1 37.3
13 14 3 117 21.2 44.3
14 15 3 117 20.4 44.6
15 16 3 117 21.9 42.7
16 17 3 117 21.1 41.9
17 18 3 117 20.1 41.7
18 19 3 117 20.1 41.3
19 1 1 555 40.1 17.0
20 2 1 555 40.7 18.3
21 3 1 555 41.0 19.6
22 4 1 555 41.5 18.4
23 5 1 555 42.6 18.4
24 6 1 555 43.8 18.0
25 7 1 555 44.2 15.8
26 8 2 555 41.2 37.1
27 9 2 555 42.3 36.5
28 10 2 555 45.6 36.3
29 11 2 555 47.9 35.6
30 12 2 555 47.4 31.3
31 13 2 555 46.8 31.5
32 14 3 555 47.3 40.3
33 15 3 555 47.2 40.6
34 16 3 555 44.5 40.8
35 17 3 555 46.5 41.0
36 18 3 555 47.6 41.4
37 19 3 555 47.6 41.5
Offense
GameTime PlayId PlayerId x-coord y-coord
0 1 1 751 30.2 15.0
1 2 1 751 31.0 15.1
2 3 1 751 31.3 15.3
3 4 1 751 32.0 15.5
4 5 1 751 31.5 15.7
5 6 1 751 33.0 15.9
6 7 1 751 32.6 15.7
7 8 2 751 51.1 30.1
8 9 2 751 51.9 30.2
9 10 2 751 51.1 30.5
10 11 2 751 49.7 30.6
11 12 2 751 49.5 30.9
12 13 2 751 49.1 31.3
13 14 3 751 12.2 40.3
14 15 3 751 12.4 40.5
15 16 3 751 12.9 40.7
16 17 3 751 13.1 40.9
17 18 3 751 13.1 41.1
18 19 3 751 13.1 41.3
19 1 1 419 41.3 15.0
20 2 1 419 41.7 15.3
21 3 1 419 41.8 15.4
22 4 1 419 42.9 15.6
23 5 1 419 42.6 15.6
24 6 1 419 44.8 16.0
25 7 1 419 45.2 15.8
26 8 2 419 62.2 30.1
27 9 2 419 63.3 30.5
28 10 2 419 62.6 31.0
29 11 2 419 63.9 30.6
30 12 2 419 67.4 31.3
31 13 2 419 66.8 31.5
32 14 3 419 30.3 40.3
33 15 3 419 30.2 40.6
34 16 3 419 30.5 40.8
35 17 3 419 30.5 41.0
36 18 3 419 31.6 41.4
37 19 3 419 31.6 41.5
38 1 1 989 10.1 15.0
39 2 1 989 10.2 15.5
40 3 1 989 10.4 15.4
41 4 1 989 10.5 15.8
42 5 1 989 10.6 15.9
43 6 1 989 10.1 15.5
44 7 1 989 10.9 15.3
45 8 2 989 25.8 30.1
46 9 2 989 25.2 30.1
47 10 2 989 21.8 30.2
48 11 2 989 25.8 30.2
49 12 2 989 25.6 30.5
50 13 2 989 25.5 31.0
51 14 3 989 50.3 40.3
52 15 3 989 50.3 40.2
53 16 3 989 50.2 40.4
54 17 3 989 50.1 40.8
55 18 3 989 50.6 41.2
56 19 3 989 51.4 41.6
The data is essentially multidimensional with GameTime, PlayId, and PlayerId as independent variables and x-coord and y-coord as dependent variables. How can I go about calculating the maximum distance from the nearest defender during the course of a play?
My guess is I would have to create columns containing the distance from each defender for each offensive player, but I don't know how to name those and be able to account for an unknown amount of defensive/offensive players (the full data set contains thousands of players).
Here is a possible solution , I think there is a way to making it more efficient :
Assuming you have a dataframe called offense_df and a dataframe called defense_df:
In the merged dataframe you'll get the answer to your question, basically it will create the following dataframe:
from scipy.spatial import distance
merged_dataframe = pd.merge(offense_df,defense_df,on=['GameTime','PlayId'],suffixes=('_off','_def'))
GameTime PlayId PlayerId_off x-coord_off y-coord_off PlayerId_def x-coord_def y-coord_def
0 1 1 751 30.2 15.0 117 20.2 20.0
1 1 1 751 30.2 15.0 555 40.1 17.0
2 1 1 419 41.3 15.0 117 20.2 20.0
3 1 1 419 41.3 15.0 555 40.1 17.0
4 1 1 989 10.1 15.0 117 20.2 20.0
The next two lines are here to create a unique column for the coordinates , basically it will create for the offender (coord_off) and the defender a column (coord_def) that contains a tuple (x,y) this will simplify the computation of the distance.
merged_dataframe['coord_off'] = merged_dataframe.apply(lambda x: (x['x-coord_off'], x['y-coord_off']),axis=1)
merged_dataframe['coord_def'] = merged_dataframe.apply(lambda x: (x['x-coord_def'], x['y-coord_def']),axis=1)
We compute the distance to all the defender at a given GameTime,PlayId.
merged_dataframe['distance_to_def'] = merged_dataframe.apply(lambda x: distance.euclidean(x['coord_off'],x['coord_def']),axis=1)
For each PlayerId,GameTime,PlayId we take the distance to the nearest defender.
smallest_dist = merged_dataframe.groupby(['GameTime','PlayId','PlayerId_off'])['distance_to_def'].min()
Finally we take the maximum distance (of these minimum distances) for each PlayerId.
smallest_dist.groupby('PlayerId_off').max()
cars_df = pd.DataFrame((car.iloc[:[1,3,4,6]].values), columns = ['mpg', 'dip', 'hp', 'wt'])
car_t = car.iloc[:9].values
target_names = [0,1]
car_df['group'] = pd.series(car_t, dtypre='category')
sb.pairplot(cars_df)
I have tried using .iloc(axis=0)[xxxx] and making a slice into a list and a tuple. no dice. Any thoughts? I am trying to make a scatter plot from a lynda.com video but in the video, the host is using .ix which is deprecated. So I am using .iloc[]
car = a dataframe
a few lines of data
"Car_name","mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear","carb"
"Mazda RX4",21,6,160,110,3.9,2.62,16.46,0,1,4,4
"Mazda RX4 Wag",21,6,160,110,3.9,2.875,17.02,0,1,4,4
"Datsun 710",22.8,4,108,93,3.85,2.32,18.61,1,1,4,1
"Hornet 4 Drive",21.4,6,258,110,3.08,3.215,19.44,1,0,3,1
"Hornet Sportabout",18.7,8,360,175,3.15,3.44,17.02,0,0,3,2
"Valiant",18.1,6,225,105,2.76,3.46,20.22,1,0,3,1
"Duster 360",14.3,8,360,245,3.21,3.57,15.84,0,0,3,4
"Merc 240D",24.4,4,146.7,62,3.69,3.19,20,1,0,4,2
"Merc 230",22.8,4,140.8,95,3.92,3.15,22.9,1,0,4,2
"Merc 280",19.2,6,167.6,123,3.92,3.44,18.3,1,0,4,4
"Merc 280C",17.8,6,167.6,123,3.92,3.44,18.9,1,0,4,4
"Merc 450SE",16.4,8,275.8,180,3.07,4.07,17.4,0,0,3,3
I think you want select multiple columns by iloc:
cars_df = car.iloc[:, [1,3,4,6]]
print (cars_df)
mpg disp hp wt
0 21.0 160.0 110 2.620
1 21.0 160.0 110 2.875
2 22.8 108.0 93 2.320
3 21.4 258.0 110 3.215
4 18.7 360.0 175 3.440
5 18.1 225.0 105 3.460
6 14.3 360.0 245 3.570
7 24.4 146.7 62 3.190
8 22.8 140.8 95 3.150
9 19.2 167.6 123 3.440
10 17.8 167.6 123 3.440
11 16.4 275.8 180 4.070
sb.pairplot(cars_df)
Not 100% sure with another code, it seems need:
#select also 9. column
cars_df = car.iloc[:, [1,3,4,6,9]]
#rename 9. column
cars_df = cars_df.rename(columns={'am':'group'})
#convert it to categorical
cars_df['group'] = pd.Categorical(cars_df['group'])
print (cars_df)
mpg disp hp wt group
0 21.0 160.0 110 2.620 1
1 21.0 160.0 110 2.875 1
2 22.8 108.0 93 2.320 1
3 21.4 258.0 110 3.215 0
4 18.7 360.0 175 3.440 0
5 18.1 225.0 105 3.460 0
6 14.3 360.0 245 3.570 0
7 24.4 146.7 62 3.190 0
8 22.8 140.8 95 3.150 0
9 19.2 167.6 123 3.440 0
10 17.8 167.6 123 3.440 0
11 16.4 275.8 180 4.070 0
#add parameetr hue for different levels of a categorical variable
sb.pairplot(cars_df, hue='group')
I am trying to plot a line graph using data from a file that has several columns in it (16 in fact). I have bee trying to use the command
plot 'snr.dat' using 2:16 with lines
but I do not seem to be getting the result I would like.
I have attached an extract from the file I am using.
2014/10/30 0:00:28.847 00000 159.9 71.6 -12.51 .40 64.1 217.1 3 23.1 15 1 3511. .055 -9.99 11.4
2014/10/30 0:00:28.847 00000 229.9 103.9 -12.51 .40 64.1 217.1 3 23.1 15 1 3511. .055 -9.99 11.4
2014/10/30 0:00:28.847 00000 159.9 81.7 -12.51 .40 59.9 92.6 3 29.4 23 1 3511. .055 -9.99 11.4
2014/10/30 0:00:28.847 00001 159.9 71.6 -12.51 .40 64.0 217.1 3 23.4 25 1 3508. .055 -9.99 11.3
2014/10/30 0:00:28.847 00001 229.9 103.9 -12.51 .40 64.0 217.1 3 23.4 25 1 3508. .055 -9.99 11.3
2014/10/30 0:00:28.847 00001 159.9 81.7 -12.51 .40 59.9 92.6 3 29.6 14 1 3508. .055 -9.99 11.3
2014/10/30 0:01:30.114 00002 229.9 92.3 1.02 1.62 67.3 138.7 2 27.2 25 1 1746. .138 -9.99 5.7
2014/10/30 0:01:30.114 00002 159.9 89.9 1.02 1.62 56.4 97.4 2 26.5 35 1 1746. .138 -9.99 5.7
2014/10/30 0:02:30.504 00005 96.0 90.1 -25.64 1.18 20.3 120.5 1 17.2 45 1 2553. .165 -9.99 8.7
2014/10/30 0:02:52.896 00007 102.0 91.5 2.23 .03 26.4 140.8 1 11.8 35 1 19393. .098 -9.99 23.6
2014/10/30 0:02:52.890 00008 100.0 89.6 3.52 .57 26.5 139.9 1 10.9 35 1 4394. .214 -9.99 13.0
2014/10/30 0:02:52.894 00009 104.0 93.3 2.39 .52 26.4 141.0 1 10.1 13 1 4376. .110 -9.99 12.5
2014/10/30 0:03:20.093 0000B 106.0 84.5 5.30 2.01 37.4 202.2 1 25.8 45 1 2306. .095 -9.99 7.8
2014/10/30 0:04:08.515 0000D 102.0 88.1 13.20 1.92 30.5 180.6 3 28.4 15 1 3200. .061 -9.99 9.9
2014/10/30 0:04:08.515 0000D 102.0 99.4 13.20 1.92 12.9 68.6 3 26.1 45 1 3200. .061 -9.99 9.9
2014/10/30 0:04:08.515 0000D 102.0 88.2 13.20 1.92 30.3 128.4 3 38.2 13 1 3200. .061 -9.99 9.9
2014/10/30 0:04:12.642 0000E 108.0 91.9 -38.85 .20 31.9 222.0 1 23.8 15 1 9636. .084 -9.99 20.2
2014/10/30 0:04:12.640 0000F 110.0 93.6 -38.17 .51 31.9 221.9 1 23.6 25 1 4974. .086 -9.99 14.7
2014/10/30 0:04:40.580 0000G 201.9 93.0 -20.01 .41 63.4 38.1 1 24.7 15 1 2716. .244 -9.99 9.3
I would like to have the time (that's in the second in the second column) on the x axis, and the snr values (that's in the 16th column) on the y axis with a line joining them.
Thanks for any help, and if you need any more info just ask please.
Then you must tell gnuplot, that you want to plot time data on the x-axis with
set xdata time
and in which format the time should be parsed
set timefmt '%H:%M:%S'
So, a complete minimal script could be
set timefmt '%H:%M:%S'
set xdata time
plot 'snr.dat' using 2:17 with lines title 'SNR'
I want to separate this textfile to 3 textfile that each paragraph makes a textfile.(my Os is ubuntu12.04)
Input
2008 2 2 1120 31.2 L 34.031 48.515 16.7 INS 5 0.3 4.0LINS 1
GAP=145 0.67 4.1 2.9 6.6 0.2283E-01 -0.1718E+00 0.1289E+02E
ACTION:UPD 08-12-28 13:25 OP:moh STATUS: ID:20080202112031 L I
2008-02-02-1120-39S.IN____006 6
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
SNGE BZ EPg 1120 57.69 91 0.0210 159 318
SNGE BZ AML 1121 24.50 2880.9 0.55 159 318
SHGR BZ EPN5 1121 5.17 52 -0.0510 215 173
GHVR BZ EPn 1121 10.84 52 0.3610 256 78
GHVR BZ ESg 1121 43.50 91 -0.0210 256 78
CHTH BZ EPn 1121 18.26 52 0.1210 317 48
CHTH BZ AML 1122 8.01 494.0 0.68 317 48
DAMV BZ EPn 1121 23.36 52 -0.49 9 362 60
DAMV BZ AML 1122 7.03 382.0 0.48 362 60
2008 211 1403 46.2 L 27.659 55.544 14.1 INS 4 0.1 4.0LINS 1
GAP=171 0.38 1.7 1.2 3.3 -0.8271E-01 -0.3724E-01 0.4284E+00E
2008-02-11-1403-37S.INSN__048 6
ACTION:NEW 08-12-28 13:25 OP:moh STATUS: ID:20080211140346 L I
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
BNDS BZ EPg 14 3 58.14 90 -0.0710 68.3 115
BNDS BN AML 14 4 26.39 8461.0 0.52 68.3 115
GHIR BZ EPn 14 4 26.40 52 0.0310 261 286
GHIR BN ESg 14 4 59.85 90 -0.0110 261 286
GHIR BN AML 14 5 25.22 1122.4 0.56 261 286
GHIR BE AML 14 5 43.83 769.3 0.64 261 286
KRBR BZ EPn 14 4 29.25 52 -0.1110 284 24
KRBR BN ESg 14 5 6.28 90 0.0010 284 24
KRBR BN AML 14 5 18.89 552.4 0.64 284 24
KRBR BE AML 14 5 19.22 574.0 0.60 284 24
ZHSF BZ EPn 14 5 3.24 52 0.25 8 555 66
2008 213 2055 31.5 L 31.713 51.180 14.1 INS 9 0.5 4.2LINS 1
GAP=127 1.21 4.6 6.5 9.6 0.7570E+01 -0.1161E+02 0.9944E+01E
ACTION:UPD 08-12-28 13:25 OP:moh STATUS: ID:20080213205531 L I
2008-02-13-2054-59S.NSN___048 6
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
NASN BZ EPg 2056 3.15 90 -0.6410 195 51
SHGR BZ EPg 2056 8.57 90 -0.3810 229 282
SHGR BN AML 2056 49.27 2371.2 0.77 229 282
SHGR BE AML 2056 51.00 2484.4 0.77 229 282
GHVR BZ EPn 2056 18.39 52 1.0110 307 1
GHVR BE AML 2057 11.42 734.2 0.85 307 1
ASAO BZ EPn 2056 20.35 52 -0.36 9 332 341
ASAO BE ESg 2057 5.23 90 0.27 9 332 341
ASAO BN AML 2057 15.86 723.3 0.64 332 341
GHIR BZ EPn 2056 31.68 52 0.48 9 418 155
GHIR BN AML 2057 51.30 259.1 0.79 418 155
DAMV BZ EPn 2056 33.90 52 -0.27 9 441 9
DAMV BN AML 2057 43.30 237.4 0.65 441 9
THKV BZ EPn 2056 37.71 52 0.33 8 467 357
THKV BE AML 2057 51.62 205.7 0.72 467 357
ZNJK BZ EPn 2056 53.12 52 -0.35 7 596 338
BNDS BZ EPn 2057 3.72 52 -0.06 7 680 133
output1.txt
2008 2 2 1120 31.2 L 34.031 48.515 16.7 INS 5 0.3 4.0LINS 1
GAP=145 0.67 4.1 2.9 6.6 0.2283E-01 -0.1718E+00 0.1289E+02E
ACTION:UPD 08-12-28 13:25 OP:moh STATUS: ID:20080202112031 L I
2008-02-02-1120-39S.IN____006 6
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
SNGE BZ EPg 1120 57.69 91 0.0210 159 318
SNGE BZ AML 1121 24.50 2880.9 0.55 159 318
SHGR BZ EPN5 1121 5.17 52 -0.0510 215 173
GHVR BZ EPn 1121 10.84 52 0.3610 256 78
GHVR BZ ESg 1121 43.50 91 -0.0210 256 78
CHTH BZ EPn 1121 18.26 52 0.1210 317 48
CHTH BZ AML 1122 8.01 494.0 0.68 317 48
DAMV BZ EPn 1121 23.36 52 -0.49 9 362 60
DAMV BZ AML 1122 7.03 382.0 0.48 362 60
output2.txt
2008 211 1403 46.2 L 27.659 55.544 14.1 INS 4 0.1 4.0LINS 1
GAP=171 0.38 1.7 1.2 3.3 -0.8271E-01 -0.3724E-01 0.4284E+00E
2008-02-11-1403-37S.INSN__048 6
ACTION:NEW 08-12-28 13:25 OP:moh STATUS: ID:20080211140346 L I
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
BNDS BZ EPg 14 3 58.14 90 -0.0710 68.3 115
BNDS BN AML 14 4 26.39 8461.0 0.52 68.3 115
GHIR BZ EPn 14 4 26.40 52 0.0310 261 286
GHIR BN ESg 14 4 59.85 90 -0.0110 261 286
GHIR BN AML 14 5 25.22 1122.4 0.56 261 286
GHIR BE AML 14 5 43.83 769.3 0.64 261 286
KRBR BZ EPn 14 4 29.25 52 -0.1110 284 24
KRBR BN ESg 14 5 6.28 90 0.0010 284 24
KRBR BN AML 14 5 18.89 552.4 0.64 284 24
KRBR BE AML 14 5 19.22 574.0 0.60 284 24
ZHSF BZ EPn 14 5 3.24 52 0.25 8 555 66
output3.txt
2008 213 2055 31.5 L 31.713 51.180 14.1 INS 9 0.5 4.2LINS 1
GAP=127 1.21 4.6 6.5 9.6 0.7570E+01 -0.1161E+02 0.9944E+01E
ACTION:UPD 08-12-28 13:25 OP:moh STATUS: ID:20080213205531 L I
2008-02-13-2054-59S.NSN___048 6
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
NASN BZ EPg 2056 3.15 90 -0.6410 195 51
SHGR BZ EPg 2056 8.57 90 -0.3810 229 282
SHGR BN AML 2056 49.27 2371.2 0.77 229 282
SHGR BE AML 2056 51.00 2484.4 0.77 229 282
GHVR BZ EPn 2056 18.39 52 1.0110 307 1
GHVR BE AML 2057 11.42 734.2 0.85 307 1
ASAO BZ EPn 2056 20.35 52 -0.36 9 332 341
ASAO BE ESg 2057 5.23 90 0.27 9 332 341
ASAO BN AML 2057 15.86 723.3 0.64 332 341
GHIR BZ EPn 2056 31.68 52 0.48 9 418 155
GHIR BN AML 2057 51.30 259.1 0.79 418 155
DAMV BZ EPn 2056 33.90 52 -0.27 9 441 9
DAMV BN AML 2057 43.30 237.4 0.65 441 9
THKV BZ EPn 2056 37.71 52 0.33 8 467 357
THKV BE AML 2057 51.62 205.7 0.72 467 357
ZNJK BZ EPn 2056 53.12 52 -0.35 7 596 338
BNDS BZ EPn 2057 3.72 52 -0.06 7 680 133
I give you an idea, just one method: iterate your file, row by row.
Save in a buffer all the row while row!="" or row!='\n': in this case, save buffer in a differente file.
buffer=""
id=0
cat test | \
while read row; do
#check row value, save in buffer
.....
cat buffer > fileName_${id}.txt
id=$((id+1))
done