[英]Need to iterate through each column and find max and min
import numpy as np
x = np.loadtxt('xdata.txt', dtype=float)
y = np.loadtxt('ydata.txt', dtype=float)
normalX = []
normalY = []
for column in x:
i = 0
while i <=17:
xmax = max(column[i])
xmin = min(column[i])
normalx = (?-xmin)/(xmax-xmin)
normalX.append(normalx)
i += 1
else:
break
I have a 148 by 17 matrix that I import and I want to normalize the data. 我有一个148 x 17的矩阵,要导入,并且我想对数据进行规范化。 I am trying to iterate through each column and find the max and min but my code so far results in an "TypeError: 'numpy.float64' object is not iterable".
我试图遍历每一列并找到最大值和最小值,但是到目前为止,我的代码导致“ TypeError:'numpy.float64'对象不可迭代”。 Also what should ?
还应该怎么办? be if I want to have it be the element in the column.
如果我想让它成为列中的元素。
Instead of a big 148x17 matrix ill put a 4x4. 而不是一个大的148x17矩阵,请放一个4x4。
1.61 125 13 933.57
1.95 135 29 1357.77
1.91 135 28 1728
2.2 137 46 1828.05
First column max would be 2.2, min = 1.61 etc. 第一列的最大值为2.2,最小值= 1.61,依此类推。
In your code, you are first accessing for columns in x
this causes columns
to be the row
in x , then later on inside the for loop you are trying to access columns[i]
, this would return the element in i position in that row. 在您的代码中,您首先访问
for columns in x
这将导致columns
成为x中的row
,然后在for循环中,您尝试访问columns[i]
,这将返回该行中i位置的元素。
You can use np.amax(x, axis=0)
to get the maximum of each column , this would inturn return a list of maximum for each column. 您可以使用
np.amax(x, axis=0)
来获取每一列的最大值,这将反过来返回每一列的最大值列表。
Similarly, you can also use np.amin(x, axis=0)
. 同样,您也可以使用
np.amin(x, axis=0)
。
Another issue is that if your matrix is of size 148x17
, in your while
loop you should only be checking till i<17
not i<=17
. 另一个问题是,如果您的矩阵的大小为
148x17
,则在while
循环中,应仅检查直到i<17
而不是i<=17
。 You can also you enumerate(column)
along with for
loop for same , the syntax would be - for i, val in enumerate(column)
. 您也可以将
enumerate(column)
与for
循环一起使用,语法将是- for i, val in enumerate(column)
。
Also, I am guessing you need to normalize all the values, so you need to create a list inside the first for loop and keep inserting to that list and then at the end , add that list to normalX
. 另外,我猜您需要对所有值进行规范化,因此您需要在第一个for循环内创建一个列表,并继续插入该列表,然后最后将该列表添加到
normalX
。
Example code - 示例代码-
xmax = np.amax(x, axis=0)
xmin = np.amin(x, axis=0)
for column in x:
tnormalX = []
for i, val in enumerate(column):
normalx = (val-xmin[i])/(xmax[i]-xmin[i])
tnormalX.append(normalx)
i += 1
normalX.append(tnormalX)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.