Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you are trying to predict one value from the other two, then you should use <code>lstsq</code> with the <code>a</code> argument as your independent variables (plus a column of 1's to estimate an intercept) and <code>b</code> as your dependent variable. </p> <p>If, on the other hand, you just want to get the best fitting line to the data, i.e. the line which, if you projected the data onto it, would minimize the squared distance between the real point and its projection, then what you want is the first principal component. </p> <p>One way to define it is the line whose direction vector is the eigenvector of the covariance matrix corresponding to the largest eigenvalue, that passes through the mean of your data. That said, <code>eig(cov(data))</code> is a really bad way to calculate it, since it does a lot of needless computation and copying and is potentially less accurate than using <code>svd</code>. See below:</p> <pre><code>import numpy as np # Generate some data that lies along a line x = np.mgrid[-2:5:120j] y = np.mgrid[1:9:120j] z = np.mgrid[-5:3:120j] data = np.concatenate((x[:, np.newaxis], y[:, np.newaxis], z[:, np.newaxis]), axis=1) # Perturb with some Gaussian noise data += np.random.normal(size=data.shape) * 0.4 # Calculate the mean of the points, i.e. the 'center' of the cloud datamean = data.mean(axis=0) # Do an SVD on the mean-centered data. uu, dd, vv = np.linalg.svd(data - datamean) # Now vv[0] contains the first principal component, i.e. the direction # vector of the 'best fit' line in the least squares sense. # Now generate some points along this best fit line, for plotting. # I use -7, 7 since the spread of the data is roughly 14 # and we want it to have mean 0 (like the points we did # the svd on). Also, it's a straight line, so we only need 2 points. linepts = vv[0] * np.mgrid[-7:7:2j][:, np.newaxis] # shift by the mean to get the line in the right place linepts += datamean # Verify that everything looks right. import matplotlib.pyplot as plt import mpl_toolkits.mplot3d as m3d ax = m3d.Axes3D(plt.figure()) ax.scatter3D(*data.T) ax.plot3D(*linepts.T) plt.show() </code></pre> <p>Here's what it looks like: <img src="https://i.imgur.com/ukDs0l.png" alt="a 3d plot of a fitted line"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload