Multiplying a vector by different values [closed] - python

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.

Related

Smart rounding an array in Python [closed]

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]

python coding from matlab [closed]

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 this code in MATLAB and I am trying to convert it in Python.
A=[1,-0.75,0.25]
yc(1:45)=-2;
y(1:6)=0
u(1:6)=0
[lig,col]=size(A);
alpha(1)=1;
alpha(2)=A(2)-1;
if(col>2)
for i=3:col
alpha(i)=A(i)-A(i-1);
end ;
end;
alpha(col+1)=-A(col);
I don't know how to convert it in python thnx for helping me
It would be better if your code would have been a minimal example of what you are trying to do. You are defining variables that are not even used. But here's a more or less literal translation. Note that you probably want to preallocate alpha (both in Matlab and Python)
import numpy as np
A = np.array([1.0, -.75, .25])
yc = -2 * np.ones(45)
y = np.zeros(6)
u = np.zeros(6)
col = A.size
alpha = np.array([1, A[1] - 1])
if col > 2:
for i in range(2, col):
alpha = np.append(alpha, A[i] - A[i-1])
alpha = np.append(alpha, -A[col-1])

How can I depict this function with numpy.sum? [closed]

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 would like to calculate this sum
using numpy.sum - how can I do that?
Phi is a function that takes one parameter, y and x are vectors of length i
Start with x and y as numpy arrays
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
You will pass in the array,
r = y - (theta_0*x + theta_1)
Define your function, and hopefully you can do vectorized operations within the function, but whatever, the function needs to return a numpy vector if you want to use numpy.sum
def Phi(r):
a = r*r # and example operation
return a # returning a numpy array
Then call the function and sum:
R = Phi(r).sum()

Efficient way to subtract 2 list in python with certain conditions [closed]

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 3 years ago.
Improve this question
I Have 2 lists
A=[1,1.5,2.3,4,5]
B=[1.6,2.6,3.3,4.4,5.5]
I want to do B-A such that index i of B gets subtracted from the index (i+1) of A, and if the value returned is positive then we will store the value else we will put 0.
For the above case, the resultant list will look like
C = [0, .1, .3, 0, 0]
Also, the 0th index of the resultant list should always be 0
You can use zip here.
A=[1,1.5,2.3,4,5]
B=[1.6,2.6,3.3,4.4,5.5]
res=[0]+[round(x-y,1) if x-y>0 else 0 for x,y in zip(B,A[1:])]
print(res)
#[0, 0.1, 0.3, 0, 0]
For numpy arrays use np.concatenate and np.maximum as suggested in comments by #hpaulj.
import numpy as np
res=np.concatenate(([0],np.maximum(0, B[:-1]-A[1:])))
print(res)
#array([0. , 0.1, 0.3, 0. , 0. ])

How should the values of a list be scaled such that they meet standard deviation and mean requirements? [closed]

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

Categories