Data Analysis Routines

These modules provide basic data analysis or data fitting functionality.

linefit

Fit a line to a data set with optional weights.

Returns the parameters of the model, bo, b1: Y = b0 + b1* X

author:Nadia Dencheva
version:‘1.0 (2007-02-20)’
stsci.tools.linefit.linefit(x, y, weights=None)
Parameters:
  • y (1D numpy array) – The data to be fitted
  • x (1D numpy array) – The x values of the y array. x and y must have the same shape.
  • weights (1D numpy array, must have the same shape as x and y) – weight values

Examples

>>> import numpy as N
>>> from numpy.core import around
>>> x = np.array([-5, -4 ,-3 ,-2 ,-1, 0, 1, 2, 3, 4, 5])
>>> y = np.array([1, 5, 4, 7, 10, 8, 9, 13, 14, 13, 18])
>>> around(linefit(x,y), decimals=5)
array([9.27273, 1.43636])
>>> x = np.array([1.3,1.3,2.0,2.0,2.7,3.3,3.3,3.7,3.7,4.,4.,4.,4.7,4.7,5.,5.3,5.3,5.3,5.7,6.,6.,6.3,6.7])
>>> y = np.array([2.3,1.8,2.8,1.5,2.2,3.8,1.8,3.7,1.7,2.8,2.8,2.2,3.2,1.9,1.8,3.5,2.8,2.1,3.4,3.2,3.,3.,5.9])
>>> around(linefit(x,y), decimals=5)
array([1.42564, 0.31579])

xyinterp

Module:xyinterp.py

Interpolates y based on the given xval.

x and y are a pair of independent/dependent variable arrays that must be the same length. The x array must also be sorted. xval is a user-specified value. This routine looks up xval in the x array and uses that information to properly interpolate the value in the y array.

author:Vicki Laidler
version:‘0.1 (2006-07-06)’
stsci.tools.xyinterp.xyinterp(x, y, xval)
Purpose:Interpolates y based on the given xval.

x and y are a pair of independent/dependent variable arrays that must be the same length. The x array must also be sorted. xval is a user-specified value. This routine looks up xval in the x array and uses that information to properly interpolate the value in the y array.

Notes

Use the searchsorted method on the X array to determine the bin in which xval falls; then use that information to compute the corresponding y value.

See also

numpy()

Parameters:
  • x (1D numpy array) – independent variable array: MUST BE SORTED
  • y (1D numpy array) – dependent variable array
  • xval (float) – the x value at which you want to know the value of y
Returns:

y – the value of y corresponding to xval

Return type:

float

Raises:
  • ValueError: – If arrays are unequal length; or x array is unsorted; or if xval falls outside the bounds of x (extrapolation is unsupported
  • version:0.1 last modified 2006-07-06