Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I'd like to make a function that rounds integers or floats homogeneously and smart. For example, if I have an array like:
[0.672, 0.678, 0.672]
my output would be:
[0.67, 0.68, 0.67]
but also if I have this kind of input:
[17836.982, 160293.673, 103974.287]
my output would be:
[17836, 160293, 103974]
But at the same time, if my array only has close together values such as:
[17836.987, 17836.976, 17836.953]
The output would be:
[17836.99, 17836.98, 17836.95]
An automated way could be to compute all absolute differences, getting the min and finding out the number of decimal places to keep to maintain a representative difference.
This doesn't give the exact output you want but follows the general logic.
Here using numpy to help on the computation, the algorithm is O(n**2):
def auto_round(l, round_int_part=False):
import numpy as np
a = np.array(l)
b = abs(a-a[:,None])
np.fill_diagonal(b, float('inf'))
n = int(np.ceil(-np.log10(b.min())))
# print(f'rounding to {n} decimals') # uncomment to get info
if n<0:
if not round_int_part:
return a.astype(int).tolist()
return np.round(a, decimals=n).astype(int).tolist()
return np.round(a, decimals=n).tolist()
auto_round([17836.987, 17836.976, 17836.953])
# [17836.99, 17836.98, 17836.95]
auto_round([0.6726, 0.6785, 0.6723])
# [0.6726, 0.6785, 0.6723]
auto_round([17836.982, 160293.673, 103974.287])
# [ 17836, 160293, 103974]
auto_round([17836.982, 160293.673, 103974.287], round_int_part=True)
# [20000, 160000, 100000]
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I was trying to create this [80000, 104000, 135000...] list in Python. Its the value, starting at 80,000 multiplied by 1.3 each time I want
What i've tried:
a = [num*1.5 for num in ??? if num>=80000] #???--> i've tried range(10)
I should be able to do this but I can't find any solutions rn..
I must use list-comprehensions, if possible.
Some help would be nice, thank you!
There is a very basic mathematical operation that represents multiplying by the same value many time: power.
a = [80000 * (1.3**n) for n in range(100)]
You could write your own generator then use that in conjunction with a list comprehension.
def numgen(start, factor, limit):
for _ in range(limit):
yield int(start)
start *= factor
mylist = [value for value in numgen(80_000, 1.3, 10)]
print(mylist)
Output:
[80000, 104000, 135200, 175760, 228488, 297034, 386144, 501988, 652584, 848359]
import numpy as np
print(80000 * 1.3**np.arange(3))
# [ 80000. 104000. 135200.]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a array, where i would like to multiply the elements inside the array by themselves (product) and i have the following vector to be multiplied by the input vector: test_vector = array([0.1, 0.3, 0.4, 0.5, 0.6).
I am looking for an easy way to automate this task
NumPy solution:
import numpy as np
arr_1 = np.array([1, 0, 1, 0, 1])
arr_2 = np.array([0.42, 0.53, 0.62, 0.60, 0.69])
res = np.prod(np.where(arr_1, arr_2, 1 - arr_2))
print(res)
Output:
0.033779088
There might be a more efficient way.
I'm not quite sure what you're calling that first array. I'm calling it signs.
result = 1
for sign, value in zip(signs, test_vector):
result *= (value if sign == 0 else 1 - value)
If these vectors are long rather than short examples as given here, you might want to switch to using numpy.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm facing a problem.
I have two elements as follow :
[array([130.05297852, 159.25004578, 140.36545944]),
array([115.27301025, 160.63392258, 132.83247375])]
and
[39.44091796875,
52.175140380859375]
and I would like to have something like that :
[array([130.05297852, 159.25004578, 140.36545944, 39.44091796875]),
array([115.27301025, 160.63392258, 132.83247375, 52.175140380859375])]
How can I manage to do this ? Thanks !
You can append elements with the append function.
for i in range(len(small_array)):
bigger_array[i].append(small_array[i])
this appends the first element to the first array, and the second element to the second array.
EDIT:
with numpy arrays you can adapt the previous method in this way:
for i in range(len(small_array):
np.append(bigger_array[i], small_array[i])
import numpy as np
a = [
np.array([130.05297852, 159.25004578, 140.36545944]),
np.array([115.27301025, 160.63392258, 132.83247375])
]
add_to_a = np.array([39.44091796875, 52.175140380859375])
result = []
for i, j in zip(a, add_to_a):
final = np.append(i, j)
result.append(final)
print(result) # If you need a normal array
result = np.array(result) # Making ND Array
print(result)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have lists of values that I want to have scaled to meet certain standard deviation and mean requirements. Specifically, I want the datasets standardised to mean 0 with standard deviation 1, except for datasets for which all values are greater than 0; these I want scaled such that their mean is 1.
What would be a good way to do this type of thing in Python?
If you're working with data in Python, you're going to want to be using the science stack (see here), in particular numpy, scipy, and pandas. What you're looking for is the zscore, and that's a common enough operation that it's built-in to scipy as scipy.stats.zscore.
Starting from a random array with non-zero mean and non-unity stddev:
>>> import numpy as np
>>> import scipy.stats
>>> data = np.random.uniform(0, 100, 10**5)
>>> data.mean(), data.std()
(49.950550280158893, 28.910154760235972)
We can renormalize:
>>> renormed = scipy.stats.zscore(data)
>>> renormed.mean(), renormed.std()
(2.0925483568134951e-16, 1.0)
And shift if we want:
>>> if (data > 0).all():
... renormed += 1
...
>>> renormed.mean(), renormed.std()
(1.0000000000000002, 1.0)
We could do this manually, of course:
>>> (data - data.mean())/data.std()
array([-0.65558504, 0.24264144, -0.1112242 , ..., -0.40785103,
-0.52998332, 0.10104563])
(Note that by default this uses a delta degrees of freedom of 0, i.e. the denominator is N. If you want N-1, pass ddof=1).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm doing Bayesian inference (manually, using a grid search) in Python. I want to calculate the probability of each model given the data. The problem is I can only calculate the 'evidence' in log, otherwise its 0.
So, even though its between 0-1, I can't get the results for:
Pr(data|model1) / (Pr(data|model1) + Pr(data|model2))
Since each term is 0 in its non-log form.
Any ideas?
Thanks
Let logpr1 and logpr2 be log(data|model1) and log(data|model2), respectively, and suppose
In [57]: logpr1 = -802
In [58]: logpr2 = -800
If you try to express those as probabilities (not logarithms of probabilities), you get 0:
In [59]: np.exp(logpr2)
Out[59]: 0.0
Now you want to compute
log(Pr(data|model1) / (Pr(data|model1) + Pr(data|model2))),
which you can also write as
log(Pr(data|model1)) - log(Pr(data|model1) + Pr(data|model2)).
For the last term, you can use the function numpy.logaddexp (which is the essential tip in this answer; see also scipy.misc.logsumexp). So your calculation is:
In [60]: logp = logpr1 - np.logaddexp(logpr1, logpr2)
In [61]: logp
Out[61]: -2.1269280110429918
In this case, that number is not very small. In fact, you can express it as a plain probability:
In [62]: np.exp(logp)
Out[62]: 0.11920292202211526