将平面拟合到点云中 (3d)

fitting a plane into a cloud of points (3d)

我有一个包含多列数据的数据文件,我想从这个数据文件中提取3列(表示坐标)并将它们放在另一个文件中,然后使用我想要的新创建的文件使用 scipy.optimize.curve_fit 拟合一个平面或表面(或任何你想称呼它的东西)。这是我的代码:

# -*- coding: utf-8 -*-

from pylab import *
import matplotlib.pyplot as plt 
import numpy as np
from scipy import stats
from scipy.optimize import curve_fit



### processing function    
def store(var,textfile):
    data=loadtxt(textfile,skiprows=1)
    p0=[]
    p1=[]
    p2=[]
    for i in range(0,len(data)):
        p0.append(float(data[i,2]))
        p1.append(float(data[i,3]))
        p2.append(float(data[i,4]))
    var.append(p0)
    var.append(p1)
    var.append(p2)

#extracting the data from a textfile
datafile1='cracks_0101005_5k_tensionTestCentreCrack_l0.001a0_r0.01.txt'
a1=[]
store(a1, datafile1)


rcParams.update({'legend.numpoints':1,'font.size': 20,'axes.labelsize':25,'xtick.major.pad':10,'ytick.major.pad':10,'legend.fontsize':14})
lw=2
ms=10

#fitting a surface(curve) into the data

def func(data, m, n, o): 
    return m*data[:,0] + n*data[:,2] + o 

guess=(1,1,1)

params, pcov = curve_fit(func, a1[::2, :2], a1[:,1], guess)
print (params)

我收到以下错误消息:

Traceback (most recent call last):
  File "fitcurve.py", line 41, in <module>
    params, pcov = curve_fit(func, a1[::2, :2], a1[:,1], guess)
TypeError: list indices must be integers, not tuple

你能告诉我我做错了什么吗?

只是为了更清楚: 我试图将 Y 作为我的依赖函数,因此它将是 X 和 Z 的函数。 显然 a1[] 是一个列表而不是数组,对吗? 但是即使我将它更改为数组 Myarray=np.asarray(a1) 我也会收到一些其他奇怪的消息。 如果有人能帮助我理解这里的问题,我将不胜感激。

干杯

下面是一个平面的线性多元回归的例子:

import numpy as np

# the below "columns" of data could be i.e., x, y**2, sin(x), log(y), etc.
# numpy's array transpose can also  be handy in formatting the data in this way

# first "column" will regress to an offset parameter (a * 1.0, or just a)
# second "column" will regress the X data (b * X)
# third "column" will regress the Y data  (c * Y)
indepData = np.array([
[1.0, 11.0, 0.1], # first data point
[1.0 ,22.0, 0.2], # second data point
[1.0, 33.0, 0.3], # third data point
[1.0, 35.0, 0.5] # fourth data point
])

# Z data
depData = np.array([5.0, 60.0, 70.0, 185.0])


coeffs = np.linalg.lstsq(indepData, depData)[0]

print(coeffs)

X = 25.0
Y = 0.2
a = coeffs[0]
b = coeffs[1]
c = coeffs[2]

regressionPredictedValue = a + b*X + c*Y
print(regressionPredictedValue)

我在您的代码中发现了以下可能的错误:

你想将 y 作为 x,z 的函数,所以你要发送的 X 数组可能是 a1[:, ::2]。但这意味着 func 已经得到一个 m,2 数组,所以这里它必须是 return m*data[:,0] + n*data[:,1] + o 我仍然认为它应该是两个参数而不是三个参数。您可以根据结果计算出一个可能的m, n, o

import matplotlib 
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import style
from random import random
style.use('ggplot')

from scipy.optimize import curve_fit

""" make some data and save to file """
data = []
a, b, s = 1.233, -2.17, 5.2
for i in range(88):
    x = 10 * (2 * random() - 1)
    y = 10 * (2 * random() - 1)
    z = a * x +  b * y+ s * (2 * random() - 1) * 0.5
    data += [[x, y, z]]
data = np.array(data)
np.savetxt("data.txt", data)

""" get the data and use unpack to directly write into x,y,z variables"""
xData, yData, zData = np.loadtxt("data.txt", unpack=True)
"""...while I actally need the packed version as well, so I could load again"""
#allData = np.loadtxt("data.txt")
"""...or..."""
allData = np.array( zip(xData, yData, zData) )

def func(data, m, o): 
    return m * data[:,0] + o * data[:, 1] 
    
guess = (1, 1)
params, pcov = curve_fit(func, allData[:, ::2], allData[:,1], guess)

""" showing data and fit result"""
x = np.linspace(-10, 10, 10)
y = np.linspace(-10, 10, 10)
X, Y = np.meshgrid(x, y)
Z = -params[0] / params[1] * X + 1 / params[1] * Y

fig1 = plt.figure(1)
ax = fig1.add_subplot( 1, 1, 1, projection='3d')
ax.scatter(xData, yData, zData)
ax.plot_wireframe(X, Y, Z, color='#4060a6', alpha=0.6 )
ax.set_title(
    "({:1.2f},{:1.2f})".format(
        -params[0] / params[1], 1 / params[1]
    )
)
plt.show()

请注意,当您安装 y = m * x + o * z 时,我绘制了 z = a * x + b * yb = 1 / oa = -m / o 的线框,即 n = 1。您可以相应地重新调整 m, n, o