| name | scipy-optimize |
| description | SciPy optimization and statistics module. Use for linear regression, curve fitting, optimization algorithms, and statistical analysis including R-squared calculations. |
SciPy Optimize and Statistics
SciPy provides scientific computing tools including optimization and statistics.
Linear Regression with scipy.stats
from scipy import stats
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2.1, 4.0, 5.9, 8.1, 10.0])
# Linear regression
result = stats.linregress(x, y)
slope = result.slope
intercept = result.intercept
r_squared = result.rvalue ** 2
std_err = result.stderr
p_value = result.pvalue
print(f"Slope: {slope:.4f}")
print(f"Intercept: {intercept:.4f}")
print(f"R-squared: {r_squared:.4f}")
Arrhenius Analysis with Linear Regression
from scipy import stats
import numpy as np
# Temperature and conductivity data
temp_c = np.array([25, 40, 55, 70, 85])
conductivity = np.array([0.008, 0.015, 0.028, 0.048, 0.082])
# Convert to Arrhenius form
temp_k = temp_c + 273.15
inv_t = 1 / temp_k # x-axis: 1/T
ln_sigma = np.log(conductivity) # y-axis: ln(sigma)
# Linear regression: ln(sigma) = ln(A) - Ea/(R*T)
result = stats.linregress(inv_t, ln_sigma)
# Extract activation energy
R = 8.314 # J/(mol*K)
Ea_jmol = -result.slope * R # J/mol
Ea_kjmol = Ea_jmol / 1000 # kJ/mol
# Pre-exponential factor
A = np.exp(result.intercept)
print(f"Activation Energy: {Ea_kjmol:.2f} kJ/mol")
print(f"Pre-exponential Factor: {A:.2e}")
print(f"R-squared: {result.rvalue**2:.4f}")
Curve Fitting with scipy.optimize
from scipy.optimize import curve_fit
import numpy as np
# Define Arrhenius function
def arrhenius(T, A, Ea):
R = 8.314
return A * np.exp(-Ea / (R * T))
# Fit to data
temp_k = np.array([298, 313, 328, 343, 358])
conductivity = np.array([0.01, 0.02, 0.035, 0.06, 0.1])
# Initial guesses
p0 = [1e5, 30000] # A, Ea
# Fit curve
popt, pcov = curve_fit(arrhenius, temp_k, conductivity, p0=p0)
A_fit, Ea_fit = popt
print(f"Fitted A: {A_fit:.2e}")
print(f"Fitted Ea: {Ea_fit/1000:.2f} kJ/mol")
Calculate R-squared Manually
def calculate_r_squared(y_actual, y_predicted):
ss_res = np.sum((y_actual - y_predicted) ** 2)
ss_tot = np.sum((y_actual - np.mean(y_actual)) ** 2)
return 1 - (ss_res / ss_tot)