I am trying to standardize a numpy array. I seem to be doing something wrong as the value of some elements of the output array is incorrect. I'd appreciate any help. Please find the code below: PYTHON CODE:
from numpy import loadtxt
import numpy as np
import math
class matrix(object):
"""A matrix class."""
def __init__(self):
self.array_2d = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
def standardise(self):
rows, columns = self.array_2d.shape
temp = self.array_2d
for j in range(columns):
for i in range(rows):
self.array_2d[i,j] = (temp[i, j] - min(temp[:, j])) / (max(temp[:, j]) - min(temp[:, j]))
m = matrix()
print(m.array_2d, "\n")
m.standardise()
print(m.array_2d)
temp = self.array_2ddoesn't make a copy ofarray_2d: it just makes the nametemppoint to the same array. So in yourforloop,temppoints to the same array that you've been changing in previous iterations of the loop, not to the original array.