Python Numpy-将列附加到空数组表示现有数组
我试图使用numpy模块(我作为np导入)来查找列向量的平均值,然后从列中减去平均值。但是,当我尝试获取平均值并将其附加到空数组时,它以0开头,并且具有错误的值。知道我做错了什么吗Python Numpy-将列附加到空数组表示现有数组,python,numpy,Python,Numpy,我试图使用numpy模块(我作为np导入)来查找列向量的平均值,然后从列中减去平均值。但是,当我尝试获取平均值并将其附加到空数组时,它以0开头,并且具有错误的值。知道我做错了什么吗 rating2 = np.array(student_data.values[:,1:]) print(rating2) means = np.empty([1,25]) for index in range(0,24): b = np.mean(rating2[:,index], axis = 0)
rating2 = np.array(student_data.values[:,1:])
print(rating2)
means = np.empty([1,25])
for index in range(0,24):
b = np.mean(rating2[:,index], axis = 0)
print("b is", b)
np.append(means,b)
A = np.array(means)
print(A)
我检查了正在计算的每个平均值,它不是以0开头的。我检查了我的索引,它似乎是正确的
我的输出:
[[3 2 3 3 3 3 3 3 4 3 4 4 3 4 3 2 2 3 4 2 2 3 3 4 4]
[2 3 4 3 2 3 4 3 3 4 5 3 4 3 3 1 1 3 3 2 1 3 3 4 3]
[2 5 4 2 3 2 4 4 1 4 3 1 4 2 2 3 3 2 2 4 3 2 1 3 2]
[3 4 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 4 4 3 3 3 3]
[4 1 1 5 4 4 2 2 4 1 1 4 1 4 4 4 4 4 4 2 4 4 5 2 4]
[4 2 2 4 3 3 2 3 4 2 2 4 2 4 4 3 3 4 4 2 3 4 4 3 4]
[2 5 5 1 2 2 4 4 2 5 5 2 5 2 2 2 2 2 2 4 2 2 1 4 2]
[3 4 3 3 3 3 3 3 2 3 3 2 3 2 3 4 4 3 2 4 4 3 2 2 2]
[4 1 2 4 3 4 2 2 4 2 3 5 3 4 4 3 3 4 4 2 2 4 4 4 4]
[3 4 3 3 3 3 3 3 2 3 2 2 3 2 3 4 4 3 2 4 4 3 3 2 2]]
b is 3.0
b is 3.1
b is 3.0
b is 3.1
b is 2.9
b is 3.0
b is 3.0
b is 3.0
b is 2.9
b is 3.0
b is 3.1
b is 2.9
b is 3.1
b is 3.0
b is 3.1
b is 2.9
b is 2.9
b is 3.1
b is 3.0
b is 3.0
b is 2.9
b is 3.1
b is 2.9
b is 3.1
[[0. 3. 3.1 3. 3.1 2.9 3. 3. 3. 2.9 3. 3.1 2.9 3.1 3. 3.1 2.9 2.9
3.1 3. 3. 2.9 3.1 2.9 3.1]]
这应该行得通
import numpy as np
# your numpy array
matrix = np.random.random((10, 10))
# this is the column wise mean
mean = np.mean(matrix, axis=0)
# this line is broadcasting the mean to the matrix
matrix_without_mean = matrix - mean
通常,在使用NumPy时应避免循环和附加。你看过numpy文档了吗?
np.append
不是一个列表。append
克隆。不要以同样的方式使用它。