Spyder compiler displays wrong output - python

So I am trying to run the following code:
import numpy as np
import numpy.linalg as la
x = np.array ( [ [1, 0, 1], [0, 1, 1], [0, 0, 1], [1, 1, 1]] )
y = np.array ( [1, 1, 0, 0] )
# solve using normal equations:
x_transpose = np.transpose(x) #calculating transpose
x_transpose_dot_x = x_transpose.dot(x) # calculating dot product
temp_1 = la.inv(x_transpose_dot_x) #calculating inverse
temp_2 = x_transpose.dot(y)
theta = temp_1.dot(temp_2)
print(theta)
The output using spyder IDE:
[2.22044605e-16 1.11022302e-16 5.00000000e-01]
The output using collab or py IDE:
[0.00000000e+00 2.22044605e-16 5.00000000e-01]
Why is spyder producing a wrong output? I like using spyder tho!

Related

Is the a workaraound for dividing two variables in a DCP optimization problem?

I am new to the world of CVXPY and have run in to an issue with an optimization problem. In it I need to derive an expression based on the optimized variable and dividing the variable with the expression - simplified but like this:
import cvxpy as cvx
x = cvx.Variable(1,nonneg=True)
y = cvx.sqrt(x)
print("y is DCP:" + str(y.is_dcp()))
z = x/y # y is x dependent so not dcp
print("z is DCP:" + str(z.is_dcp()))
objective = cvx.Maximize(cvx.sum(z))
probl = cvx.Problem(objective, [x<=10])
probl.solve(verbose=True)
Looking at the rules for DCP optimization I realize that variable/variable division is not DCP. My question if therefore if someone has a solution or workI am noew to the world ov cvxpy and have d for this issue?
Inputing a constant in place for y in z obviously fixes the issue. However, I need to optimize on an expression based on the variable. Is there a way to do this?
I added the example above for simplicity, but my problem is more in line with the following:
import numpy as np
import cvxpy as cvx
import warnings
warnings.simplefilter(action='ignore')
ratio = np.array([-1.95844359, -7.14519994, 0.08811825, 2.92089828, 2.87685278,
-3.13022284, -1.12513724, 3.72449473, -2.68733876, 2.31347068,
4.06927235, -5.38002868, 2.18026303, -2.95228569, -7.00564848,
-3.19870931, -2.1249305 ])
category = np.array([[0,0, 1, 0],
[0,0, 0, 1],
[0,1, 0, 0],
[1,0, 0, 0],
[0,1, 0, 0],
[0,0, 1, 0],
[0,0, 1, 0],
[0,1, 0, 0],
[0,0, 1, 0],
[1,0, 0, 0],
[0,1, 0, 0],
[0,0, 1, 0],
[1,0, 0, 0],
[0,0, 1, 0],
[0,0, 0, 1],
[0,0, 1, 0],
[0,0, 1, 0]])
x = cvx.Variable(17,nonneg=True)
constraints = [cvx.sum(x) == 1]
constraints += [cvx.max(x.T*category) <= 0.34]
x2 =x.copy()
category_weight = x2.T*category # Category weights
category_weight.is_dcp()
category_weight_x = category_weight*category.T
category_weight_x.is_dcp() # Category weight for each x
category_weight_x = cvx.sum(category_weight_x,axis = 1)
# sum over rows to get (len(x),)
category_weight_x_inv = cvx.inv_pos(category_weight_x)
category_weight_x_inv.is_dcp() #1/n
# PROBLEM:
x_category_weight = x2/category_weight_x # category weight_x is not constant - not allowed!
x_category_weight.is_dcp()
#
ratio_weighted_opt = ratio*x_category_weight.T #Get Ratio value for x in category
ratio_category_opt = ratio_weighted_opt.T*category #split ratio to category columns
ratio_category_opt_cap = cvx.pos(ratio_category_opt) #set negativ to 0
ratio_category_opt_cap.is_dcp()
ratio_category_opt_cap = cvx.pos(1-ratio_category_opt) #set bigger than 1 to 1
ratio_category_opt_cap +=1
ratio_category_opt_cap.is_dcp()
ratio_category_opt_cap_category = ratio_category_opt_cap*category_weight #multiply with category weight to total
objective = cvx.Maximize(cvx.sum(ratio_weighted_opt))
probl = cvx.Problem(objective, constraints)
probl.solve(verbose=True)

Extract sub arrays based on kernel in numpy

