What is the difference between numpy.linalg.lstsq and scipy.linalg.lstsq?

If I read the source code right (Numpy 1.8.2, Scipy 0.14.1 ), numpy.linalg.lstsq() uses the LAPACK routine xGELSD and scipy.linalg.lstsq() usesxGELSS. The LAPACK Manual Sec. 2.4 states The subroutine xGELSD is significantly faster than its older counterpart xGELSS, especially for large problems, but may require somewhat more workspace depending on the matrix dimensions. That means … Read more

How to force zero interception in linear regression?

As @AbhranilDas mentioned, just use a linear method. There’s no need for a non-linear solver like scipy.optimize.lstsq. Typically, you’d use numpy.polyfit to fit a line to your data, but in this case you’ll need to do use numpy.linalg.lstsq directly, as you want to set the intercept to zero. As a quick example: import numpy as … Read more

Random Number from Histogram

It’s probably what np.random.choice does in @Ophion’s answer, but you can construct a normalized cumulative density function, then choose based on a uniform random number: from __future__ import division import numpy as np import matplotlib.pyplot as plt data = np.random.normal(size=1000) hist, bins = np.histogram(data, bins=50) bin_midpoints = bins[:-1] + np.diff(bins)/2 cdf = np.cumsum(hist) cdf = … Read more

How to create a white image in Python?

Every color in an image is represented by one byte. So to create an image array, you should set it’s dtype to uint8. And, you don’t need for-loop to set every elements to 255, you can use fill() method or slice index: import numpy as np img = np.zeros([100,100,3],dtype=np.uint8) img.fill(255) # or img[:] = 255

pyinstaller creating EXE RuntimeError: maximum recursion depth exceeded while calling a Python object

This worked for me Run pyinstaller and stop it to generate the spec file : pyinstaller filename.py A file with .spec as extension should be generated Now add the following lines to the beginning of the spec file : import sys sys.setrecursionlimit(5000) Now run the spec file using : pyinstaller filename.spec

Tutorial for scipy.cluster.hierarchy [closed]

There are three steps in hierarchical agglomerative clustering (HAC): Quantify Data (metric argument) Cluster Data (method argument) Choose the number of clusters Doing z = linkage(a) will accomplish the first two steps. Since you did not specify any parameters it uses the standard values metric=”euclidean” method = ‘single’ So z = linkage(a) will give you … Read more

Fitting a closed curve to a set of points

Actually, you were not far from the solution in your question. Using scipy.interpolate.splprep for parametric B-spline interpolation would be the simplest approach. It also natively supports closed curves, if you provide the per=1 parameter, import numpy as np from scipy.interpolate import splprep, splev import matplotlib.pyplot as plt # define pts from the question tck, u … Read more