我试图用 scipy 计算矩阵的特征向量。一些数字像这样的结果:-3.47686396e-01+0.j。代表什么j意思?即如何解释这个数字?!
还有如何以通常的格式进行转换/打印,即 -1.00 或类似的格式。通常已知的格式。
What I want my code to do is to create an array of elements: [13.8, 13.9, 14.,...] That increase by 0.1, but each of the elements should repeat 17 times before going on to the next number. Below is my code.
from numpy import*
from pylab import*
def f(elem):
return repeat((elem + 0.1),17)
print f(13.8)
def lst(init):
yield init
while True:
next = f(init)
yield next
init = next
for i in lst(13.8):
print i
if i > 20:
break …Run Code Online (Sandbox Code Playgroud) 我想-2.5 to 2.0通过使用该np.linspace()命令插入 8 个数据点,并完全理解这些数字应该是非负数,但是当我运行我的代码时,我收到一个错误,上面写着raise ValueError("x and y arrays must be equal in length along "
ValueError: x and y arrays must be equal in length along interpolation axis.这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
X = np.linspace(-2.5, 2.0, num=8, endpoint=True)
Y = np.linspace(1, 44, num=44, endpoint=True)
f = interp1d(X,Y)
f2 = interp1d(X, Y, kind='cubic')
Xnew = np.linspace(-2.5, 2.0, num=44, endpoint=True)
plt.plot(X, Y, 'o', Xnew, f(Xnew), Xnew, f2(Xnew), …Run Code Online (Sandbox Code Playgroud) 我有一个带概率密度函数的分段四次分布:
p(x)= c(x/a)^2 if 0?x<a;
c((b+a-x)^2/b)^2 if a?x?b;
0 otherwise
Run Code Online (Sandbox Code Playgroud)
假设 c、a、b 是已知的,我试图从分布中抽取 100 个随机样本。我怎样才能用 numpy/scipy 做到这一点?
我正在使用irand=randrange(0,10)在程序中生成随机数。这个随机数生成器在代码中多次使用。在代码的开头,我用random.seed(1234). 这是正确的做法吗?
我一直在尝试用 Python 解决以下线性问题:
minimize{x1,x2}, such that:
x1+2*x2 = 2
2*x1+3*x2 =2
x1+x2=1
x1>=0
x2>=0
Run Code Online (Sandbox Code Playgroud)
我已经尝试过纸浆和 linprog 库 ( from scipy.optimize import linprog),但我什么也没有。第一点是,这两个库都希望我输入某种“目标函数”来最小化,而我希望将我的变量最小化(本质上是为了验证我没有无限多的解决方案)。但是,我尝试最小化以下目标函数:
x1 + x2
判断这几乎等于最小化 x1 和 x2,因为它们都大于 0。第二点是纸浆和 linprog 似乎都无法处理“不可行”的情况。这意味着当这两个库遇到不可行的问题时,它们不会打印出相关的内容(即:“问题无法解决”),而是开始放弃约束,直到得到答案。例如,上述问题是不可行的,因为不存在满足上述所有方程的数 x1 和 x2。现在 linprog 在这种情况下打印出以下内容
message: 'Optimization terminated successfully.'
Run Code Online (Sandbox Code Playgroud)
和
x: array([ 0., 0.])
Run Code Online (Sandbox Code Playgroud)
从中我了解到解决方案是x1 = 0和x2 = 0,这当然是不正确的。
我的下一步是用嵌套的 for 循环和条件语句自己编写所有代码来描述约束,但我还不想去那里。此外,我正在寻找一个可以轻松推广的解决方案,比如 100 多个不同的方程(因为我将在实数的 n 维空间中进行操作)和 60 多个变量(x1,x2,..., x60)。
我想使用 scipy 的树状图。我有以下数据:
我有一个包含七种不同方式的列表。例如:
Y = [71.407452200146807, 0, 33.700136456196823, 1112.3757110973756, 31.594949722819372, 34.823881975554166, 28.36368420190157]
Run Code Online (Sandbox Code Playgroud)
每个平均值是为不同的用户计算的。例如:
X = ["user1", "user2", "user3", "user4", "user5", "user6", "user7"]
Run Code Online (Sandbox Code Playgroud)
我的目标是在树状图的帮助下显示上述数据。
我尝试了以下方法:
Y = [71.407452200146807, 0, 33.700136456196823, 1112.3757110973756, 31.594949722819372, 34.823881975554166, 28.36368420190157]
X = ["user1", "user2", "user3", "user4", "user5", "user6", "user7"]
# Attempt with matrix
#X = np.concatenate((X, Y),)
#Z = linkage(X)
Z = linkage(Y)
# Plot the dendogram with the results above
dendrogram(Z, leaf_rotation=45., leaf_font_size=12. , show_contracted=True)
plt.style.use("seaborn-whitegrid")
plt.title("Dendogram to find clusters")
plt.ylabel("Distance")
plt.show()
Run Code Online (Sandbox Code Playgroud)
但它说:
ValueError:压缩距离矩阵 'y' …
a=np.zeros((3,3,3))
b=np.arange(3)
c=np.arange(9).reshape(3,3)
Run Code Online (Sandbox Code Playgroud)
我想将数组的元素b或c沿着 3d 矩阵(张量)的对角线(或对角线上方/下方)a相对于特定轴放置。
我累了numpy.diagflat,但它只适用于二维矩阵。
例如,如何制作以下矩阵?
array([[[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 2.]],
[[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 2.]],
[[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 2.]]])
Run Code Online (Sandbox Code Playgroud) 这很奇怪!
np.nextafter 返回 0 之后的最小数字。这不应该等于 float64 epsilon 吗?
In [25]: np.nextafter(0, 1).dtype == np.finfo(np.float64).eps.dtype
Out[25]: True
In [26]: np.nextafter(0, 1) < np.finfo(np.float64).eps
Out[26]: True
Run Code Online (Sandbox Code Playgroud) scipy ×10
python ×9
numpy ×7
python-2.7 ×2
arrays ×1
diagonal ×1
matplotlib ×1
matrix ×1
random ×1