首页 > 其他分享 >7.10

7.10

时间:2024-11-18 17:40:20浏览次数:1  
标签:plt 7.10 degree fit normal interp data

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d, PchipInterpolator, CubicSpline
from scipy.optimize import curve_fit
from scipy.stats import norm

file_path = '7.17.xlsx'
data = pd.read_excel(file_path, header=None)
x_data = data.iloc[:, 0].values
y_data = data.iloc[:, 1].values

x_interp = np.linspace(-2, 4.9, 400)

interp_functions = {
'Linear': interp1d(x_data, y_data, kind='linear', bounds_error=False, fill_value='extrapolate'),
'Cubic': interp1d(x_data, y_data, kind='cubic', bounds_error=False, fill_value='extrapolate'),
'PCHIP': PchipInterpolator(x_data, y_data, extrapolate=True),
'CubicSpline': CubicSpline(x_data, y_data, bc_type='natural', extrapolate=True)
}

y_interps = {name: func(x_interp) for name, func in interp_functions.items()}

plt.figure(figsize=(10, 6))
for name, y_interp in y_interps.items():
plt.plot(x_interp, y_interp, label=name)
plt.plot(x_data, y_data, 'o', label='Original Data')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Interpolation Methods Comparison')
plt.grid(True)
plt.show()

degrees = range(1, 6)
coeffs = {}
residuals = {}
for degree in degrees:
coefficients, residual = np.polyfit(x_data, y_data, degree, cov=True)
residual_std = np.sqrt(residual[0, 0])
coeffs[degree] = coefficients
residuals[degree] = residual_std

for degree, coeffs_val in coeffs.items():
print(f"Degree {degree} Polynomial Coefficients: {coeffs_val}")
print(f"Residual Standard Deviation: {residuals[degree]:.4f}")

best_degree = min(residuals, key=residuals.get)
print(f"Best fitting polynomial degree: {best_degree}")

best_poly = np.poly1d(coeffs[best_degree])
y_poly_fit = best_poly(x_interp)

plt.figure(figsize=(10, 6))
plt.plot(x_interp, y_poly_fit, label=f'{best_degree}-degree Polynomial Fit')
plt.plot(x_data, y_data, 'o', label='Original Data')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Polynomial Fit')
plt.grid(True)
plt.show()

def normal_dist(x, mu, sigma):
return norm.pdf(x, mu, sigma)

params, params_covariance = curve_fit(normal_dist, x_data, y_data, p0=[np.mean(x_data), np.std(x_data)])
mu, sigma = params

x_normal_fit = np.linspace(min(x_data), max(x_data), 400)
y_normal_fit = normal_dist(x_normal_fit, mu, sigma)

plt.figure(figsize=(10, 6))
plt.plot(x_normal_fit, y_normal_fit, label=f'Normal Distribution Fit\nmu={mu:.2f}, sigma={sigma:.2f}')
plt.plot(x_data, y_data, 'o', label='Original Data')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Normal Distribution Fit')
plt.grid(True)
plt.show()

2023310143007



标签:plt,7.10,degree,fit,normal,interp,data
From: https://www.cnblogs.com/zzzzddddd/p/18553280

相关文章

  • 7.1 7.3 7.4 7.7 7.10
    7.1点击查看代码importnumpyasnpimportscipy.interpolateasspiimportscipy.integrateasspi_integratedefg(x):return((3*x**2+4*x+6)*np.sin(x))/(x**2+8*x+6)x_values=np.linspace(0,10,1000)y_values=g(x_values)spline=spi.C......
  • 习题7.10(3)
    1.代码实现点击查看代码importnumpyasnpimportpylabaspltfromscipy.optimizeimportcurve_fit#原始数据点x0=np.array([-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9])y0=np.......
  • 数学建模习题7.10
    `importnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltfromscipy.interpolateimportinterp1d,PchipInterpolator,CubicSplinefromscipy.optimizeimportcurve_fitfromscipy.statsimportnorm读取数据file_path='7.17.xISX'#确保文件......
  • 习题7.10(2)
    1.代码实现点击查看代码importnumpyasnpimportpylabasplt#原始数据点x0=np.array([-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9])y0=np.array([0.1029,0.1174,0.1316,0.144......
  • 习题7.10(1)
    1.代码实现点击查看代码importnumpyasnpimportpylabasplt#原始数据点x0=np.array([-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9])y0=np.array([0.1029,0.1174,0.1316,0.1448......
  • 7.10 已知一组观测数据,如表中7.17.excel(表中第一列为x的值,第二列为y的值)。试用插值方
    importnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltfromscipy.interpolateimportinterp1d,PchipInterpolator,CubicSplinefromscipy.optimizeimportcurve_fitfromscipy.statsimportnormfile_path='7.17.xlsx'data=pd.rea......
  • 高等数学 7.10常系数线性微分方程组解法举例
    在研究某些实际问题时,会遇到由几个微分方程联立起来共同确定几个具有同一自变量的函数的情况。这些联立的微分方程称为微分方程组。如果微分方程组中的每一个微分方程都是常系数线性微分方程,那么,这种微分方程组就叫做常系数线性微分方程组。对于常系数线性微分方程组,我们可以用......
  • 2024.7.10
    2024.7.10T1题面请构造一颗有\(a\)个度数为\(1\)的点与\(b\)个度数为\(3\)的点的树,无解输出\(0\)\(a,b\le200\)题解先满足\(3\)度点,再满足\(1\)度点即可T2题面给定一个\(n\)个点\(m\)条边的有向图,便有边权\(w\),请找一条从\(1\)到\(n\)的路径,使得......
  • 极狐GitLab 发布安全版本16.10.10, 16.9.11, 16.8.10, 16.7.10, 16.6.10, 16.5.10, 16
    近期,极狐GitLab针对16.x版本正式推出安全版本16.10.10,16.9.11,16.8.10,16.7.10,16.6.10,16.5.10,16.4.7,16.3.9,16.2.11,16.1.8,16.0.10,用来减缓安全漏洞CVE-2024-45409带来的安全风险。极狐GitLab正式推出针对GitLabCE老旧版本免费用户的GitLab专业升级服......
  • Rocksdb 7.0.0 ~ 7.10.2 重要版本特性
    7.9.0(2022-11-21)现在可以提供对宽列数据模型的基本支持。可以使用API存储宽列实体PutEntity,并使用GetEntity和迭代器的新columnsAPI进行检索。为了兼容,经典APIGet和MultiGet以及迭代器的valueAPI返回宽列实体的匿名默认列的值;此外,GetEntity和迭代器的APIcolumns以仅具......