2017-02-14 1 views
0

Wenn ich ein Array mit x- und y-Daten haben, die in 20 Gruppen unterteilt, wie würde ich die Fläche unter jeder Gruppe finden?Den Bereich unter Array mit x- und y-Daten in n Gruppen in numpy/python?

Der Einfachheit halber betrachten

x=np.linspace(1,100,100) 
y=a 
c=np.vstack((x,y)).T 

dies nur die Linie y = x mit 100 Punkten für x und y. Wenn ich Trapz (b, a) verwende, erhalte ich eine ungefähre Fläche unter der Linie.

Jetzt möchte ich dies in 20 Gruppen zu unterteilen und die Fläche unter jeder Gruppe

d=np.array_split(temprange,20) 

finden Wie finde ich den Bereich in jeder Gruppe. Jede Gruppe sollte 5 x Punkte und 5 y Punkte haben und ich möchte Trapz verwenden, um den Bereich jeder Gruppe zu finden.

Antwort

0

Sie können dies tun, w/out zu viel auf np verlassen, obwohl die array_split ein großartiges Werkzeug. Im Wesentlichen teilt der Code die xs und ys getrennt auf und kombiniert sie dann mit zip, um eine einfache Indizierung zu ermöglichen.

Code:

import numpy as np 
x = np.linspace(1,100,100) 
y = x 

#Split x's and y's separately then combine them w/ zip 
num_groups = 20 
split_xs = np.array_split(x,num_groups) 
split_ys = np.array_split(y,num_groups) 
split_groups = zip(split_xs,split_ys) 

#Short version w/ list comprehension 
split_trapzs = [np.trapz(split_y,split_x) for split_x,split_y in split_groups] 

#Longer version w/out list comprehension 
split_trapzs = [] 
for split_x,split_y in split_groups: 
    split_trapz = np.trapz(split_y,split_x) 
    print 'x:',split_x,'y:',split_x,'trapz:',split_trapz 
    split_trapzs.append(split_trapz) 

print 'Split trapzs:',split_trapzs 

OUTPUT:

x: [ 1. 2. 3. 4. 5.] y: [ 1. 2. 3. 4. 5.] trapz: 12.0 
x: [ 6. 7. 8. 9. 10.] y: [ 6. 7. 8. 9. 10.] trapz: 32.0 
x: [ 11. 12. 13. 14. 15.] y: [ 11. 12. 13. 14. 15.] trapz: 52.0 
x: [ 16. 17. 18. 19. 20.] y: [ 16. 17. 18. 19. 20.] trapz: 72.0 
x: [ 21. 22. 23. 24. 25.] y: [ 21. 22. 23. 24. 25.] trapz: 92.0 
x: [ 26. 27. 28. 29. 30.] y: [ 26. 27. 28. 29. 30.] trapz: 112.0 
x: [ 31. 32. 33. 34. 35.] y: [ 31. 32. 33. 34. 35.] trapz: 132.0 
x: [ 36. 37. 38. 39. 40.] y: [ 36. 37. 38. 39. 40.] trapz: 152.0 
x: [ 41. 42. 43. 44. 45.] y: [ 41. 42. 43. 44. 45.] trapz: 172.0 
x: [ 46. 47. 48. 49. 50.] y: [ 46. 47. 48. 49. 50.] trapz: 192.0 
x: [ 51. 52. 53. 54. 55.] y: [ 51. 52. 53. 54. 55.] trapz: 212.0 
x: [ 56. 57. 58. 59. 60.] y: [ 56. 57. 58. 59. 60.] trapz: 232.0 
x: [ 61. 62. 63. 64. 65.] y: [ 61. 62. 63. 64. 65.] trapz: 252.0 
x: [ 66. 67. 68. 69. 70.] y: [ 66. 67. 68. 69. 70.] trapz: 272.0 
x: [ 71. 72. 73. 74. 75.] y: [ 71. 72. 73. 74. 75.] trapz: 292.0 
x: [ 76. 77. 78. 79. 80.] y: [ 76. 77. 78. 79. 80.] trapz: 312.0 
x: [ 81. 82. 83. 84. 85.] y: [ 81. 82. 83. 84. 85.] trapz: 332.0 
x: [ 86. 87. 88. 89. 90.] y: [ 86. 87. 88. 89. 90.] trapz: 352.0 
x: [ 91. 92. 93. 94. 95.] y: [ 91. 92. 93. 94. 95.] trapz: 372.0 
x: [ 96. 97. 98. 99. 100.] y: [ 96. 97. 98. 99. 100.] trapz: 392.0 
Split trapzs: [12.0, 32.0, 52.0, 72.0, 92.0, 112.0, 132.0, 152.0, 172.0, 192.0, 212.0, 232.0, 252.0, 272.0, 292.0, 312.0, 332.0, 352.0, 372.0, 392.0] 
+0

fantastisch! Danke. Ich werde versuchen, eine Variation davon, was ich brauche, und hoffentlich funktioniert es. –

+0

großartig, zögern Sie nicht, Ihre Frage zu aktualisieren, wenn Sie mehr Probleme haben – mitoRibo

Verwandte Themen