我有一个包含多列数据的数据文件,我想从这个数据文件中提取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)
,我也会得到一些其他奇怪的消息。
如果有人能帮我理解这个问题,我将不胜感激。
干杯
答案 0 :(得分:0)
以下是平面的线性多元回归的示例:
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)
答案 1 :(得分:0)
我在您的代码中发现了可能出现的错误:
您希望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)*.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=.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*y
mit b=1/o
和a=-m/o
的线框,即n=1
。您可以相应地重新调整m,n,o
。