Numpy array using expression - python

how can I create an np array using expression y1 = x, when x array is already defined
x = [1,2,5,7]
from this array x , I would like to create another array y1 using the expression
y1 = x
using numpy

If you want a copy of the array it would be
import numpy as np
y1 = np.array(x)
Currently you just assign the list from x to y1. With this you create a new numpy array with the values from x.

Related

How to find slope of LinearRegression using sklearn on python?

I'm newbie in python and I would like to find slope and intercept using sklearn package. Below is my code.
import numpy as np
from sklearn.linear_model import LinearRegression
def findLinearRegression():
x = [1,2,3,4,5]
y = [5,7,12,9,15]
lrm = LinearRegression()
lrm.fit(x,y)
m = lrm.coef_
c = lrm.intercept_
print(m)
print(c)
I got an error ValueError: Expected 2D array, got 1D array instead. Any advice or guidance on this would be greatly appreciated, Thanks.
You'll need to reshape the x and y series to a 2D array.
Replace the code where you declare x and y with the below code and the function would work the way intended.
x = np.array([1,2,3,4,5]).reshape(-1, 1)
y = np.array([5,7,12,9,15]).reshape(-1, 1)
x should be a column vector
x = np.array([1,2,3,4,5]).reshape(-1,1)
You need to reshape your inputs. Simply replace
x = [1,2,3,4,5]
by
x = np.array([1,2,3,4,5]).reshape(-1, 1)

How to subtrac a numpy array element-by-elemnt by another numpy array

I have two numpy arrays, with just the 3-dimensional coordinates of two molecules.
I need to implement the following equation, and I'm having problems in the subtraction of each coordinate of one of the arrays by the second, and then square it.
I have tried the following, but since I'm still learning I feel that I am making some major mistake. The simple code I use is:
a = [math.sqrt(1/3*((i[:,0]-j[:,0])**2) + ((i[:,1] - j[:,1])**2) + ((i[:,2]-j[:,2])**2) for i, j in zip(coordenates_2, coordenates_1))]
It's numpy you can easily do it using the following example:
import numpy as np
x1 = np.random.randn(3,3,3)
x2 = np.random.randn(3,3,3)
res = np.sqrt(np.mean(np.power(x1-x2,2)))

How to go from numpy 3d mgrid to position array

I am trying to generate a multi variate gaussian that will give me an output based on 3 coordinates, x,y and z. I want each coordinate to take on a value between 0 and 199 inclusive.
I am not sure how to go from x, y and z as Ive got defined below, to an array size 200^3 x 3, which contains all the positions or coordinates xyz.
I need an array of positions so that I can pass it as a parameter for the scipy multivariate_normal.pdf function.
import numpy as np
from scipy.stats import multivariate_normal
x, y, z= np.mgrid[0:200,0:200,0:200]
mu = np.array([100,100,100])
covar = np.array([[100,0,0],[0,100,0],[0,0,100]])
It turns out numpy as a function called vstack which does the job.
import numpy as np
from scipy.stats import multivariate_normal
x,y,z = np.mgrid[0:200,0:200,0:200]
xyz = np.vstack((x.flat,y.flat,z.flat)).T
mu = np.array([100,100,100])
covar = np.array([[100000,0,0],[0,100000,0],[0,0,100000]])
pdf = multivariate_normal.pdf(xyz,mu,covar)
pdf = pdf.reshape(200,200,200)

Passing Empty Index

I have python code that implements something similar to the following:
import numpy as np
x = np.array([1,1,0,2,1,4])
ind = np.array([0,3,5])
def foo(x, ind):
x1 = x[ind]
x2 = np.delete(x, ind)
return x1, x2
foo(x, ind)
The vector x is passed by a user to the function and within the function x is split into two vectors based on some predetermined indices in ind. Then, some work later in the function is performed on the vectors x1 and x2 (conditionally if they exist)
Conceptually there are 3 possible outcomes and currently I can make only two of them work based on how I have written this code. Outcome 1) All values are assigned to x1 and nothing is assigned to x2. This is done when, for example, the value for ind = range(6) using the sample code above. Outcome 2) some values are assigned to x1 and the remaining others are assigned to x2. This outcome is the code in the example provided above.
However, what I cannot figure out is outcome 3) all values are assigned to x2 and nothing is assigned to x1. Within the context of how I have written this code, is it possible to assign some value to ind such that everything in the vector x would be assigned to x2 and nothing would be assigned to x1?
Thank you for your support.
Do you want to split numpy array like below? Then you can do this.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
ind = 4
newarr = arr[0:ind]
print(newarr)

Creating new 2D array from a slice of 3D array in python?

I have a 3D array [256,256,450] that I would like to fetch a 2D array from a cross section along the z axis. The new 2D array should start at z=0 at the top and have the values across some i'th y slice for x =0 to x=255. Then the next row in the new 2D array should be the same for z=1, and so on until z=449. How can this be done?
Use NumPy's NDArray class and slicing syntax.
import numpy as np
my_array = np.zeros([256, 256, 450]) # 256x256x450 array
... # Do whatever you want to do to load data in
x_slice = my_array[0,:,:] # A 256x450 array, the first element in x
y_slice = my_array[:,0,:] # A 256x450 array, the first element in y
y_slice = my_array[:,99,:] # A 256 x 450 array, the 100th element in y
import numpy as np
array_3d = np.ones((256, 256, 450))
y_layer = 24 # arbitrary y layer
array_2d = array_3d[:, y_layer, :]

Categories

Resources