How can I depict this function with numpy.sum? [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 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()

Related

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

Multiplying a vector by different values [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 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.

Numpy For Large Data Sets [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 4 years ago.
Improve this question
For generating a probability density function of some cases, maybe 1 million observations are considered. When I work with numpy array, I was encountered by size limit 32.
Is it too few ?
In this case, how can we store more than 32 elements without distributing the elements into different columns and maybe arrays in arrays ?
import numpy
my_list = []
for i in range(0, 100):
my_list.append(i)
np_arr = numpy.ndarray(np_arr) # ValueError: sequence too large; cannot be greater than 32
When you create an array with numpy.ndarray, the first argument is the shape of the array. Interpreting that list as a shape would indeed give a huge array. If you just want to turn the list into an array, you want numpy.array:
import numpy
my_list = []
for i in range(0, 100):
my_list.append(i)
np_arr = numpy.array(my_list)

I need to implement a ​SparseArray​ with a ​linked list​ implementation [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 5 years ago.
Improve this question
sparse
array objects will have a fixed size ​n ​ set when they are created - attempting to set or get elements larger
than the size of the array should raise an ​IndexError
Use Scipy sparse Matrics e.g. COO sparse matrix
matrix = sparse.coo_matrix((C,(A,B)),shape=(5,5))
Or you can use Pandas sparseArray :
arr = np.random.randn(10)
arr[2:5] = np.nan; arr[7:8] = np.nan
sparr = pd.SparseArray(arr)
I'd bet these are already implemented in numpy or scipy.

find (i,j) location of closest (long,lat) values in a 2D array [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 8 years ago.
Improve this question
I have multiple 2D arrays, three of which are LONG, LAT and HEIGHT. I would like to determine the closest index in these 2D arrays for a given (long,lat).
So, within my 2D HEIGHT array, what is the index of (-43.5,45)?
A few foolish minutes later; I have worked out an answer I believe sufficient:
a = abs( LAT-chosen_lat ) + abs( LONG-chosen_lon )
i,j = np_unravel_index(a.argmin(), a.shape)

Categories