Python一次拟合函数

一次拟合

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D

fig=plt.figure()
ax=Axes3D(fig)

#输入的数据
df = pd.read_excel('E:/后100个.xlsx',sheet_name='Sheet1')
x = df['计算结果']
y = df['交通拥堵延时指数']
#θ0,θ1的取值范围和精度
parameter0=np.arange(1.1, 3.1,0.1)
parameter1=np.arange(1, 3.4,0.1)

def func_j(p0,p1):
    sum=0
    for i in range(0,7):
        h=p0+p1*x[i]
        sum+=(h-y[i])**2
    sum=sum/14
    return sum

parameter0,parameter1=np.meshgrid(parameter0,parameter1)
z=func_j(parameter0,parameter1)

surf=ax.plot_surface(parameter0,parameter1,z)

min_value=np.min(z)
min_index=np.argmin(z)
print (np.unravel_index(min_index,z.shape))

min_point=np.unravel_index(min_index,z.shape)
min_x=min_point[0]
min_y=min_point[1]

print(parameter0[min_x][min_y])
print(parameter1[min_x][min_y])

plt.show()

你可能感兴趣的:(Python一次拟合函数)