<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def fit_nsphere(points, prior=None):
 """ Fit an n-sphere to a set of points using least squares. Parameters --------- points : (n, d) float Points in space prior : (d,) float Best guess for center of nsphere Returns --------- center : (d,) float Location of center radius : float Mean radius across circle error : float Peak to peak value of deviation from mean radius """

    # make sure points are numpy array
    points = np.asanyarray(points, dtype=np.float64)
    # create ones so we can dot instead of using slower sum
    ones = np.ones(points.shape[1])

    def residuals(center):
        # do the axis sum with a dot
        # this gets called a LOT so worth optimizing
        radii_sq = np.dot((points - center) ** 2, ones)
        # residuals are difference between mean
        # use our sum mean vs .mean() as it is slightly faster
        return radii_sq - (radii_sq.sum() / len(radii_sq))

    if prior is None:
        guess = points.mean(axis=0)
    else:
        guess = np.asanyarray(prior)

    center_result, return_code = leastsq(residuals,
                                         guess,
                                         xtol=1e-8)

    if not (return_code in [1, 2, 3, 4]):
        raise ValueError('Least square fit failed!')

    radii = np.linalg.norm(points - center_result, axis=1)
    radius = radii.mean()
    error = radii.ptp()
    return center_result, radius, error