How do I resolve "Use scipy.optimize.linear_sum_assignment instead" - python

I am using python script for people detection.
I have the following line in my script:
import time
import cv2 as cv
import glob
import argparse
import sys
import numpy as np
import os.path
from imutils.video import FPS
from collections import deque
from sklearn.utils.linear_assignment_ import linear_assignment
When I run my script I have got the following lines:
/home/user/.local/lib/python3.6/site-packages/sklearn/utils/linear_assignment_.py:127:
DeprecationWarning: The linear_assignment function is deprecated in 0.21 and will be removed from 0.23. Use scipy.optimize.linear_sum_assignment instead.
DeprecationWarning)
Please, advice me how to solve it.

You need to replace the sklearn.utils.linear_assignment_.linear_assignment function by the scipy.optimize.linear_sum_assignment function.
The difference is in the return format: linear_assignment() is returning a numpy array and linear_sum_assignment() a tuple of numpy arrays. You obtain the same output by converting the output of linear_sum_assignment() in array and transpose it.
Your script should look like this:
import time
import cv2 as cv
import glob
import argparse
import sys
import numpy as np
import os.path
from imutils.video import FPS
from collections import deque
from scipy.optimize import linear_sum_assignment
#compute your cost matrix
indices = linear_sum_assignment(cost_matrix)
indices = np.asarray(indices)
indices = np.transpose(indices)

Replace the linear_assignment for linear_sum_assignment
# from sklearn.utils.linear_assignment_ import linear_assignment
from scipy.optimize import linear_sum_assignment
cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])
# result = linear_assignment(cost)
result = linear_sum_assignment(cost)
result = np.array(list(zip(*result)))
https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linear_sum_assignment.html

Related

is there a way to get 'numpy.linalg.svd()' code

Since numpy.linalg.svd() is a predefined function i didn't find the inner code of it.
from scipy import linalg
u, s, v = np.linalg.svd(b, full_matrices=True)
import inspect
from scipy import linalg
import numpy as np
print(inspect.getsource(np.linalg.svd))

How to calculate the probability at a given condition when I know the outcome in pymc3?

import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pymc3 as pm
from pymc3.math import dot, exp
import pandas as pd
trace = pm.sample(
10000,
chains=4,
tune=400,
return_inferencedata=True,
)
summary = az.summary(trace, hdi_prob=0.95)
print(summary)
with m:
pm.set_data({"condition1": [val1], "condition2": [val2], "condition3":
[val3]})
ppc = pm.sample_posterior_predictive(trace)
In the above code I have the values of three condition available and I know the output as well, I want to calculate the probability of arriving at that output.

Output Shape not correct

One week ago I run this code perfectly. But today I am getting runtime error: output shape not correct
from PIL import Image
import glob
import numpy as np
import scipy.ndimage.filters
import matplotlib.pyplot as plt
image_list_Brownspot = []
for filename in glob.glob('./dataset/BrownSpot/*.jpg'):
im=Image.open(filename)
image_list_Brownspot.append(im.copy())
im.close()
len(image_list_Brownspot)
lap = scipy.ndimage.filters.laplace(image_list_Brownspot[0])
`

Why does numpy give correct least squares solution but not scipy?

I am scratching my head over this very simple problem. Given this toy data:
randgen = np.random.RandomState(9)
npoints = 1000
noise = randgen.randn(npoints)
x = np.linspace(0, 1, npoints)
y = 5 + 10*x + noise
Solving this using numpy's least squares:
# design matrix::
X = np.ones((npoints, 2))
X[:,0] = np.copy(x)
p, res, rnk, s = np.linalg.lstsq(X, y)
p
gives a reasonable answer: array([ 9.94406755, 5.05954009]) for p. However, solving using scipy's least squares gives wildly different answer (which changes on each invocation of the function):
p, res, rnk, s = scipy.linalg.lstsq(X, y)
p
An example solution is array([ 1.16328381e+08, -2.26560583e+06]). I don't understand what I am missing. I encountered this problem when using Scikit-learn's LinearRegression which internally uses scipy's lstsq. That was giving me weird answers.
Edit:
Numpy version: 1.11.2
Scipy version: 0.18.1
Python: 3.5
Edit 2:
I have realized that loading a particular library before loading scipy is causing this problem. The following order of loading libraries causes problem:
import numpy as np
from numpy.ma import MaskedArray
from matplotlib import pyplot as plt
from netCDF4 import Dataset
import matplotlib as mpl
from mpl_toolkits.basemap import Basemap
from pyeemd import ceemdan
from scipy.sparse.linalg import svds
from sklearn.utils.extmath import svd_flip
from matplotlib.colors import BoundaryNorm
from matplotlib.ticker import MaxNLocator
from scipy.signal import convolve, boxcar
If I removed the from pyeemd import ceemdan line then the problem is solved! This raises the following question: why could this be happening?

Plotting a signal in Python

I'm trying to plot a simple signal in python, and when i run this it doesn't show any error only 'Restart' and a blank space
from pymatlab import*
import numpy as np
from numpy import sqrt
import matplotlib.pyplot as plt
import scipy as sp
import math
(hashtags) n, coef, freq, phase
def sinyal(N,c,f,p):
y=np.zeros(N)
t=np.linspace(0,2*pi,N)
Nf=len(c)
for i in range(Nf):
y+=c[i]*np.sin(f[i]*t)
return y;
# Signal Generator
c=[2,5,10]
f=[50, 150, 300]
p=[0,0]
N=2000
x=np.linspace(0,2.0*math.pi,N)
y=sinyal(N,c,f,p)
plt.plot(x[:100],y[:100])
plt.show()
The code you posted has a logical indentation error. The call to sinyal is indented one level, placing it inside the definition of sinyal itself. So although sinyal gets defined, it never gets called.
Using 4 spaces for indentation may help you avoid this error in the future.
Your code basically works (apart from some formatting errors and other oddities). I don't have pymatlab but it isn't necessary for this.
import numpy as np
from numpy import sqrt
import matplotlib.pyplot as plt
import scipy as sp
import math
def sinyal(N,c,f,p):
y=np.zeros(N)
t=np.linspace(0,2*np.pi,N)
Nf=len(c)
for i in range(Nf):
y+=c[i]*np.sin(f[i]*t)
return y;
# Signal Generator
c=[2,5,10]
f=[50, 150, 300]
p=[0,0]
N=2000
x=np.linspace(0,2.0*math.pi,N)
y=sinyal(N,c,f,p)
plt.plot(x[:100],y[:100])
plt.show()

Categories