Related
Here is my array:
array([-0.1142 , 0.11127 , 0.0374 , 0.02007 , -0.05737 , -0.02058 ,
-0.1595 , -0.1288 , 0.1436 , -0.05212 , 0.2437 , 0.0046 ,
-0.1456 , -0.09485 , -0.0788 , 0.1755 , -0.2429 , -0.1204 ,
-0.01064 , 0.01154 , 0.06058 , -0.02666 , 0.01773 , 0.03436 ,
-0.1262 , -0.3428 , -0.068 , -0.10645 , 0.0669 , -0.02094 ,
-0.0751 , 0.001348, -0.1737 , 0.01146 , 0.01648 , 0.03613 ,
0.03384 , -0.063 , 0.1617 , -0.03023 , -0.258 , 0.0385 ,
0.0382 , 0.1821 , 0.2104 , -0.01604 , 0.05945 , -0.1809 ,
0.1847 , -0.1569 , 0.02007 , 0.1757 , 0.08514 , 0.07886 ,
-0.00872 , -0.1108 , -0.01473 , 0.1075 , -0.1221 , 0.0163 ,
0.03275 , -0.01775 , 0.01232 , -0.0705 ], dtype=float16)
I need to convert it into binary that should recognize the decimal and negative sign then every element should output 16-bit but I don't know how please help me. I am using Jupyter Notebook to run my python program.
Use the struct module to interpret the half (16-bit float) as an integer, then convert that integer to a 16-character long binary string.
Per the documentation, ! means use the computer's system (either big-endian or little-endian) for the ordering of the two bytes, H means a unsigned short (16-bit unsigned integer), and e means a half (16-bit floating point number)
import numpy as np
import struct
arr = np.array([-0.1142 , 0.11127 , 0.0374 , 0.02007 , -0.05737 , -0.02058 ,
-0.1595 , -0.1288 , 0.1436 , -0.05212 , 0.2437 , 0.0046 ,
-0.1456 , -0.09485 , -0.0788 , 0.1755 , -0.2429 , -0.1204 ,
-0.01064 , 0.01154 , 0.06058 , -0.02666 , 0.01773 , 0.03436 ,
-0.1262 , -0.3428 , -0.068 , -0.10645 , 0.0669 , -0.02094 ,
-0.0751 , 0.001348, -0.1737 , 0.01146 , 0.01648 , 0.03613 ,
0.03384 , -0.063 , 0.1617 , -0.03023 , -0.258 , 0.0385 ,
0.0382 , 0.1821 , 0.2104 , -0.01604 , 0.05945 , -0.1809 ,
0.1847 , -0.1569 , 0.02007 , 0.1757 , 0.08514 , 0.07886 ,
-0.00872 , -0.1108 , -0.01473 , 0.1075 , -0.1221 , 0.0163 ,
0.03275 , -0.01775 , 0.01232 , -0.0705 ], dtype=np.float16)
def half_to_binstr(half):
bits, = struct.unpack('!H', struct.pack('!e', half))
return "{:016b}".format(bits)
res = np.vectorize(half_to_binstr)(arr)
Output:
array(['1010111101001111', '0010111100011111', '0010100011001010',
'0010010100100011', '1010101101011000', '1010010101000101',
'1011000100011011', '1011000000011111', '0011000010011000',
'1010101010101100', '0011001111001100', '0001110010110110',
'1011000010101001', '1010111000010010', '1010110100001011',
'0011000110011110', '1011001111000110', '1010111110110101',
'1010000101110011', '0010000111101001', '0010101111000001',
'1010011011010011', '0010010010001010', '0010100001100110',
'1011000000001010', '1011010101111100', '1010110001011010',
'1010111011010000', '0010110001001000', '1010010101011100',
'1010110011001110', '0001010110000101', '1011000110001111',
'0010000111011110', '0010010000111000', '0010100010100000',
'0010100001010101', '1010110000001000', '0011000100101101',
'1010011110111101', '1011010000100001', '0010100011101110',
'0010100011100100', '0011000111010100', '0011001010111100',
'1010010000011011', '0010101110011100', '1011000111001010',
'0011000111101001', '1011000100000101', '0010010100100011',
'0011000110011111', '0010110101110011', '0010110100001100',
'1010000001110111', '1010111100010111', '1010001110001011',
'0010111011100001', '1010111111010000', '0010010000101100',
'0010100000110001', '1010010010001011', '0010001001001111',
'1010110010000011'], dtype='<U16')
I have data like this:
x = np.array([ 0. , 3. , 3.3 , 10. , 18. , 43. , 80. ,
120. , 165. , 210. , 260. , 310. , 360. , 410. ,
460. , 510. , 560. , 610. , 660. , 710. , 760. ,
809.5 , 859. , 908.5 , 958. , 1007.5 , 1057. , 1106.5 ,
1156. , 1205.5 , 1255. , 1304.5 , 1354. , 1403.5 , 1453. ,
1502.5 , 1552. , 1601.5 , 1651. , 1700.5 , 1750. , 1799.5 ,
1849. , 1898.5 , 1948. , 1997.5 , 2047. , 2096.5 , 2146. ,
2195.5 , 2245. , 2294.5 , 2344. , 2393.5 , 2443. , 2492.5 ,
2542. , 2591.5 , 2640. , 2690. , 2740. , 2789.67, 2839.33,
2891.5 ])
y = array([ 1.45 , 1.65 , 5.8 , 6.8 , 8.0355, 8.0379, 8.04 ,
8.0505, 8.175 , 8.3007, 8.4822, 8.665 , 8.8476, 9.0302,
9.528 , 9.6962, 9.864 , 10.032 , 10.2 , 10.9222, 11.0553,
11.1355, 11.2228, 11.3068, 11.3897, 11.4704, 11.5493, 11.6265,
11.702 , 11.7768, 11.8491, 11.9208, 11.9891, 12.0571, 12.1247,
12.1912, 12.2558, 12.3181, 12.3813, 12.4427, 12.503 , 12.5638,
12.6226, 12.6807, 12.7384, 12.7956, 12.8524, 12.9093, 12.9663,
13.0226, 13.0786, 13.1337, 13.1895, 13.2465, 13.3017, 13.3584,
13.4156, 13.4741, 13.5311, 13.5899, 13.6498, 13.6533, 13.657 ,
13.6601])
and iam do this:
svr_res = SVR(kernel='rbf',C=100,gamma=0.5,epsilon=0.1)
X = np.arange(0,np.max(x),10).reshape(-1,1)
Y = svr_res.fit(x,y).predict(X)
and the result like this :
Where the red line is my data. I don't know why iam get result like that (blue line is the issue at my result). My goal is can make curve smooth with a nice result (RMS was low) and start from point (x(0),y(0))
I am trying to apply a vectorised function to a 1D NumPy array (test).
If an element is greater than a certain threshold the function is applied, otherwise a 0 is returned.
Passing the function on the whole array gives an array of zeros (result #1)
Yet this works for some slices of the large array(result #2) and a small array (result #3)
Can you help me understand why this is?
import numpy as np
test = np.array([-58.08281 , -47.07844 , -39.38589 , -38.244213 , -36.04118 ,
-35.17719 , -47.651756 , -47.123497 , -38.47037 , -31.427711 ,
-35.980206 , -39.04678 , -43.247276 , -29.217781 , -26.16616 ,
-23.175611 , -19.073223 , -19.573145 , -19.291908 , -19.084608 ,
-24.24286 , -26.768343 , -29.40547 , -42.254036 , -32.5126 ,
-27.8232 , -26.521381 , -18.53816 , -16.300032 , -14.897881 ,
-11.96727 , -11.895884 , -11.958228 , -11.689035 , -19.331993 ,
-22.528988 , -14.850136 , -10.7898 , -10.738896 , -11.510415 ,
-11.297523 , -14.9558525, -18.261246 , -20.11386 , -35.434853 ,
-36.547577 , -29.713285 , -35.055378 , -19.717499 , -15.524372 ,
-14.905738 , -11.690297 , -12.295127 , -14.571337 , -14.457521 ,
-20.896961 , -35.145 , -39.106945 , -20.592056 , -19.292147 ,
-21.957949 , -20.131369 , -31.953508 , -24.577961 , -23.88112 ,
-16.549093 , -16.742077 , -22.181223 , -21.692726 , -34.572075 ,
-20.111103 , -18.57012 , -12.833547 , -11.325545 , -12.807129 ,
-11.844269 , -19.830124 , -21.79983 , -18.484238 , -12.855567 ,
-11.830711 , -14.83697 , -14.618052 , -19.990686 , -30.934792 ,
-27.72318 , -17.222315 , -14.099125 , -16.516563 , -15.129327 ,
-19.21385 , -41.145554 , -37.12835 , -20.674335 , -17.670841 ,
-26.641182 , -26.721628 , -29.708376 , -16.29707 , -15.220005 ,
-11.475418 , 35.859955 , -10.404102 , 35.160667 , -11.339685 ,
-17.627815 , -18.65314 , -25.346134 , -38.297813 , -22.460407 ,
-21.334377 , -16.922516 , -10.733174 , 35.263527 , 35.078003 ,
35.26928 , 35.44266 , 35.89205 , -10.965962 , -16.772722 ,
-10.638295 , 35.37294 , 35.32364 , 35.271263 , 35.900078 ,
35.145794 , -12.282233 , -14.206524 , -18.138363 , -37.339016 ,
-26.27323 , -27.531588 , -25.00942 , -13.963585 , -12.315678 ,
-10.978365 , 35.439877 , -10.534686 , -11.77856 , -12.630129 ,
-22.29188 , -32.74709 , -29.052572 , -16.526686 , -18.223225 ,
-19.174236 , -18.920668 , -34.266537 , -23.23388 , -19.992903 ,
-13.9729805, -16.85691 , -20.88271 , -21.805904 , -24.517344 ,
-17.412155 , -15.050234 , 35.047886 , -10.27907 , -10.765995 ,
-11.394721 , -34.574 , -18.185272 , -15.156159 , -10.370025 ,
-11.406872 , -13.781429 , -13.863158 , -24.35263 , -29.509377 ,
-24.758411 , -14.150916 , -13.686075 , -15.366934 , -14.149103 ,
-22.916718 , -35.810047 , -33.369896 , -17.931974 , -18.65556 ,
-28.330248 , -27.015589 , -23.890095 , -15.020579 , -13.920487 ,
35.49385 , 35.613037 , 35.326546 , 35.1469 , -12.024554 ,
-17.770742 , -18.414755 , -31.574192 , -35.00205 , -20.591629 ,
-21.097118 , -14.166552 , 35.61772 , 35.196175 , 35.884003 ,
35.032402 , 35.289963 , 35.18595 , -36.364285 , -10.158181 ,
35.040634 , 35.349873 , 35.31796 , 35.87602 , 35.88828 ,
35.086105 , -12.404961 , -13.550255 , -20.19417 , -35.630135 ,
-23.762396 , -27.673418 , -19.928736 , -12.206515 , -11.781338 ,
35.307823 , 35.67385 , -10.780588 , -11.199528 , -13.561855 ,
-24.982666 , -30.838753 , -25.138466 , -16.61114 , -20.002995 ,
-18.823566 , -21.581133 , -25.644733 , -22.914455 , -17.489904 ,
-13.714966 , -18.483316 , -20.454823 , -25.238888 , -20.592503 ,
-17.511456 , -13.5111885, 35.399975 , -10.711888 , -10.577221 ,
-13.2071705, -27.878649 , -16.227467 , -13.394671 , 35.33075 ,
-10.933496 , -12.903596 , -13.261947 , -23.191689 , -36.082005 ,
-26.252464 , -14.935854 , -14.955426 , -16.291502 , -15.563564 ,
-27.91648 , -30.43707 , -27.09887 , -16.93166 , -19.03229 ,
-26.68034 , -26.50705 , -22.435007 , -15.312309 , -13.67744 ,
35.70387 , 35.197517 , 35.21866 , 35.759956 , -12.934032 ,
-18.348143 , -19.073929 , -36.864773 , -32.881073 , -20.560263 ,
-20.530846 , -13.128365 , 35.65545 , 35.465275 , 35.028538 ,
35.842434 , 35.676643 , -17.01441 , -17.217728 , 35.667717 ,
35.871662 , 35.92965 , 35.316013 , 35.096027 , 35.02661 ,
35.988937 , -12.0597515, -13.201061 , -20.259245 , -28.855875 ,
-21.791933 , -25.400242 , -17.618946 , -11.611944 , -11.329423 ,
35.063614 , 35.825493 , -10.553531 , -10.820301 , -13.883024 ,
-22.231556 , -26.921532 , -31.872276 , -18.039211 , -19.713062 ,
-20.517511 , -21.620483 , -26.919012 , -20.787134 , -17.330051 ,
-13.198881 , -15.984946 , -19.181019 , -21.50328 , -25.311642 ,
-18.11811 , -14.696231 , -10.136784 , -10.480961 , -11.110486 ,
-13.739718 , -28.865023 , -15.966995 , -13.198223 , 35.18759 ,
-10.803377 , -12.718526 , -13.597855 , -23.472122 , -34.405643 ,
-24.122065 , -14.643904 , -14.425363 , -15.651573 , -15.197855 ,
-25.13602 , -33.207695 , -26.908777 , -17.217882 , -19.061764 ,
-27.06517 , -28.88142 , -21.721449 , -14.84623 , -12.997027 ,
35.853565 , 35.51484 , 35.660423 , 35.982292 , -12.461762 ,
-17.52755 , -19.008127 , -32.69878 , -30.82928 , -20.193447 ,
-19.172876 , -12.901536 , 35.05082 , 35.915546 , 35.254303 ,
35.797028 , -14.470562 , -22.461277 , -15.07134 , 35.970448 ,
35.198704 , 35.945583 , 35.362762 , 35.306732 , 35.064957 ,
35.10975 , -11.703257 , -13.411005 , -20.08778 , -28.905445 ,
-22.59493 , -25.155657 , -17.814808 , -11.842859 , -11.154184 ,
35.989094 , 35.854362 , -10.2389765, -10.827884 , -14.010275 ,
-25.168896 , -33.99552 , -22.858255 , -16.562387 , -19.22073 ,
-18.317003 , -23.036928 , -25.22068 , -21.934307 , -16.469448 ,
-13.88927 , -18.307293 , -20.485218 , -29.06332 , -19.628113 ,
-16.496414 , -12.351503 , 35.66623 , -10.330103 , -10.866837 ,
-16.813847 , -21.454565 , -15.892494 , -12.269305 , 35.174488 ,
-11.898882 , -13.1494465, -15.517578 , -35.11971 , -29.069548 ,
-19.153015 , -13.194953 , -14.334308 , -14.483275 , -15.592762 ,
-30.123589 , -38.262245 , -24.752253 , -17.36696 , -22.627728 ,
-29.787828 , -44.489254 , -17.438164 , -13.678364 , -11.26264 ,
35.92086 , 35.600876 , 35.231567 , 35.960655 , -13.438512 ,
-16.794493 , -19.414097 , -33.008324 , -23.844492 , -18.63253 ,
-17.060545 , -10.566847 , 35.735447 , 35.061024 , 35.95225 ,
-11.117262 , -18.978222 , -39.73106 , -11.048341 , 35.58616 ,
35.699783 , 35.32885 , 35.09172 , 35.119743 , 35.753242 ,
35.73512 , -12.641587 , -14.861554 , -25.59355 , -29.808552 ,
-24.463276 , -26.617489 , -15.665792 , -11.706967 , -11.054789 ,
35.413254 , 35.13033 , -10.968152 , -11.514641 , -17.074472 ,
-31.623056 , -40.51703 , -18.116985 , -15.995826 , -18.33452 ,
-17.266975 , -28.274193 , -24.104795 , -21.711021 , -15.209898 ,
-15.003292 , -20.39471 , -21.562126 , -34.197975 , -16.957975 ,
-14.923981 , -10.418877 , 35.874657 , -10.214642 , -11.880876 ])
test2 = np.array([5,5,5])
Thresh = -10
Ratio = 5
#Defining function
def gr(x):
if x >=Thresh:
return Thresh + (x-Thresh)/Ratio
else:
return 0
#vectorising function
gr_v = np.vectorize(gr)
#RESULTS
##1
print(sum(gr_v(test))) #0
##2
print(sum(gr_v(test2)))
##3
print(sum(gr_v(test[200:250])))
Just as a remark, know that np.vectorize "function is provided primarily for convenience, not for performance. The implementation is essentially a for loop."
Therefore it is the same as:
l = [gr(x) for x in test]
sum(l)
-80.41708859999999
To use a vectorize implementation you can do:
np.sum(Thresh + (test[test>=Thresh]-Thresh)/Ratio)
-80.41708859999999
As time performance is concerned:
%timeit np.sum(Thresh + (test[test>=Thresh]-Thresh)/Ratio)
13.8 µs ± 864 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit sum(gr_v(test))
217 µs ± 5.32 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
The actual vectorized implemantation is 15x faster with this test size.
This has to do with the inherent property of vectorisation. If the first output of your vectorised function is integer 0, then the entire output is off. While this behaviour of vectorise is a bit memorising, I can provide a rather simple fix.
Instead of using
def gr(x):
if x >=Thresh:
return Thresh + (x-Thresh)/Ratio
else:
return 0
which returns:
0
-21.0
-12.044083
rewrite the function as :
def gr(x):
if x >=Thresh:
return Thresh + (x-Thresh)/Ratio
else:
return 0.0
it returns:
-80.41708859999999
-21.0
-12.044083
The purpose of returning 0.0 instead of 0 is to have a float(0) instead of int(0) as your output. It also explains why your codes work for your test2 and test[200:250] arrays, because both have positive numbers as their first element, therefore what is returned by gr_v is a float, but not int(0).
Hope that it helps!
It is something to do with type inference:
Try this:
gr_v = np.vectorize(gr, otypes=[np.float])
And this fixes it (note the .0):
Thresh = -10.0
Ratio = 5.0
#Defining function
def gr(x):
if x >=Thresh:
return Thresh + (x-Thresh)/Ratio
else:
return 0.0
#vectorising function
gr_v = np.vectorize(gr)
I would like to apply the [lowess()][1] function column-wise(column 1 of 1st array and column 1 of 2nd array) across two large numpy arrays with identical shapes (33,~14000), to return a third array containing the smoothed values with an identical shape.
One array contains the x-values and the other the y-values for the [lowess()][1] function.
I looked at [numpy.vectorize()][1] and don't seem to find a way to do this.
Is there a vectorized solution for this?
Code shown below includes a subset of the data (33, 5):
import numpy as np
from Bio.Statistics import lowess
x = np.array([[ 0.02482128, 0.04800129, 0.05970001, 0.2500887 , 0.13813114],
[-0.15335226, 0.03859329, 0.5601454 , -0.47751045, 0.14223003],
[ 0.17191982, -0.03256845, -0.2704444 , -0.093647 , -0.539505 ],
[-0.18524027, 0.01060915, -0.30057144, -0.14830208, 0.56457806],
[-0.1775446 , -0.01310873, -0.26189804, -0.17747355, -0.23629665],
[ 0.07648993, 0.11103201, 0.589818 , -0.11406326, 0.1776371 ],
[ 0.317667 , -0.01560402, 0.39747334, 0.11308289, 0.4232664 ],
[ 0.38938808, 0.23480797, -0.42039776, 0.06176662, -0.24095821],
[-0.06608486, 0.04713726, 0.35143852, 0.3167987 , 0.07772255],
[ 0.28575754, 0.08154392, -0.1795454 , -0.06183243, -0.6071148 ],
[ 0.27942848, 0.03686762, -0.11383152, -0.37741995, -0.67893934],
[ 0.09874439, -0.11218214, 0.53765774, 0.12229681, 0.09937191],
[ 0.07899189, -0.06172419, -0.01412106, 0.29815912, 0.06921196],
[-0.5575795 , 0.17440462, 0.4482336 , -0.42329168, -0.42318058],
[-0.00781012, 0.05469799, -0.00262833, -0.16313648, 0.33030987],
[ 0.01612997, -0.29763603, -0.35993958, 0.20896721, -0.01481056],
[-0.05184507, 0.22348356, 0.39140987, -0.14832544, 0.17410088],
[ 0.02847338, 0.02308035, -0.12702274, -0.19778919, 0.31087875],
[-0.5492716 , -0.02013874, 0.12577438, -0.11392832, -0.39846134],
[-0.03468943, -0.0295558 , -0.07844639, 0.04537296, 0.05451107],
[-0.18794298, 0.10741138, 0.36670208, -0.1904726 , -0.32082558],
[ 0.5786147 , 0.32772923, 0.2062025 , 0.32470465, -0.84580183],
[ 0.22584343, -0.02698565, -0.26552105, -0.06375647, 0.58737755],
[ 0.03465605, -0.24093103, -0.23005867, 0.15275288, 0.38084507],
[ 0.00937891, -0.14649105, -0.01934338, -0.23434734, -0.37168026],
[-0.02025366, -0.14081764, -0.21980095, 0.01488113, -0.2420435 ],
[-0.15545464, 0.17256403, 0.1636858 , 0.3812251 , 0.06866932],
[-0.02390814, 0.07711935, 0.28530407, 0.3817525 , 0.10252571],
[-0.2442751 , -0.0260272 , -0.2731142 , 0.31553936, 0.41060162],
[ 0.02275944, -0.00415039, 0.15864182, 0.33187914, 0.07651806],
[ 0.25222778, 0.15338421, -0.09697151, 0.0145421 , -0.08250237],
[ 0.61664677, -0.05378103, -0.5358076 , 0.15497589, -0.698823 ],
[-0.10142422, 0.4852066 , -0.25492382, -0.05385685, 0.15751743]],
dtype=np.float32)
y = np.array([[4.59169 , 4.418433 , 5.0820627, 4.6869965, 5.207094 ],
[4.5058136, 4.415601 , 5.0899377, 4.6807904, 4.9426126],
[4.5569663, 4.305499 , 4.886066 , 4.5803747, 5.056634 ],
[4.4900746, 4.37889 , 5.1278543, 4.6113243, 5.2266693],
[4.503826 , 4.400037 , 5.211916 , 4.682177 , 5.098976 ],
[4.509243 , 4.3813405, 5.3079786, 4.6301603, 5.2500844],
[4.41681 , 4.3992863, 5.0737085, 4.670207 , 5.070466 ],
[4.591771 , 4.4518366, 5.107484 , 4.596655 , 4.997365 ],
[4.494399 , 4.3796077, 5.242492 , 4.47066 , 5.0400076],
[4.519887 , 4.4044647, 5.1427035, 4.6785154, 5.021449 ],
[4.448152 , 4.4056873, 5.144441 , 4.5711513, 4.9853024],
[4.49775 , 4.385726 , 5.216489 , 4.6923037, 5.201977 ],
[4.5084896, 4.4458 , 5.2891297, 4.624942 , 4.9242325],
[4.5791883, 4.4296904, 5.280667 , 4.703702 , 5.1066465],
[4.589999 , 4.3781233, 5.0826826, 4.7128706, 5.1191587],
[4.638551 , 4.3878164, 5.1654997, 4.6913614, 5.324785 ],
[4.500376 , 4.500883 , 5.1015763, 4.656 , 5.2887416],
[4.488534 , 4.5052986, 5.367143 , 4.7019596, 5.1367083],
[4.470419 , 4.3764915, 5.065318 , 4.7039804, 5.168439 ],
[4.423869 , 4.513232 , 5.1516542, 4.609404 , 5.3527145],
[4.5888453, 4.480359 , 5.1191635, 4.676672 , 4.9557304],
[4.5064774, 4.4485407, 5.147471 , 4.648161 , 5.057909 ],
[4.557802 , 4.3785524, 5.0217905, 4.697456 , 5.0020485],
[4.54012 , 4.454771 , 5.033494 , 4.606558 , 4.8119564],
[4.6103888, 4.393633 , 5.1113367, 4.58374 , 5.0711713],
[4.574035 , 4.3336782, 5.0118513, 4.6048336, 5.288041 ],
[4.482691 , 4.3997755, 5.093251 , 4.624267 , 5.115726 ],
[4.4595666, 4.3373346, 5.1056104, 4.6904593, 5.3377056],
[4.488827 , 4.4477224, 5.190276 , 4.61971 , 5.233069 ],
[4.513173 , 4.402734 , 5.142403 , 4.625946 , 5.175432 ],
[4.4656053, 4.388732 , 5.1367936, 4.6234007, 5.249696 ],
[4.5307274, 4.429734 , 5.2134457, 4.6189766, 5.2845373],
[4.4585304, 4.471982 , 5.194815 , 4.6854787, 5.2281733]],
dtype=np.float32)
lowess.lowess(x[:,1], y[:,1])
#Output
'''array([4.39855668, 4.39910091, 4.40520765, 4.40238191, 4.40618866,
4.40353337, 4.40657502, 4.44116607, 4.39858563, 4.39976923,
4.39925592, 4.39799667, 4.3990058 , 4.4273006 , 4.39843829,
4.40196311, 4.43833446, 4.400469 , 4.40700818, 4.405916 ,
4.40225547, 4.45443733, 4.40643108, 4.40104955, 4.40217919,
4.40204647, 4.42686513, 4.39935488, 4.40659024, 4.40457143,
4.42105802, 4.40040609, 4.47989016])'''
I have a numpy array of the following shape
(categories, models, types, events, days) -> (2, 3, 4, 100, 14)
Now, I want to calculate the maximum of 14 days of data per event for a particular category, model, and type
I am doing this
np.max(data[0][0][0], axis=1)
I also want to calculate the max per type and per model for example.
I will be doing several of these operations by looping through [0] as [i] .
Is this the correct way of accessing the outermost array? Is there another way?
Addendum
np.max(data[0][0][0], axis=1)
array([ 3.9264417 , 3.3029506 , 3.0707457 , 3.6646023 , 1.7508441 ,
3.1634364 , 6.195052 , 1.5353022 , 1.8033538 , 1.4508389 ,
1.3882699 , 2.0849068 , 3.654939 , 6.6364765 , 3.92829 ,
6.6467876 , 1.5442419 , 4.639682 , 9.361191 , 5.261462 ,
1.7438816 , 5.6970205 , 2.4356377 , 1.6073244 , 2.6177561 ,
6.886767 , 3.890399 , 2.8880894 , 1.9826577 , 1.0888597 ,
4.3763924 , 3.8597727 , 1.790302 , 1.0277777 , 6.270729 ,
9.311213 , 2.318774 , 2.9298437 , 1.139397 , 0.9598383 ,
3.0489902 , 1.6736581 , 1.3983868 , 2.0979824 , 4.169757 ,
1.0739225 , 1.5311266 , 1.4676268 , 1.726325 , 1.8057758 ,
2.226462 , 2.6197987 , 4.49518 , 2.3042605 , 5.7164993 ,
1.182242 , 1.5107205 , 2.2920077 , 2.205539 , 1.4702082 ,
2.154468 , 2.0641963 , 4.9628353 , 1.9987459 , 2.1360166 ,
1.7073958 , 1.943267 , 7.5767093 , 1.3124634 , 2.2648168 ,
1.1504744 , 3.210688 , 2.6720855 , 2.998225 , 4.365262 ,
3.5410352 , 10.765423 , 4.6292825 , 3.1789696 , 0.92157686,
1.663245 , 1.5835482 , 3.1070056 , 1.6918416 , 8.086268 ,
3.7994847 , 2.4314868 , 1.6471033 , 1.1688241 , 1.7820593 ,
3.3509188 , 1.3092748 , 3.7915008 , 1.018912 , 3.2404447 ,
1.596657 , 2.0869658 , 2.6753283 , 2.1096318 , 8.786542 ],
dtype=float32)
Also,
type(np.array(data)) = numpy.ndarray
type(data) = list
I convert it for these operations.
What you have now is a one dimensional array. You can reshape the array to 2D, this way it is easier to access a column. To access all elements of a column use :. If each column has a specific meaning (events, days, etc.), you could also consider to store the data as a dictionary instead, as {'days': array([...]), 'events': array([])}
from numpy import array, float32
import numpy as np
x = array([ 3.9264417 , 3.3029506 , 3.0707457 , 3.6646023 , 1.7508441 ,
3.1634364 , 6.195052 , 1.5353022 , 1.8033538 , 1.4508389 ,
1.3882699 , 2.0849068 , 3.654939 , 6.6364765 , 3.92829 ,
6.6467876 , 1.5442419 , 4.639682 , 9.361191 , 5.261462 ,
1.7438816 , 5.6970205 , 2.4356377 , 1.6073244 , 2.6177561 ,
6.886767 , 3.890399 , 2.8880894 , 1.9826577 , 1.0888597 ,
4.3763924 , 3.8597727 , 1.790302 , 1.0277777 , 6.270729 ,
9.311213 , 2.318774 , 2.9298437 , 1.139397 , 0.9598383 ,
3.0489902 , 1.6736581 , 1.3983868 , 2.0979824 , 4.169757 ,
1.0739225 , 1.5311266 , 1.4676268 , 1.726325 , 1.8057758 ,
2.226462 , 2.6197987 , 4.49518 , 2.3042605 , 5.7164993 ,
1.182242 , 1.5107205 , 2.2920077 , 2.205539 , 1.4702082 ,
2.154468 , 2.0641963 , 4.9628353 , 1.9987459 , 2.1360166 ,
1.7073958 , 1.943267 , 7.5767093 , 1.3124634 , 2.2648168 ,
1.1504744 , 3.210688 , 2.6720855 , 2.998225 , 4.365262 ,
3.5410352 , 10.765423 , 4.6292825 , 3.1789696 , 0.92157686,
1.663245 , 1.5835482 , 3.1070056 , 1.6918416 , 8.086268 ,
3.7994847 , 2.4314868 , 1.6471033 , 1.1688241 , 1.7820593 ,
3.3509188 , 1.3092748 , 3.7915008 , 1.018912 , 3.2404447 ,
1.596657 , 2.0869658 , 2.6753283 , 2.1096318 , 8.786542 ],
dtype=float32)
x = np.reshape(x, (20, 5))
print x[:, -1]
>> [1.7508441 1.4508389 3.92829 5.261462 2.6177561 1.0888597
6.270729 0.9598383 4.169757 1.8057758 5.7164993 1.4702082
2.1360166 2.2648168 4.365262 0.92157686 8.086268 1.7820593
3.2404447 8.786542 ]