I would like to know if there is an efficient method to get sub-arrays from a larger numpy array.
What I have is an application of np.where. I iterate 'manually' over x and y as offsets and apply where with a kernel to each rectangle extracted from the larger array with proper dimensions.
But is there a more direct approach in numpy's collection of methods?
import numpy as np
example = np.arange(20).reshape((5, 4))
# e.g. a cross kernel
a_kernel = np.asarray([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
np.where(a_kernel, example[1:4, 1:4], 0)
# returns
# array([[ 0, 6, 0],
# [ 9, 10, 11],
# [ 0, 14, 0]])
def arrays_from_kernel(a, a_kernel):
width, height = a_kernel.shape
y_max, x_max = a.shape
return [np.where(a_kernel, a[y:(y + height), x:(x + width)], 0)
for y in range(y_max - height + 1)
for x in range(x_max - width + 1)]
sub_arrays = arrays_from_kernel(example, a_kernel)
This returns the arrays I need for further processing.
# [array([[0, 1, 0],
# [4, 5, 6],
# [0, 9, 0]]),
# array([[ 0, 2, 0],
# [ 5, 6, 7],
# [ 0, 10, 0]]),
# ...
# array([[ 0, 9, 0],
# [12, 13, 14],
# [ 0, 17, 0]]),
# array([[ 0, 10, 0],
# [13, 14, 15],
# [ 0, 18, 0]])]
The context: similar to 2D convolution I would like to apply a custom function on each of the subarrays (e.g. product of squared numbers).
At the moment, you're manually advancing a sliding window over the data - stride tricks to the rescue! (And no, I didn't just make that up - there's actually a submodule called stride_tricks in numpy!) Instead of manually building windows into the data, and calling np.where() on them, if you had the windows in an array, you could call np.where() just once. Stride tricks allow you to create such an array without even having to copy the data.
Let me explain. Normal slices in numpy create views into the original data instead of copies. This is done by referring to the original data, but changing the strides used to access the data (ie. how much to jump between two elements or two rows, and so on). Stride tricks allow you to modify those strides more freely than just slicing and reshaping does, so you can eg. iterate over the same data more than once, which is useful here.
Let me demonstrate:
import numpy as np
example = np.arange(20).reshape((5, 4))
a_kernel = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
def sliding_window(data, win_shape, **kwargs):
assert data.ndim == len(win_shape)
shape = tuple(dn - wn + 1 for dn, wn in zip(data.shape, win_shape)) + win_shape
strides = data.strides * 2
return np.lib.stride_tricks.as_strided(data, shape=shape, strides=strides, **kwargs)
def arrays_from_kernel(a, a_kernel):
windows = sliding_window(a, a_kernel.shape)
return np.where(a_kernel, windows, 0)
sub_arrays = arrays_from_kernel(example, a_kernel)
The scipy.ndimage module offers a number of filters -- one of which might meet your needs. If none of those filters do what you want, you could use ndimage.generic_filter
to call a custom function on each subarray. ndimage.generic_filter is not as fast as the other ndimage filters, however.
For example,
import numpy as np
example = np.arange(20).reshape((5, 4))
a_kernel = np.asarray([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
# def arrays_from_kernel(a, a_kernel):
# width, height = a_kernel.shape
# y_max, x_max = a.shape
# return [np.where(a_kernel, a[y:(y + height), x:(x + width)], 0)
# for y in range(y_max - height + 1)
# for x in range(x_max - width + 1)]
# sub_arrays = arrays_from_kernel(example, a_kernel)
# for arr in sub_arrays:
# print(arr)
# print('-'*80)
import scipy.ndimage as ndimage
def func(x):
# reject subarrays that extend beyond the border of the `example` array
if not np.isnan(x).any():
y = np.zeros_like(a_kernel, dtype=example.dtype)
np.put(y, np.flatnonzero(a_kernel), x)
print(y)
# Instead or returning 0, you can perform your desired computation on the subarray here.
# Note that you may not need the 2D array y; often, you only need the values in the 1D array x
return 0
result = ndimage.generic_filter(example, func, footprint=a_kernel, mode='constant', cval=np.nan)
For the particular problem of computing the product of squares for each subarray, you
could convert the product into a sum by taking advantage of the fact that A * B = exp(log(A)+log(B)). This would allow you to express the computation as a normal convolution. Now using ndimage.convolve can improve performance a lot. The amount of the improvement depends on the size of example:
import numpy as np
import scipy.ndimage as ndimage
import perfplot
a_kernel = np.asarray([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
def orig(example, a_kernel=a_kernel):
def arrays_from_kernel(a, a_kernel):
width, height = a_kernel.shape
y_max, x_max = a.shape
return [
np.where(a_kernel, a[y : (y + height), x : (x + width)], 1)
for y in range(y_max - height + 1)
for x in range(x_max - width + 1)
]
return [np.prod(x) ** 2 for x in arrays_from_kernel(example, a_kernel)]
def alt(example, a_kernel=a_kernel):
logged = np.log(example)
result = ndimage.convolve(logged, a_kernel, mode="constant", cval=0)[1:-1, 1:-1]
return (np.exp(result) ** 2).ravel()
def make_example(N):
return np.random.random(size=(N, N))
def check(A, B):
return np.allclose(A, B)
perfplot.show(
setup=make_example,
kernels=[orig, alt],
n_range=[2 ** k for k in range(2, 11)],
logx=True,
logy=True,
xlabel="len(example)",
equality_check=check,
)

What is the correction definition of `sympy.vector.CoordSysCartesian.rotation_matrix`?

A rotation matrix can either be written in two ways.
With row unit vectors,
[ ex_0 ex_1 ex_2]
[ ey_0 ey_1 ey_2]
[ ez_0 ez_1 ez_2]
Or with column unit vectors,
[ ex_0 ey_0 ez_0]
[ ex_1 ey_1 ez_1]
[ ex_2 ey_2 ez_2]
In the following code which definition is being used? Is DCM in row or column format?
from sympy.vector import CoordSysCartesian
from sympy import symbols, pi, Matrix
theta = symbols('theta')
N = CoordSysCartesian('N')
A = N.orient_new_axis('A', theta, N.k)
dcm = N.rotation_matrix(A).subs({'theta':pi/2})
Output,
Matrix([
[0, -1, 0],
[1, 0, 0],
[0, 0, 1]])

Wrong result in a simple ipython script

I defined a simple function that operates on 1st and 3rd rows of a 3x3 matrix:
In [1]: from numpy import *
from sympy import Symbol
In [2]: def ifsin(mat):
mat[2,:]+=1
mat[0,:]=mat[0,:]/pow(mat[2,:],mat[2,:]>0)
return mat
In [3]:Ay=Symbol('Ay')
By=Symbol('By')
q=array([[Ay,-10 ,By],[0,0.4,1],[-1 ,-1, -1]])
q
Out[3]:
array([[Ay, -10, By],
[0, 0.4, 1],
[-1, -1, -1]], dtype=object)
In [4]:V=ifsin(q)
q
Out[4]: array([[Ay, -10.0, By],
[0, 0.4, 1],
[0, 0, 0]], dtype=object)
Why was updated 3rd row of matrix q?
Also if I eval following:
In [5]:M=ifsin(V)
q
Out[5]: array([[Ay, -10.0, By],
[0, 0.4, 1],
[1, 1, 1]], dtype=object)
Again 3rd row of q is updated!!
I tried this script on "Computable" (ipad app) and ipython notebook on ubuntu 14.04 (python 2.7.6) with the same results
Thanks in advance for your help.
updated…
I changed my function to:
def ifsin(m):
return vstack([m[0,:]/pow(m[2,:]+1,(m[2,:]+1)>0),m[1,:],m[2,:]+1])
And now all works fine. Thanks!!
You are literally asking for that update on this line:
mat[2,:]+=1
In numpy syntax, this means "update row 2 of the array named mat by incrementing each value by 1".

Can you broadcast dictionary definitions over numpy arrays?

I'm working on a very simple example of random walk simulations using numpy. My professor insists that we use numpy's broadcast functionality instead of for loops as much as we can, and I want to know if it's possible to broadcast dictionary definitions.
e.g. I have the array [E W N S]. Running through that array using the dictionary would result in [[1, 0] [-1, 0] [0, 1] [0, -1]].
import numpy as np
import matplotlib.pyplot as plt
def random_path(origin, nsteps, choices, choice_probs, choice_map):
directions = np.random.choice(choices, size=(15,), p=choice_probs)
print directions
def main():
directions = ['N', 'S', 'E', 'W']
dir_probabilities = [.2, .3, .45, .05]
dir_map = {'N': [0, 1], 'S': [0, -1], 'E': [1, 0], 'W': [-1, 0]}
origin = [0, 0]
np.random.seed(12345)
path = random_path(origin, 15, directions, dir_probabilities, dir_map)
main()
Why not just ignore the actual directional labels and just store the directions as a (4,2) shaped numpy array? Then you would just index into that array directly.
def random_path(origin, nsteps, choices, choice_probs, choice_map):
directions = np.random.choice(choices, size=(15,), p=choice_probs)
return directions
dir_map = np.array([[0,1], [0,-1], [1,0], [-1,0]])
# Everything else is the same as defined by OP
path_directions = random_path(origin, 15, np.arange(4), dir_probabilities, dir_map)
path = dir_map[path_directions]
Now path is a (15,2) shaped numpy array containing the sequence of moves from the dir_map.

Categories

Resources