I'm trying to plot a matrix multiplication in python.
I have the matrix A = [[0,-1],[1,1.6]], and x0 = [[5],[-1]].
The task is to plot xn, when I know that xn = A**n * x0, for n = 1, ... ,30
This is my code so far:
import numpy as np
import matplotlib.pyplot as plt
n = 30
A = np.matrix([[0,-1],[1,1.6]])
xn = np.zeros(n)
x0 = np.matrix([[5],[-1]])
for i in range(n):
xn[i]= A**i*x0
plt.plot(xn)
plt.show()
I keep getting the value error: setting an array element with a sequence, and when it works I get a really strange plot, which is probably wrong. Any ideas on how to do it?
I'm not sure if you want to take the matrix to a power by multiplying it by itself or by exponentiating the numbers inside of it. In any case, the reason why your code is throwing a ValueError: setting an array element with a sequence. is because the matrix vector multiplication A**i * x0 returns an array of length two - i.e. a vector.
Maybe you want to plot the vectors that result from the matrix-vector product. In that case, this code should do the trick:
import numpy as np
import matplotlib.pyplot as plt
n = 30
A = np.matrix([[0,-1],[1,1.6]])
xn = np.zeros((n, 2))
x0 = np.matrix([[5],[-1]])
fig, ax = plt.subplots()
for i in range(n):
A = A # A
xn[i, :] = np.squeeze(np.dot(A, x0))
ax.plot([0, xn[i, 0]], [0, xn[i, 1]])
label = r"$A^{%i}$" % (i+1)
ax.annotate(label, xy=(xn[i, 0], xn[i, 1]))
plt.show()
Notice I changed the shape of xn - it's now (nx2) compared to n as in your code. This means the result of the matrix-vector product will fit into xn. The # notation indicates matrix multiply in python3. I also labeled the line in the resulting plot with the power the matrix was taken to. You can see the output vector changing direction as the matrix changes. I think this is a nice example of how matrices (especially 2x2) can be thought of as linear transformations when applied to vectors. This video explains that concept nicely: https://www.youtube.com/watch?v=kYB8IZa5AuE.
Related
As an extension to my previous project, where the equation X[i+1]=R*X[i](1-X[i]) is used to demonstrate a chaotic system (depending on R). Now I'm trying to construct the bifurcation graph.
About the code, I defined the function to do the actual calculations, and extracting the last 100 calculated values (to ensure the equilibrium reached), in order to plot out the bifurcated R vs x[i], I'm appending each R value to a empty X-data list, and multiple (aka, the returned 100 values) x[i] to a Y-data list (so it is actually a nested list...)
The thing is, depending on the R value, x[i] can be either single value (after equilibrium reached) or multiple values. So I was thinking to "purify" the nested Y-data list by numpy.unique() to remove all the replicated values.
Weirdly, when I don't make the extra "purification" step, the code actually works.
But when I put x = np.unique(logistic_calc(R,N)) it throws me a error says ValueError: setting an array element with a sequence.
Below is the code that works...
import numpy as np
import matplotlib.pyplot as plt
R = 0.2
N = 10_000
x0 = 0.5
def logistic_calc(R,N):
x = np.empty(N)
x[0] = x0
for i in range(1, N):
x[i] = R* x[i-1] * (1 - x[i-1])
return x[-100:]
x_lst = []
y_lst = []
for r in np.linspace(0.1,4,100):
R = r
x = logistic_calc(R,N)
x_lst.append(r)
y_lst.append(x)
plt.figure(figsize=(7, 4))
plt.plot(x_lst, y_lst, ls='', marker='.',ms='0.5', c="royalblue")
plt.ylim(0, 1)
plt.grid(c="lightgray")
plt.xlabel(r"$r$")
plt.ylabel(r"$x_n$")
plt.show()
From matplotlib documentation, paragraph "Plotting multiple sets of data":
"If x and/or y are 2D arrays a separate data set will be drawn for every column. If both x and y are 2D, they must have the same shape. If only one of them is 2D with shape (N, m) the other must have length N and will be used for every data set m."
It is not explicitly written that all sublists must have the same length. But it only refers to 2D arrays and not ragged nested sequences. To understand the behavior of plt.plot, just imagine that x and y will be cast into numpy arrays. In your second case, since y_lst contains lists with different lengths, this conversion cannot be made.
So I would go for something like this:
plt.figure(figize=(7, 4))
for r in np.linspace(1, 4, 100):
x = np.unique(logistic_calc(r, N))
plt.plot([r], [x], '.', ms=.5, c="royalblue") # a little bit tricky!
# OR
# plt.plot([r] * len(x), x, '.', ms=.5, c="royalblue")
...
plt.show()
When I run your example with np.unique, I get ...
...
Traceback (most recent call last):
File "test.py", line 29, in <module>
plt.plot(x_lst, y_lst, ls='', marker='.',ms='0.5', c="royalblue")
... more stack trace
ValueError: setting an array element with a sequence.
So the error is clearly happening at line ...
plt.plot(x_lst, y_lst, ls='', marker='.',ms='0.5', c="royalblue")
because the shapes of x_lst and y_lst no longer match up when you use np.unique.
You can get the code to work by looping over each each index of x_lst and y_lst and plotting them separately ...
import numpy as np
import matplotlib.pyplot as plt
R = 0.2
N = 10_000
x0 = 0.5
def logistic_calc(R,N):
x = np.empty(N)
x[0] = x0
for i in range(1, N):
x[i] = R* x[i-1] * (1 - x[i-1])
return x[-100:]
x_lst = []
y_lst = []
for r in np.linspace(0.1,4,100):
R = r
x = logistic_calc(R,N)
x = x.reshape(100)
x_lst.append(r)
y_lst.append(np.unique(x.round(decimals=4)))
plt.figure(figsize=(7, 4))
for x, y in zip(x_lst, y_lst):
plt.plot([x]*len(y), y, ls='', marker='.',ms='0.5', c="royalblue")
plt.ylim(0, 1)
plt.grid(c="lightgray")
plt.xlabel(r"$r$")
plt.ylabel(r"$x_n$")
plt.show()
I have a combinatorics problem that I can't solve.
Given a set of vectors and a target vector, return a scalar for each vector, so that the average of the scaled vectors in the set is closest to the target.
Edit: Weights w_i are in range [0, 1]. This is a constrained optimisation problem:
minimise d(avg(w_i * x_i), target)
subject to sum(w_i) - 1 = 0
If i had to name this problem it would be unbounded subset average.
I have looked at the unbounded knapsack and similar problems, but a dynamic programming implementation seems to be impossible due to the interdependence of the numbers.
I also inplemented a genetic algorithm that is able to approximate the weights moderately well, but it takes too long and I was initially hoping to solve the problem using dynamic programming.
Is there any hope?
Visualization
In a 2D space the solution to the problem can be represented like this
Problem class identification
As recognized by others this is a an optimization problem. You have linear constraints and a convex objective function, it can be cast to quadratic programming, (read Least squares session)
Casting to standard form
If you want to minimize the average of w[i] * x[i], this is sum(w[i] * x[i]) / N, if you arrange w[i] as the elements of a (1 x N_vectors) matrix, and each vector x[i] as the i-th row of a (N_vectors x DIM) matrix, it becomes w # X / N_vectors (with # being the matrix product operator).
To cast to that form you would have to construct a matrix so that each rows of A*x < b expressing -w[i] < 0, the equality is sum(w) = 1 becomes sum(w) < 1 and -sum(w) < -1. But there there are amazing tools to automate this part.
Implementation
This can be readily implemented using cvxpy, and you don't have to care about expanding all the constraints.
The following function solves the problem and if the vectors have dimension 2 plot the result.
import cvxpy;
import numpy as np
import matplotlib.pyplot as plt
def place_there(X, target):
# some linear algebra arrangements
target = target.reshape((1, -1))
ncols = target.shape[1]
X = np.array(X).reshape((-1, ncols))
N_vectors = X.shape[0]
# variable of the problem
w = cvxpy.Variable((1, X.shape[0]))
# solve the problem with the objective of minimize the norm of w * X - T (# is the matrix product)
P = cvxpy.Problem(cvxpy.Minimize(cvxpy.norm((w # X) / N_vectors - target)), [w >= 0, cvxpy.sum(w) == 1])
# here it is solved
print('Distance from target is: ', P.solve())
# show the solution in a nice plot
# w.value is the w that gave the optimal solution
Y = w.value.transpose() * X / N_vectors
path = np.zeros((X.shape[0] + 1, 2))
path[1:, :] = np.cumsum(Y, axis=0)
randColors=np.random.rand( 3* X.shape[0], 3).reshape((-1, 3)) * 0.7
plt.quiver(path[:-1,0], path[:-1, 1], Y[:, 0], Y[:, 1], color=randColors, angles='xy', scale_units='xy', scale=1)
plt.plot(target[:, 0], target[:, 1], 'or')
And you can run it like this
target = np.array([[1.234, 0.456]]);
plt.figure(figsize=(12, 4))
for i in [1,2,3]:
X = np.random.randn(20) * 100
plt.subplot(1,3,i)
place_there(X, target)
plt.xlim([-3, 3])
plt.ylim([-3, 3])
plt.grid()
plt.show();
Good day to you fellow programmer !
Today I would like to do something that I believe is tricky. I have a very large 2D array called tac that basically contains time curve values and a file containing a tuple of coordinates called coor which contains information on where to place these curves in a 3D array. What this set of variables represents is actually a 4D array: the first 3 dimensions represent space dimensions and the fourth is time. The whole thing is stored as is to avoid storing an immense amount of zeros.
I would like to apply, for each time (in other words, each values in the 4th dimension), a gaussian kernel to this set of data. I was able to generate this kernel and to perform the convolution quite easily for a fixed standard deviation for the whole array using scipy.ndimage.convolve. The kernel was created using scipy.signal.gaussian. Here is a brief example of the principle where tac_4d contains the 4D array (stores a lot of data I know... but one problem at the time):
def gaussian_kernel_3d(radius, sigma):
num = 2 * radius + 1
kernel_1d = signal.gaussian(num, std=sigma).reshape(num, 1)
kernel_2d = np.outer(kernel_1d, kernel_1d)
kernel_3d = np.outer(kernel_1d, kernel_2d).reshape(num, num, num)
kernel_3d = np.expand_dims(kernel_3d, -1)
return kernel_3d
g = gaussian_kernel_3d(1, .5)
cag = nd.convolve(tac_4d, g, mode='constant', cval=0.0)
The trick is now to convolve the array with a kernel which standard deviation is different for each SPACE coordinate. In other words, I would have a 3D array std containing standard deviations for each coordinate of the array.
It seems https://github.com/sheliak/varconvolve is the code needed to take care of this problem. However I don't really understand how to use it and quite frankly, I would prefer to come up with a genuine solution. Do you guys see a way to solve this problem?
Thanks in advance !
EDIT
Here is what I hope can be considered MCVE
import numpy as np
from scipy import signal
from scipy import ndimage as nd
def gaussian_kernel_2d(radius, sigma):
num = 2 * radius + 1
kernel_1d = signal.gaussian(num, std=sigma).reshape(num, 1)
kernel_2d = np.outer(kernel_1d, kernel_1d)
return kernel_2d
def gaussian_kernel_3d(radius, sigma):
num = 2 * radius + 1
kernel_1d = signal.gaussian(num, std=sigma).reshape(num, 1)
kernel_2d = np.outer(kernel_1d, kernel_1d)
kernel_3d = np.outer(kernel_1d, kernel_2d).reshape(num, num, num)
kernel_3d = np.expand_dims(kernel_3d, -1)
return kernel_3d
np.random.seed(0)
number_of_tac = 150
time_samples = 915
z, y, x = 100, 150, 100
voxel_number = x * y * z
# TACs in the right order
tac = np.random.uniform(0, 4, time_samples * number_of_tac).reshape(number_of_tac, time_samples)
arr = np.array([0] * (voxel_number - number_of_tac) + [1] * number_of_tac)
np.random.shuffle(arr)
arr = arr.reshape(z, y, x)
coor = np.where(arr != 0) # non-empty voxel
# Algorithm to replace TAC in 3D space
nnz = np.zeros(arr.shape)
nnz[coor] = 1
tac_4d = np.zeros((x, y, z, time_samples))
tac_4d[np.where(nnz == 1)] = tac
# 3D convolution for all time
# TODO: find a way to make standard deviation change for each voxel
g = gaussian_kernel_3d(1, 1) # 3D kernel of std = 1
v = np.random.uniform(0, 1, x * y * z).reshape(z, y, x) # 3D array of std
cag = nd.convolve(tac_4d, g, mode='constant', cval=0.0) # convolution
Essentially, you have a 4D dataset, shape (nx, ny, nz, nt) that is sparse in (nx, ny, nz) and dense in the nt axis. If (i, j, k) are coordinates of nonzero points in the sparse dimensions, you want to convolve with a Gaussian 3D kernel that has a sigma that depends on (i, j, k).
For example, if there are nonzero points at [1, 2, 5] and [1, 4, 5] with corresponding sigmas 0.1 and 1.0, then the output at coordinates [1, 3, 5] is affected mostly by the [1, 4, 5] point because that one has the largest point spread.
Your question is ambiguous; it could also mean that point [1, 3, 5] has a its own associated sigma, for example 0.5, and pulls data from the two adjacent points with equal weight. I will assume the first definition (sigma values associated with input points, not with output points).
Because the operation is not a true convolution, there is no fast FFT-based method to do the entire operation in one operation. Instead, you have to loop over the sigma values. Fortunately, your example has only 150 nonzero points, so the loop is not too expensive.
Here is an implementation. I keep the data in sparse representation as long as possible.
import scipy.signal
import numpy as np
def kernel3d(mm, sigma):
"""Return (mm, mm, mm) shaped, normalized kernel."""
g1 = scipy.signal.gaussian(mm, std=sigma)
g3 = g1.reshape(mm, 1, 1) * g1.reshape(1, mm, 1) * g1.reshape(1, 1, mm)
return g3 * (1/g3.sum())
np.random.seed(1)
s = 2 # scaling factor (original problem: s=10)
nx, ny, nz, nt, nnz = 10*s, 11*s, 12*s, 91*s, 15*s
# select nnz random voxels to fill with time series data
randint = np.random.randint
tseries = {} # key: (i, j, k) tuple; value: time series data, shape (nt,)
for _ in range(nnz):
while True:
ijk = (randint(nx), randint(ny), randint(nz))
if ijk not in tseries:
tseries[ijk] = np.random.uniform(0, 1, size=nt)
break
ijks = np.array(list(tseries.keys())) # shape (nnz, 3)
# sigmas: key: (i, j, k) tuple; value: standard deviation
sigmas = { k: np.random.uniform(0, 2) for k in tseries.keys() }
# output will be stored as dense array, padded to avoid edge issues
# with convolution.
m = 5 # padding size
cag_4dp = np.zeros((nx+2*m, ny+2*m, nz+2*m, nt))
mm = 2*m + 1 # kernel width
for (i, j, k), tdata in tseries.items():
kernel = kernel3d(mm, sigmas[(i, j, k)]).reshape(mm, mm, mm, 1)
# convolution of one voxel by kernel is trivial.
# slice4d_c has shape (mm, mm, mm, nt).
slice4d_c = kernel * tdata
cag_4dp[i:i+mm, j:j+mm, k:k+mm, :] += slice4d_c
cag_4d = cag_4dp[m:-m, m:-m, m:-m, :]
#%%
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2, tight_layout=True)
plt.close('all')
# find a few planes
#ks = np.where(np.any(cag_4d != 0, axis=(0, 1,3)))[0]
ks = ijks[:4, 2]
for ax, k in zip(axs.ravel(), ks):
ax.imshow(cag_4d[:, :, k, nt//2].T)
ax.set_title(f'Voxel [:, :, {k}] at time {nt//2}')
fig.show()
for ijk, sigma in sigmas.items():
print(f'{ijk}: sigma={sigma:.2f}')
I want to study symbolic functions in python. I want to create y(x) = x^2 + 2x + 3 and plot it in the range [1, 255]. I want to use the subs() function to calculate the values by using the for loop. However, when I run that I get this error:
IndexError('list index out of range')
Can you help me please?
import numpy as np
import matplotlib.pyplot as plot
from sympy import *
a = [1,2,3]
x = Symbol('x')
fx = a[0]*x**2 + a[1]*x + a[2]
t = list(range(1,256))
y = np.zeros(256)
for i in t:
y[i] = fx.subs({x:t[i]})
plot.plot(t,y)
plot.show()
Just replace with the following lines:
y = np.zeros(len(t))
for i in range(len(t)):
y[i] = fx.subs({x:t[i]})
The problem was that the length of t was only 255 but the len of y was 256 in your code because you define y = np.zeros(256), hence the Index Error because there is no t[256]. I am using y = np.zeros(len(t)) because you have as many y points as t (or x) points. By the way, you are most likely to get an error in your plot command the way it is right now because you have called import matplotlib.pyplot as plot. I would simply call it plt instead of plot
Output
I am trying to draw the phase space plot for a certain dynamical system. In effect, I have a 2d plane in which there is a starting point followed by next point and so on. I want to connect these points with lines and on top of that I want to draw some arrows so that I would be able to see the direction (starting point to the next point etc). I decided to use linetype '->' to achieve this but it doesn't give any good result and arrows actually seem to point in wrong direction many times. Also they are quite closely spaced and hence I can't see the individual lines.
My code is given below:
import numpy as np
import matplotlib.pylab as plt
from scipy.integrate import odeint
def system(vect, t):
x, y = vect
return [x - y - x * (x**2 + 5 * y**2), x + y - y * (x**2 + y**2)]
vect0 = [(-2 + 4*np.random.random(), -2 + 4*np.random.random()) for i in range(5)]
t = np.linspace(0, 100, 1000)
for v in vect0:
sol = odeint(system, v, t)
plt.plot(sol[:, 0], sol[:, 1], '->')
plt.show()
The resulting plot is shown below:
As can be seen, the arrows are not properly aligned to the lines that connect the points. Also, many arrows are "going out" and I want them to "come in" because the next point always lies towards the close loop at the middle. Moreover, plot looks too messy and I would like to plot fewer arrows so that plot would look better. Does anybody have any idea as how to do it? Thanks in advance.
I think a solution would then look like this:
Using that code:
import numpy as np
import matplotlib.pylab as plt
from scipy.integrate import odeint
from scipy.misc import derivative
def system(vect, t):
x, y = vect
return [x - y - x * (x**2 + 5 * y**2), x + y - y * (x**2 + y**2)]
vect0 = [(-2 + 4*np.random.random(), -2 + 4*np.random.random()) for i in range(5)]
t = np.linspace(0, 100, 1000)
color=['red','green','blue','yellow', 'magenta']
plot = plt.figure()
for i, v in enumerate(vect0):
sol = odeint(system, v, t)
plt.quiver(sol[:-1, 0], sol[:-1, 1], sol[1:, 0]-sol[:-1, 0], sol[1:, 1]-sol[:-1, 1], scale_units='xy', angles='xy', scale=1, color=color[i])
plt.show(plot)
[EDIT: Some explanation on indices:
A definition of quiver and its arguments can be found here: https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.quiver
Good examples for quiver can be found here: https://www.getdatajoy.com/examples/python-plots/vector-fields
quiver requires vectors as inputs, which are defined by a start and end points (start and end points are basically points i and i+1 from the line coordinates stored in sol)
As a consequence, the length of the vector array will be one shorter than the length of the coordinate array
In order to compensate for that and to provide arrays with the same length for coordinates and vectors to quiver, we have to play with indices as follows:
sol[:-1, 0] (:-1 in first index drops the last coordinate)
sol[1:, 0] (1: in first index starts drops first coordinate)
sol[1:, 0] - sol[:-1, 0] is therefore a convenient way to create two vectors of length n-1 and subtract them in a way that the result is sol[i+1] - sol[i]