How to convert float array into binary? - python

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')

Related

SVR Regression with first point same as (x0,y0)

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))

Why numpy vectorised function does not apply to each element

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)

How can I select data in a multidimensional numpy array?

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 ]

Python - how to store multiple lists in one list?

this is the output of multiple lists and i want to store them in one list or array
(array([[ 1.52494154e+11, 1.52811638e+11, 1.52565040e+11, ...,
1.47778892e+11, 1.46781213e+11, 1.46678951e+11],
[ 7.69589176e+10, 7.73638333e+10, 7.76935891e+10, ...,
7.48498747e+10, 7.40088248e+10, 7.40343108e+10],
[ 6.32683585e+04, 1.58170271e+06, 6.11287648e+06, ...,
5.06690834e+05, 3.31360693e+05, 7.04757400e+05],
...,
[ 7.79589127e+05, 8.09843763e+04, 2.52907491e+05, ...,
2.48520301e+05, 2.11734697e+05, 2.50917758e+05],
[ 9.41199946e+05, 4.98371406e+05, 1.29328139e+06, ...,
2.56729806e+05, 3.45253951e+05, 3.51932417e+05],
[ 4.36846676e+05, 1.24123764e+06, 9.20694394e+05, ...,
8.35807658e+04, 8.36986905e+05, 3.57807267e+04]]), array([ 0. , 3.90625, 7.8125 , 11.71875, 15.625 ,
19.53125, 23.4375 , 27.34375, 31.25 , 35.15625,
39.0625 , 42.96875, 46.875 , 50.78125, 54.6875 ,
58.59375, 62.5 , 66.40625, 70.3125 , 74.21875,
78.125 , 82.03125, 85.9375 , 89.84375, 93.75 ,
97.65625, 101.5625 , 105.46875, 109.375 , 113.28125,
117.1875 , 121.09375, 125. , 128.90625, 132.8125 ,
136.71875, 140.625 , 144.53125, 148.4375 , 152.34375,
156.25 , 160.15625, 164.0625 , 167.96875, 171.875 ,
175.78125, 179.6875 , 183.59375, 187.5 , 191.40625,
195.3125 , 199.21875, 203.125 , 207.03125, 210.9375 ,
214.84375, 218.75 , 222.65625, 226.5625 , 230.46875,
234.375 , 238.28125, 242.1875 , 246.09375, 250. ,
253.90625, 257.8125 , 261.71875, 265.625 , 269.53125,
273.4375 , 277.34375, 281.25 , 285.15625, 289.0625 ,
292.96875, 296.875 , 300.78125, 304.6875 , 308.59375,
312.5 , 316.40625, 320.3125 , 324.21875, 328.125 ,
332.03125, 335.9375 , 339.84375, 343.75 , 347.65625,
351.5625 , 355.46875, 359.375 , 363.28125, 367.1875 ,
371.09375, 375. , 378.90625, 382.8125 , 386.71875,
390.625 , 394.53125, 398.4375 , 402.34375, 406.25 ,
410.15625, 414.0625 , 417.96875, 421.875 , 425.78125,
429.6875 , 433.59375, 437.5 , 441.40625, 445.3125 ,
449.21875, 453.125 , 457.03125, 460.9375 , 464.84375,
468.75 , 472.65625, 476.5625 , 480.46875, 484.375 ,
488.28125, 492.1875 , 496.09375, 500. ]), array([ 1.28000000e-01, 2.56000000e-01, 3.84000000e-01, ...,
1.41529600e+03, 1.41542400e+03, 1.41555200e+03]), <matplotlib.image.AxesImage object at 0x000002161A78F898>)
this my code, tese multiple lists comes from the spectrogram of three axis sensor data, to calculate the spectrogram i have calculate the magnitude of the three axis.. what i want is to save the spectrogram output in a more efficient way to use it as an input in another model as a text file
dataset = np.loadtxt("trainingdatasetMAG.txt", delimiter=",")
X = dataset[:,0:6]
Y = dataset[:,6]
fake_size = 1415684
time = np.arange(fake_size)/1000 # 1kHz
base_freq = 2 * np.pi * 100
magnitude = dataset[:,5]
plt.title('xyz_magnitude')
ls=(plt.specgram(magnitude, Fs=1000))
It sounds like you want to flatten the list. Please see this answer, repeated here.
newlist = [item for sublist in mainlist for item in sublist]

