我正在尝试做我认为应该很简单的事情:
我做了一个2D列表:
a = [[1,5],[2,6],[3,7]]
我想滑出第一个column并尝试:
1)
a[:,0]
...
TypeError: list indices must be integers or slices, not tuple
2)
a[:,0:1]
...
TypeError: list indices must be integers or slices, not tuple
3)
a[:][0]
[1, 5]
4)
a[0][:]
[1, 5]
5)明白了,但这是做这件事的方法吗?
aa[0] for aa in a
我遇到了一个问题,一些numpy数组不能使用cv.fromarray()转换为cvMat。似乎每当numpy数组被转置时,问题就会发生。
import numpy as np
import cv
# This works fine:
b = np.arange(6).reshape(2,3).astype('float32')
B = cv.fromarray(b)
print(cv.GetSize(B))
# But this produces an error:
a = np.arange(6).reshape(3,2).astype('float32'
我有一个像a = np.zeros((100,100, 20))这样的三维数字数组。我希望对每个x,y位置执行一项操作,该操作涉及z轴上的所有元素,并且结果存储在一个数组中,比如在相同的x,y位置上的b = np.zeros((100,100))。
现在,我使用for循环来完成这个任务:
d_n = np.array([...]) # a parameter with the same shape as b
for (x,y), v in np.ndenumerate(b):
C = a[x,y,:]
### calculate some_value using C
我试图从波士顿数据集中得到两个矩阵之间的相关性。所以我要这么做。
import sklearn as skl
from sklearn.datasets import load_boston
import numpy as np
import scipy as sc
import matplotlib.pyplot as plt
boston_dataset = load_boston()
X = boston_dataset.data
Y = boston_dataset.target
# Correlation between RM and Y
RM = X[:, 5:6]
np.
import numpy as np
def qrhouse(A):
(m,n) = A.shape
R = A
V = np.zeros((m,n))
for k in range(0,min(m-1,n)):
x = R[k:m,k]
x.shape = (m-k,1)
v = x + np.sin(x[0])*np.linalg.norm(x.T)*np.eye(m-k,1)
V[k:m,k] = v
R[k:m,k:n] = R[k:m,k:n]-(2*v)*(np.tra
我遇到了函数ascontiguousarray()的问题,它在NumPy 1.11.1和1.13.1中返回不同的步长。
要重现的代码(Ubuntu 16.04、Python 2.7.12):
import os
import numpy as np
x = np.array([[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16]],dtype='uint32')
x = x.T
x = np.ascontiguousarray(x)
print(x.strides)
在NumPy 1.11
我记得有一次我在编写一个类,在定义__init__方法时,我试图设置一个属于同一个类的实例,如下例所示: class Complex:
def __init__(self, real, imag):
self.real = real
self.imag = imag
self.conjugate = Complex(real, - imag) # instance of the same class
Compex(1, 2) # RecursionError: maximum recursion depth exceeded 但是当我尝试该
因此,如果我有以下代码,它基本上与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行末尾的含义,但如果没有它,