I have following code
round_subset_list = subset.round(2).values.tolist()
print(round_subset_list)
The result is
[0.47, -0.36, -0.5, 0.2, 0.35, 1.82, -0.78, -0.91, 0.36, -1.74, 0.24, 0.76, 0.57, 2.32, 1.55, -1.31, -0.09, -0.02, -0.07, -0.19, -0.25, -1.09, 0.64, 1.22, -0.56, 1.76, 0.13, 1.33, -0.74, -1.15, 1.63, 1.04, -0.26, 0.02, -1.2, 0.37, 0.43, 0.04, 1.34, 0.57, 0.76, -1.25, -0.05, 0.12, 0.8, -0.99, -0.11, -0.54, -0.08, -0.04, -0.76, -0.8, 0.35, 1.54, -0.99, -0.35, -0.28, 0.45, -0.04, -0.06, 0.02, 0.58, -0.32, -0.1, 0.28, 0.3, -0.36, 0.81, 0.79, 0.21, 1.81, 0.19, 0.84, 0.2, -0.06, -0.11, -1.4, -2.08, 0.88, -0.14, -0.96, 1.3, 0.06, -0.37, 1.49, -0.91, 1.14, -1.05, 1.49, -0.79, 2.02, 0.38, 2.4, 1.25, 0.5, 1.11, -0.54, -0.1, 0.63, 1.01]
I wanna convert them into something look like ['0.47', '-0.36', '-0.5', '0.2', ...]
subset.string = ''.join(str(e) for e in round_subset_list)
print(subset.string)
The above code doesn't work
Try this.
round_subset_list = [str(i) for i in round_subset_list]
You can use the map fuction.
round_subset_list = [0.47, -0.36, -0.5, 0.2, 0.35, 1.82, -0.78, -0.91, 0.36, -1.74, 0.24, 0.76, 0.57, 2.32, 1.55, -1.31, -0.09, -0.02, -0.07, -0.19, -0.25, -1.09, 0.64, 1.22, -0.56, 1.76, 0.13, 1.33, -0.74, -1.15, 1.63, 1.04, -0.26, 0.02, -1.2, 0.37, 0.43, 0.04, 1.34, 0.57, 0.76, -1.25, -0.05, 0.12, 0.8, -0.99, -0.11, -0.54, -0.08, -0.04, -0.76, -0.8, 0.35, 1.54, -0.99, -0.35, -0.28, 0.45, -0.04, -0.06, 0.02, 0.58, -0.32, -0.1, 0.28, 0.3, -0.36, 0.81, 0.79, 0.21, 1.81, 0.19, 0.84, 0.2, -0.06, -0.11, -1.4, -2.08, 0.88, -0.14, -0.96, 1.3, 0.06, -0.37, 1.49, -0.91, 1.14, -1.05, 1.49, -0.79, 2.02, 0.38, 2.4, 1.25, 0.5, 1.11, -0.54, -0.1, 0.63, 1.01]
print(list(map(str, round_subset_list)))
Related
I tried to fit a dataset with exp decay, curve_fit raise this error "'Covariance of the parameters could not be estimated' ". Then I followed this link to try another package, still gives weird estimations.
scipy curve_fit raises "OptimizeWarning: Covariance of the parameters could not be estimated"
X: array([18827., 18828., 18829., 18830., 18831., 18832., 18833., 18834.,
18835., 18836., 18837., 18838., 18839., 18840., 18841., 18842.,
18843., 18844., 18845., 18846., 18847., 18848., 18849., 18850.,
18851., 18852., 18853., 18854., 18855., 18856., 18857., 18858.,
18859., 18860., 18861., 18862., 18863., 18864., 18865., 18866.,
18867., 18868., 18869., 18870., 18871., 18872., 18873., 18874.,
18875., 18876., 18877., 18878., 18880., 18881., 18882., 18883.,
18884., 18885., 18886., 18887., 18889., 18891., 18892., 18893.,
18894., 18895., 18896., 18897., 18898., 18899., 18900., 18901.,
18902., 18903., 18904., 18905., 18906., 18907., 18908., 18909.,
18910., 18911., 18912., 18913., 18914., 18915., 18916., 18917.,
18918., 18919., 18920., 18921., 18922., 18923., 18924., 18925.,
18926., 18927., 18928., 18929., 18930., 18931., 18932., 18933.,
18934., 18935., 18936., 18937., 18938., 18939., 18940., 18941.,
18942., 18943., 18944., 18945., 18946., 18947., 18948., 18949.,
18950., 18951., 18952., 18953., 18954., 18955., 18956., 18957.,
18958., 18959., 18960., 18961., 18962., 18963., 18964., 18965.,
18966., 18967., 18968., 18969., 18970., 18971., 18972., 18973.,
18974., 18975., 18976., 18977., 18978., 18979., 18980., 18981.,
18982., 18983., 18984., 18985., 18986., 18987., 18988., 18989.,
18990., 18991., 18992., 18993., 18994., 18995., 18996., 18997.,
18999., 19000., 19001., 19002., 19003., 19004., 19005., 19006.,
19007., 19008., 19009., 19010., 19011., 19012., 19013., 19014.,
19015., 19016., 19017., 19018., 19019., 19020., 19021., 19022.])
y: array([0.25, 0.24, 0.25, 0.27, 0.27, 0.27, 0.26, 0.26, 0.26, 0.26, 0.26,
0.3 , 0.3 , 0.3 , 0.3 , 0.3 , 0.3 , 0.31, 0.31, 0.31, 0.31, 0.31,
0.31, 0.35, 0.35, 0.35, 0.35, 0.35, 0.32, 0.32, 0.32, 0.32, 0.32,
0.32, 0.32, 0.32, 0.32, 0.32, 0.32, 0.28, 0.28, 0.28, 0.28, 0.29,
0.28, 0.25, 0.25, 0.25, 0.25, 0.26, 0.25, 0.25, 0.24, 0.24, 0.24,
0.24, 0.22, 0.22, 0.23, 0.23, 0.23, 0.24, 0.24, 0.24, 0.24, 0.24,
0.24, 0.25, 0.24, 0.24, 0.25, 0.25, 0.25, 0.25, 0.25, 0.24, 0.24,
0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24,
0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24,
0.23, 0.23, 0.23, 0.25, 0.25, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23,
0.23, 0.21, 0.21, 0.2 , 0.2 , 0.2 , 0.21, 0.21, 0.21, 0.21, 0.21,
0.21, 0.21, 0.21, 0.19, 0.19, 0.19, 0.2 , 0.2 , 0.2 , 0.2 , 0.19,
0.19, 0.2 , 0.2 , 0.2 , 0.2 , 0.2 , 0.2 , 0.2 , 0.2 , 0.2 , 0.2 ,
0.2 , 0.22, 0.22, 0.22, 0.2 , 0.2 , 0.2 , 0.2 , 0.2 , 0.19, 0.19,
0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.24, 0.25, 0.25, 0.25,
0.25, 0.26, 0.26, 0.31, 0.31, 0.31, 0.33, 0.33, 0.33, 0.32, 0.29,
0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29,
0.29, 0.29, 0.26, 0.26, 0.26])
Could someone guide me through, please?
This warning is telling you that one or more variables are not actually changing the fit. Based on your parameter report and the range of data, the problem is almost certainly because your initial values are so far off as to be useless starting points for refinement. It also seems very likely that the data is not explained well by an exponential decay.
To do curve fitting you must start with plausible initial values for all parameters that are not absurdly far off. Plot your data with your initial model before starting your fit.
This question already has answers here:
Cosinus of an array in Python
(3 answers)
Closed 1 year ago.
so this is the code:
def s(t):
return math.cos(2*(math.pi)*10*t)
s1_n = s(n1T)
error message: "only size-1 arrays can be converted to Python scalars"
this is the output for
n1T
"array([0. , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ,
0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 , 0.21,
0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 , 0.31, 0.32,
0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 , 0.41, 0.42, 0.43,
0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 , 0.51, 0.52, 0.53, 0.54,
0.55, 0.56, 0.57, 0.58, 0.59, 0.6 , 0.61, 0.62, 0.63, 0.64, 0.65,
0.66, 0.67, 0.68, 0.69, 0.7 , 0.71, 0.72, 0.73, 0.74, 0.75, 0.76,
0.77, 0.78, 0.79, 0.8 , 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87,
0.88, 0.89, 0.9 , 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98,
0.99, 1. ])"
this are the used modules:
from thkdsp import * from dsplab import * from numpy import arange,
shape, array, zeros, size, ones, isscalar %pylab inline from
thkdsp.interpolation import ideal_interpolation from
thkdsp.interpolation import ideal_interpolation_mat
how to solve this?
-thanks!!
Found it myself :)
Just use numpy.cos(...) and it works perfectly.
I am trying to check for shared elements between two arrays. Here are the arrays and how they are formed:
# get range between two values from a df
d = pd.DataFrame([[0.06, 0.81]], columns=['start','stop'])
# rounding to 2 to enforce 2 sig digits
a = np.arange(
np.around(d.iloc[0]['start'], 2),
np.around(d.iloc[0]['stop'], 2),
.01
)
and the other array:
b = np.around(0.6999999999999993,2), np.around(1.2400000000000002,2)
b = np.arange(
b[0],
b[1],
.01
)
Now, I want to check if they share any values:
bool(set(a) & set(b))
This gives me False. It should be True. It should be true because this is what a and b look like printed out:
# a
array([0.06, 0.07, 0.08, 0.09, 0.1 , 0.11, 0.12, 0.13, 0.14, 0.15, 0.16,
0.17, 0.18, 0.19, 0.2 , 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27,
0.28, 0.29, 0.3 , 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38,
0.39, 0.4 , 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49,
0.5 , 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ,
0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 , 0.71,
0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ])
# b
array([0.7 , 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ,
0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 , 0.91,
0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. , 1.01, 1.02,
1.03, 1.04, 1.05, 1.06, 1.07, 1.08, 1.09, 1.1 , 1.11, 1.12, 1.13,
1.14, 1.15, 1.16, 1.17, 1.18, 1.19, 1.2 , 1.21, 1.22, 1.23])
I get this weird behavior where if I print out a and b in my jupyter notebook, and then copy and paste the results into new variables and rerun the test, I get True. See below:
a = [0.06, 0.07, 0.08, 0.09, 0.1 , 0.11, 0.12, 0.13, 0.14, 0.15, 0.16,
0.17, 0.18, 0.19, 0.2 , 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27,
0.28, 0.29, 0.3 , 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38,
0.39, 0.4 , 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49,
0.5 , 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ,
0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 , 0.71,
0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ]
b = [0.7 , 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ,
0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 , 0.91,
0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. , 1.01, 1.02,
1.03, 1.04, 1.05, 1.06, 1.07, 1.08, 1.09, 1.1 , 1.11, 1.12, 1.13,
1.14, 1.15, 1.16, 1.17, 1.18, 1.19, 1.2 , 1.21, 1.22, 1.23]
bool(set(a) & set(b))
This made me think that the arrays needed to be lists, so I ran this with the original a and b data:
bool(set(list(a)) & set(list(b)))
but still False.
Any ideas?
how I can create matrix using python numpy such this
array([[ 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ],
[ 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 ],
[ 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ],
[ 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ],
[ 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 ],
[ 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ],
[ 0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ],
[ 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ],
[ 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 ],
[ 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. ]])
Do this:
np.linspace(0.01, 1, 100).reshape(10, 10)
output:
array([[0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ],
[0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 ],
[0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ],
[0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ],
[0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 ],
[0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ],
[0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ],
[0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ],
[0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 ],
[0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. ]])
import numpy as np
np.array([[(i+j)/100 for i in range(1, 11)] for j in range(0, 100, 10)])
I have a list of dictionaries:
wt
Out[189]:
[defaultdict(int,
{'A01': 0.15,
'A02': 0.17,
'A03': 0.13,
'A04': 0.17,
'A05': 0.01,
'A06': 0.12,
'A07': 0.15,
'A08': 0.0,
'A09': 0.02,
'A10': 0.09}),
defaultdict(int,
{'A01': 0.02,
'A02': 0.02,
'A03': 0.06,
'A04': 0.08,
'A05': 0.08,
'A06': 0.04,
'A07': 0.02,
'A08': 0.24,
'A09': 0.34,
'A10': 0.1}),
defaultdict(int,
{'A01': 0.0,
'A02': 0.12,
'A03': 0.01,
'A04': 0.01,
'A05': 0.11,
'A06': 0.13,
'A07': 0.1,
'A08': 0.36,
'A09': 0.13,
'A10': 0.03})]
And I have another dictionary:
zz
Out[188]: defaultdict(int, {'S1': 0.44, 'S2': 0.44, 'S3': 0.12})
I need to run a loop to aggregate the following computation:
'S1':0.44 * 'A01':0.15 + 'S2':0.44 * 'A01':0.02 + 'S3':0.12 * 'A01':0.00 ----- to be stored in a dict with the key 'A01'
'S1':0.44 * 'A02':0.17 + 'S2':0.44 * 'A02':0.02 + 'S3':0.12 * 'A02':0.12 ----- to be stored in a dict with the key 'A02'
.
.
.and so on upto:
'S1':0.44 * 'A10':0.09 + 'S2':0.44 * 'A10':0.1 + 'S3':0.12 * 'A10':0.03 ----- to be stored in a dict with the key 'A10'
Can somebody please suggest a loop for this? The issue I'm facing is that:
wt[0]
Out[197]:
defaultdict(int,
{'A01': 0.15,
'A02': 0.17,
'A03': 0.13,
'A04': 0.17,
'A05': 0.01,
'A06': 0.12,
'A07': 0.15,
'A08': 0.0,
'A09': 0.02,
'A10': 0.09})
But:
wt[0][0]
Out[199]: 0
I'm not being able to access each value within the dict.
You can do your aggregation with a dict comprehension:
x = [defaultdict(int, {'A01': 0.15, 'A02': 0.17, 'A03': 0.13, 'A04': 0.17, 'A05': 0.01, 'A06': 0.12, 'A07': 0.15, 'A08': 0.0, 'A09': 0.02, 'A10': 0.09}),
defaultdict(int, {'A01': 0.02, 'A02': 0.02, 'A03': 0.06, 'A04': 0.08, 'A05': 0.08, 'A06': 0.04, 'A07': 0.02, 'A08': 0.24, 'A09': 0.34, 'A10': 0.1}),
defaultdict(int, {'A01': 0.0, 'A02': 0.12, 'A03': 0.01, 'A04': 0.01, 'A05': 0.11, 'A06': 0.13, 'A07': 0.1, 'A08': 0.36, 'A09': 0.13, 'A10': 0.03})]
mult = defaultdict(int, {'S1': 0.44, 'S2': 0.44, 'S3': 0.12})
d = {k: sum(d[k] * mult['S'+str(idx+1)]
for idx, d in enumerate(x)) for k in x[0].keys()}
If you want to multiply your matrix with a vector, you should try numpy:
import numpy as np
# Transform data to matrix
x = np.array([[d['A'+str(i+1).zfill(2)] for i in range(len(d))] for d in x])
v = np.array([mult['S'+str(i+1)] for i in range(len(mult))]).reshape(1, 3)
print(np.matmul(v, x))
# [[0.0748 0.098 0.0848 0.1112 0.0528 0.086 0.0868 0.1488 0.174 0.0872]]