gpt4 book ai didi

python-3.x - 使用 python3 给定方程的线性逼近

转载 作者:行者123 更新时间:2023-11-30 09:46:30 26 4
gpt4 key购买 nike

我得到了一组原始数据,并且必须通过一些机器学习技术对其进行建模。经过一番研究,我决定采用线性逼近的方法。

<小时/>

方程的描述。

enter image description here

  • z - 深度(米)

  • T(z) - 深度 z 处的温度

  • T(zᵢ) - zᵢ深度处的温度

  • T₀ - 表面温度(恒定且已知)

  • K - 地温梯度系数(温度如何随深度变化)

  • Mᵢ - 深度 zᵢ 处的液体流速


从方程中可以看出,我们可以求出井眼任意深度的液体温度。

我有液体的深度温度流速列表。我必须根据这些数据通过 python3 建立方程模型。目前我使用 matplotlib 库进行此类计算。

<小时/>

最佳答案

这是 Python 3 中非线性多元回归的示例,这应该很容易适应您的多元回归问题。

import numpy, scipy, scipy.optimize
import matplotlib
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm # to colormap 3D surfaces from blue to red
import matplotlib.pyplot as plt

graphWidth = 800 # units are pixels
graphHeight = 600 # units are pixels

# 3D contour plot lines
numberOfContourLines = 16


def SurfacePlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True)

axes.scatter(x_data, y_data, z_data) # show data along with plotted surface

axes.set_title('Surface Plot (click-drag with mouse)') # add a title for surface plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
axes.set_zlabel('Z Data') # Z axis data label

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ContourPlot(func, data, fittedParameters):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)

x_data = data[0]
y_data = data[1]
z_data = data[2]

xModel = numpy.linspace(min(x_data), max(x_data), 20)
yModel = numpy.linspace(min(y_data), max(y_data), 20)
X, Y = numpy.meshgrid(xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot(x_data, y_data, 'o')

axes.set_title('Contour Plot') # add a title for contour plot
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label

CS = matplotlib.pyplot.contour(X, Y, Z, numberOfContourLines, colors='k')
matplotlib.pyplot.clabel(CS, inline=1, fontsize=10) # labels for contours

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ScatterPlot(data):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

matplotlib.pyplot.grid(True)
axes = Axes3D(f)
x_data = data[0]
y_data = data[1]
z_data = data[2]

axes.scatter(x_data, y_data, z_data)

axes.set_title('Scatter Plot (click-drag with mouse)')
axes.set_xlabel('X Data')
axes.set_ylabel('Y Data')
axes.set_zlabel('Z Data')

plt.show()
plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def func(data, a, alpha, beta):
t = data[0]
p_p = data[1]
return a * (t**alpha) * (p_p**beta)


if __name__ == "__main__":
xData = numpy.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
yData = numpy.array([11.0, 12.1, 13.0, 14.1, 15.0, 16.1, 17.0, 18.1, 90.0])
zData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.0, 9.9])

data = [xData, yData, zData]

# this example uses curve_fit()'s default initial paramter values
fittedParameters, pcov = scipy.optimize.curve_fit(func, [xData, yData], zData)

ScatterPlot(data)
SurfacePlot(func, data, fittedParameters)
ContourPlot(func, data, fittedParameters)

print('fitted prameters', fittedParameters)

关于python-3.x - 使用 python3 给定方程的线性逼近,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51824963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com