相关疑难解决方法(0)

如何仅使用Python stdlib检查jpeg图像是彩色还是灰度

我必须在python中编写一个测试用例来检查jpg图像是彩色还是灰度.任何人都可以告诉我,如果有任何方法可以安装额外的库,如opencv吗?

python image-processing python-2.6

20
推荐指数
7
解决办法
3万
查看次数

如何在3D中使用固定点进行多项式拟合

我在3D空间中有一组x,y,z点,另一个变量叫做charge表示在特定的x,y,z坐标中沉积的电荷量.我想对这个数据进行加权(通过检测器中沉积的电荷量加权,这对应于更高的重量以获得更多电荷),使得它通过给定点即顶点.

现在,当我为2D做这个时,我尝试了各种方法(将顶点带到原点并对所有其他点进行相同的变换,并强制拟合穿过原点,给出顶点非常高的权重)但是没有一个像Jaime在这里给出的答案一样好:如何用固定点进行多项式拟合

它使用拉格朗日乘数的方法,我从本科高级多变量课程中模糊地熟悉,但其他并不多,看起来这个代码的转换就像添加az坐标一样简单.(请注意,即使代码没有考虑到沉积的电荷量,它仍然给我最好的结果).我想知道是否有相同算法的版本,但是在3D中.我还在Gmail中联系了答案的作者,但没有收到他的回复.

以下是有关我的数据的更多信息以及我在2D中尝试做的事情:如何权衡散点图中的点以获得拟合?

这是我执行此操作的代码,其中我强制顶点位于原点,然后适合数据设置fit_intercept=False.我目前正在研究2D数据的这种方法,因为我不确定拉格朗日乘数是否有3D版本,但是在3D中有线性回归方法,例如,这里:在3D中拟合一条线:

import numpy as np
import sklearn.linear_model

def plot_best_fit(image_array, vertexX, vertexY):
    weights = np.array(image_array)
    x = np.where(weights>0)[1]
    y = np.where(weights>0)[0]
    size = len(image_array) * len(image_array[0])
    y = np.zeros((len(image_array), len(image_array[0])))
    for i in range(len(np.where(weights>0)[0])):
        y[np.where(weights>0)[0][i]][np.where(weights>0)[1][i]] = np.where(weights>0)[0][i]
    y = y.reshape(size)
    x = np.array(range(len(image_array)) * len(image_array[0]))
    weights = weights.reshape((size))
    for i in range(len(x)):
        x[i] -= vertexX
        y[i] -= vertexY
    model = sklearn.linear_model.LinearRegression(fit_intercept=False)
    model.fit(x.reshape((-1, 1)),y,sample_weight=weights)
    line_x = np.linspace(0, 512, 100).reshape((-1,1))
    pred …
Run Code Online (Sandbox Code Playgroud)

python curve-fitting data-fitting

9
推荐指数
1
解决办法
618
查看次数

有没有办法防止numpy.linalg.svd内存不足?

我传递了100万个3d点,numpy.linalg.svd但内存很快耗尽.有没有办法将此操作分解为更小的块?

我不知道它在做什么但是我只应该传递代表3x3,4x4矩阵的数组?因为我已经看到它在线使用它们传递具有任意数量元素的数组.

python numpy matrix linear-algebra svd

5
推荐指数
1
解决办法
2613
查看次数

将参数传递给函数进行拟合

我试图拟合一个函数,它将输入2个独立变量x,y和3个参数作为输入a,b,c.这是我的测试代码:

import numpy as np
from scipy.optimize import curve_fit

def func(x,y, a, b, c):
    return a*np.exp(-b*(x+y)) + c    

y= x = np.linspace(0,4,50)
z = func(x,y, 2.5, 1.3, 0.5) #works ok
#generate data to be fitted
zn = z + 0.2*np.random.normal(size=len(x))
popt, pcov = curve_fit(func, x,y, zn) #<--------Problem here!!!!!
Run Code Online (Sandbox Code Playgroud)

但是我得到了错误:"func()正好接受5个参数(给定51个)".怎么能正确地传递我的论证x,y?

python numpy curve-fitting scipy

3
推荐指数
1
解决办法
4496
查看次数

通过 3D x,y,z 散点图数据拟合一条线

我有一些数据点沿着 3d 空间中的一条线聚集。我想要导入 csv 文件中的 x、y、z 数据。我想找到一个方程来表示该线,或垂直于该线的平面,或任何数学上正确的方程。这些数据是相互独立的。也许有比我尝试做的更好的方法,但是......

我试图在这里复制一篇旧帖子,它似乎正在做我想做的事情 Fitting a line in 3D

但似乎过去十年的更新使得代码的第二部分无法工作?或者也许我只是做错了什么。我已经把我从弗兰肯斯坦开始的所有内容都放在了底部。有两行似乎给我带来了问题。

我已经把它们摘录在这里了...

import numpy as np

pts = np.add.accumulate(np.random.random((10,3)))
x,y,z = pts.T

# this will find the slope and x-intercept of a plane
# parallel to the y-axis that best fits the data
A_xz = np.vstack((x, np.ones(len(x)))).T
m_xz, c_xz = np.linalg.lstsq(A_xz, z)[0]

# again for a plane parallel to the x-axis
A_yz = np.vstack((y, np.ones(len(y)))).T
m_yz, c_yz = np.linalg.lstsq(A_yz, z)[0]

# the intersection of those two planes …
Run Code Online (Sandbox Code Playgroud)

python numpy matplotlib linear-algebra pandas

2
推荐指数
1
解决办法
3198
查看次数