Equation for fitting exponential Decay model in scipy

I have a data set, (x and y) that I need to fit with an analytical model in scipy.
I have tried using the MATLAB curve fitting toolbox and a few other tools, including simply adding terms to standard fitting equations (e.g. exponential).
My only real constraint is that I would like to limit the number of parameters that need to be fitted, 4 would be ideal, but 6 could work and I am somewhat sensitive to a model that fits the data set above the linear portion of the curve at the beginning. This is why I don't like the polynominal fitting solutions I have been able to get.
Here is the data for referencs,
Y = np.array([7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.49E-01 , 7.49E-01 , 7.49E-01 , 7.49E-01 , 7.49E-01 , 7.48E-01 , 7.48E-01 , 7.48E-01 , 7.47E-01 , 7.47E-01 , 7.46E-01 , 7.46E-01 , 7.45E-01 , 7.44E-01 , 7.44E-01 , 7.43E-01 , 7.42E-01 , 7.41E-01 , 7.40E-01 , 7.39E-01 , 7.38E-01 , 7.37E-01 , 7.36E-01 , 7.35E-01 , 7.34E-01 , 7.32E-01 , 7.31E-01 , 7.30E-01 , 7.28E-01 , 7.27E-01 , 7.25E-01 , 7.24E-01 , 7.22E-01 , 7.21E-01 , 7.19E-01 , 7.17E-01 , 7.16E-01 , 7.14E-01 , 7.12E-01 , 7.10E-01 , 7.08E-01 , 7.06E-01 , 7.04E-01 , 7.02E-01 , 7.00E-01 , 6.98E-01 , 6.96E-01 , 6.94E-01 , 6.92E-01 , 6.90E-01 , 6.88E-01 , 6.86E-01 , 6.83E-01 , 6.81E-01 , 6.79E-01 , 6.77E-01 , 6.75E-01 , 6.72E-01 , 6.70E-01 , 6.68E-01 , 6.65E-01 , 6.63E-01 , 6.61E-01 , 6.58E-01 , 6.56E-01 , 6.54E-01 , 6.51E-01 , 6.49E-01 , 6.46E-01 , 6.44E-01 , 6.42E-01 , 6.39E-01 , 6.37E-01 , 6.34E-01 , 6.32E-01 , 6.30E-01 , 6.27E-01 , 6.25E-01 , 6.22E-01 , 6.20E-01 , 6.17E-01 , 6.15E-01 , 6.12E-01 , 6.10E-01 , 6.08E-01 , 6.05E-01 , 6.03E-01 , 6.00E-01 , 5.98E-01 , 5.95E-01 , 5.93E-01 , 5.91E-01 , 5.88E-01 , 5.86E-01 , 5.83E-01 , 5.81E-01 , 5.79E-01 , 5.76E-01 , 5.74E-01 , 5.71E-01 , 5.69E-01 , 5.67E-01 , 5.64E-01 , 5.62E-01 , 5.60E-01 , 5.57E-01 , 5.55E-01 , 5.53E-01 , 5.50E-01 , 5.48E-01 , 5.46E-01 , 5.43E-01 , 5.41E-01 , 5.39E-01 , 5.36E-01 , 5.34E-01 , 5.32E-01 , 5.29E-01 , 5.27E-01 , 5.25E-01])
X = np.arange(len(Y))
Lastly, I am using symfit but I am open to using scipy.optimize.curve_fit or other methods if it helps to solve my problem.
Here's an attempt to fit your data with a three-parameter function f. Note that the function is undefined at x=0. However, scipy does manage to provide a reasonable fit for all other points. You may want to replace x in f with something like x+very_small_number to obtain a function that is valid for x=0.
from scipy.optimize import curve_fit
def f(x, a, b, c):
return a + b * (1 - np.exp(-c/x))
sol = curve_fit(f, X, Y, p0 = (.75,.75,200,) )
plt.plot(X, Y, 'o', label = 'data')
plt.plot(X, [f(x,*sol[0]) for x in X], lw = 3, label = 'fit')

Categories