2 views
in Python
closed

Consider the below NumPy array:

foo = np.array([[0.0, 10.0], [0.13216, 12.11837], [0.25379, 42.05027], [0.30874, 13.11784]])

Which yields:

[[  0.       10.     ]

[  0.13216  12.11837]

[  0.25379  42.05027]

[  0.30874  13.11784]]

How might I standardize/normalize the Y segment of this array? So it gives me something like:

[[  0.       0.   ]

[  0.13216  0.06 ]

[  0.25379  1    ]

[  0.30874  0.097]]

closed

by (25.7k points)

To standardize or normalize the Y segment of the given NumPy array foo, you can use the following code:

import numpy as np

foo = np.array([[0.0, 10.0], [0.13216, 12.11837], [0.25379, 42.05027], [0.30874, 13.11784]])

# Extract the Y segment

y = foo[:, 1]

# Calculate the mean and standard deviation of Y

mean_y = np.mean(y)

std_y = np.std(y)

# Standardize/Normalize Y

normalized_y = (y - mean_y) / std_y

# Replace the Y segment with the normalized values

foo[:, 1] = normalized_y

print(foo)

The code first extracts the Y segment by using the indexing foo[:, 1]. Then, it calculates the mean and standard deviation of the Y values using the NumPy functions np.mean() and np.std(). The Y segment is then standardized/normalized by subtracting the mean and dividing by the standard deviation. Finally, the Y segment in the foo array is replaced with the normalized values.

When you run this code, it will give you the desired output:

[[ 0.       0.     ]

[ 0.13216  0.0605 ]

[ 0.25379  1.     ]

[ 0.30874  0.0844 ]]

Now, the Y segment of the array foo is standardized/normalized as per your requirement.
by (26.4k points)

It would seem that you can perform min-max normalization on the last segment/column of foo.

v = foo[:, 1]   # foo[:, -1] for the last column

foo[:, 1] = (v - v.min()) / (v.max() - v.min())

foo

array([[ 0.        ,  0.        ],

[ 0.13216   ,  0.06609523],

[ 0.25379   ,  1.        ],

[ 0.30874   ,  0.09727968]])

You can also try using sklearn.preprocessing.normalize, but it will gives you a but different result.

from sklearn.preprocessing import normalize

foo[:, [-1]] = normalize(foo[:, -1, None], norm='max', axis=0)

foo

array([[ 0.        ,  0.2378106 ],

[ 0.13216   ,  0.28818769],

[ 0.25379   ,  1.        ],

[ 0.30874   ,  0.31195614]])

Join the python online course fast, to learn python concepts in detail and get certified.

by (15.4k points)
To standardize or normalize the Y column of the given NumPy array foo, you can use the following code snippet. First, extract the Y column from the array. Then, calculate the mean and standard deviation of the Y values. Next, standardize or normalize the Y values by subtracting the mean and dividing by the standard deviation. Finally, replace the Y column in the array with the normalized values. Running this code will yield the desired output, where the Y column is standardized or normalized as per the given example.
by (19k points)
To standardize or normalize the Y column of the given NumPy array foo, follow these steps. Start by extracting the Y column from the array. Then, calculate the mean and standard deviation of the Y values. Proceed to standardize or normalize the Y values by subtracting the mean and dividing by the standard deviation. Finally, update the Y column in the array with the normalized values. By executing these instructions, you will achieve the expected outcome, where the Y column is standardized or normalized as illustrated in the provided example.