我有一个numpy数组列表,我想检查一个给定的数组是否在列表中。这其中有一些非常奇怪的行为,我想知道如何绕过它。下面是这个问题的一个简单版本:
import numpy as np
x = np.array([1,1])
a = [x,1]
x in a # Returns True
(x+1) in a # Throws ValueError
1 in a # Throws ValueError
我不明白这是怎么回事。这个问题有没有很好的解决方法?
我正在使用Python 3.7。
编辑:确切的错误是:
ValueError: The truth valu
我只想填横轴上两个值之间的一些曲线下的面积。我试过了
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
x = np.linspace(-4,4, 10000)
nVals = [norm.pdf(i,0,1) for i in x]
line = plt.plot(x,nVals)
plt.fill_between(x,nVals,color = '#111111',where = x > -3 and x < -2)
plt.axis([-4,4,
因此,如果我有以下代码,它基本上与numpy参考页面上的显示相同:
import numpy as np
import matplotlib.pyplot as plt
mean = [0,0]
cov = [[1,-0.5], [-0.5,1]]
n = int(input("How many random points?"))
x, y = np.random.multivariate_normal(mean, cov, size = n).T
plt.plot(x,y, 'x')
plt.show()
我不明白.T在第9行末尾的含义,但如果没有它,
我有一个非常简单的查询,并尝试使用NumPy ()函数将其转换为fromiter()数组。然而,我不明白为什么它不工作,或者下面的错误是什么意思。有什么想法吗?
import numpy as np
c.execute("SELECT video_id FROM video")
results = c.fetchall()
D = np.fromiter(results, dtype=float, count=-1)
ERROR: ValueError: setting an array element with a sequence.
我正在尝试用strptime将时间字符串转换为datetime对象。问题是,从string到datetime对象都有格式错误。
我不明白为什么这种格式不适合我的数据。
import numpy as np
from datetime import datetime
Vent_date = np.array([b'"2018-06-28 15:00:00"', b'"2018-06-28 15:00:00"'], dtype='|S21')
dates = []
for line in Vent_date:
我有一个numpy数组,其中包含:
[1, 2, 3]
我想创建一个包含以下内容的数组:
[1, 2, 3, 1]
也就是说,我希望将第一个元素添加到数组的末尾。
我尝试过显而易见的方法:
np.concatenate((a, a[0]))
但是我收到一个错误,说是ValueError: arrays must have same number of dimensions
我不明白这一点--两个数组都只是一维数组。
我想使用numpy.array (形状的(3, 2))构建numpy.fromiter
numpy数组将由3个numpy数组组成,每个数组包含2个浮点数。这3个数组将是自定义函数的输出,但对于示例,我将使用numpy.random.randn。
受的启发,我的代码如下所示:
iterable = (np.random.randn(2) for _ in range(3))
np.fromiter(iterable, float, 3)
但是,我得到了以下错误,我不明白:
ValueError: setting an array element with a sequence.
我可以简单地使用
我正试图了解numpy的广播如何影响np.allclose的输出。
>>> np.allclose([], [1.])
True
我不明白这是为什么,但这并不是:
>>> np.allclose([], [1., 2.])
ValueError: operands could not be broadcast together with shapes (0,) (2,)
这里有什么规定?我在中找不到任何关于空数组的信息。
我在Python3中有一个最小的代码,它使用numpy和函数apply_along_axis。我不明白我为什么会犯这个错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
在lambda中提供一个直接公式是可行的。一旦我使用了另一个函数,我就会得到这个错误。我应该还点别的东西吗?
最低限度代码:
import numpy as np
def logn(x, b):
return np.log(x)/np.log(b)
de
当使用LDA模型时,我每次都会得到不同的主题,并且我希望复制相同的集合。我在谷歌上搜索过类似的问题,比如。
我修复了num.random.seed(1000)文章中显示的种子,但它不起作用。我阅读了ldamodel.py并找到了下面的代码:
def get_random_state(seed):
"""
Turn seed into a np.random.RandomState instance.
Method originally from maciejkula/glove-python, and written by @joshloyal
我不明白为什么我的relu函数不能工作,但squarer可以工作,它有什么不同? import numpy as np
x = np.array([1, 2, 3, 4, 5])
squarer = lambda x: x ** 2
squarer(x)
# array([ 1, 4, 9, 16, 25])
relu = lambda x : 0 if x <= 0 else x
relu(x)
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.a
当使用scipy.optimize的fmin时,我得到一个错误,我不明白:
ValueError: setting an array element with a sequence.
下面是一个简单的平方误差示例来演示:
import numpy as np
from scipy.optimize import fmin
def cost_function(theta, X, y):
m = X.shape[0]
error = X.dot(theta) - y
J = 1/(2*m) * error.T.dot(error)
return J
我真的很困惑为什么会出现这个错误。下面是我的代码:
import numpy as np
x = np.array([0, 0])
y = np.array([10, 10])
a = np.array([1, 6])
b = np.array([3, 7])
points = [x, y, a, b]
max_pair = [x, y]
other_pairs = [p for p in points if p not in max_pair]
>>>ValueError: The truth value of an array with more than one el
我有一个形为( 29,10)的numpy数组和一个由29个元素组成的列表,我想以一个形(29,11)数组结束。
我基本上是将列表转换为numpy数组,并尝试使用vstack,但它抱怨维度不同。
玩具示例
a = np.zeros((29,10))
a.shape
(29,10)
b = np.array(['A']*29)
b.shape
(29,)
np.vstack((a, b))
ValueError: all the input array dimensions except for the concatenation axis must match exactly
我正在一台新机器(Windows10)上设置Python (CPython 3.4,64位)。我安装了numpy & nose,并在解释器提示符中运行numpy.test(),以确保一切正常运行:
Running unit tests for numpy
NumPy version 1.9.0
NumPy is installed in C:\Python34\lib\site-packages\numpy
Python version 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:16:31) [MSC v.1600 64 bit (AMD6
我正在尝试这个代码片段。我使用的是scikits.learn 0.8.1
from scikits.learn import linear_model
import numpy as np
num_rows = 10000
X = np.zeros([num_rows,2])
y = np.zeros([num_rows,1])
# assume here I have filled in X and y appropriately with 0s and 1s from the dataset
clf = linear_model.LogisticRegression()
clf.fit(X
numpy.histogram有两个输出
hist:值的histogrambin_edges:返回bin边(length(hist)+1)
两者都是向量,但在下面的示例中,第二个向量的长度为101,比长度为100的第一个向量高1:
import numpy as np
from numpy.random import rand, randn
n = 100 # number of bins
X = randn(n)*.1
a,bins1 = np.histogram(X,bins=n)
如果然后尝试plt.plot(bins1,a),则会发生以下形状错误
ValueError: x an
import numpy as n
a=n.loadtxt("data.txt",dtype=[('ID',int),('ID2',int),('ID3',int),
('GENDER','|S1'),('ID4',int),('ID5',int)])
print len(n.unique(a[a['GENDER']==f]['ID']))
我对这个代码有问题,我试图计算我