'Fitting a graph to find gradients for data curves
I have a bunch of data which I need to find the gradient for. The data is set up like I have the nuclear metallicity which is the metallicity at the center of a galaxy (distance = 0) and the metallicities for a bunch of different galaxies at different distances from the center. I want to find an universal gradient so I was planning on plotting all of the data and looking for a curve of best fit. The idea is that the metallicity is what it is at the center. So I need to fix any metallicity gradient such that it returns the correct nuclear metallicity. I need to fix the offset of any gradient to be the nuclear metallicity (when fitting a single galaxy) or 0 (if shifting everything by their nuclear metallicity and fitting the full sample).
Here is an example of my data:
NAME Metallicity Nuclear Metallicity Distance
1990U - - -
1991ar 8.52 - 4.61
1996d 8.66 - 2.0295
1996aq 8.59 9.03 2.97297
1997B - - 8.24493
1999cn 8.69 - 16.71392
2005eo 8.49 9.23 10.25775
2005mf 8.83 9.05 7.2698
2006jc - 8.48 2.0295
2007uy 8.7 9 3.61248
2008D 8.86 9 9.59352
So I basically want an equation like Metallicity - Nuclear Metallicity = distance * gradient. Right now I am using a very simple script that just calls in the data from the text file and then plot it using matplotlib. What I am confused about is how to offset the graph so that the nuclear metallicity is zero and I can get a universal gradient. Since the equation above uses the gradient but I need to solve for it I'm not sure how to go about this. Does anyone know how to fit a graph using a hyperparameter?
EDIT:
So what I mean about gradient is an equation of the line of best fit I suppose. Basically I have a metallicity at the center of a galaxy and the metallicity at a distance from the center for a bunch of different galaxies and I need to find an equation using the data so if I know the central metallicity and the distance from the center I can plug it into said equation to know the metallicity at that point. Since currently all the galaxies have different metallicity I'm trying to fit all the data to find the gradient. Does that make sense?
Solution 1:[1]
If the equation you're trying to fit is
Metallicity - Nuclear Metallicity = distance * gradient
then letting the LHS = y, and distance = x, if your data is in a pandas dataframe called df:
import numpy as np
#drop rows with nulls
df = df.dropna()
y = df['Metallicity'] - df['Nuclear Metallicity']
x = df['Distance']
#fit a degree-1 polynomial i.e. a line
intercept, gradient = np.polyfit(x,y,1)
print(intercept,gradient)
-0.0116381176987 -0.289543910605
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | maxymoo |
