0

I have a list (iris data sample) and i want to scale variables (all but last column). I made a loop to do that, but i can't figure out how to merge them after this process.

X = [[5.1, 3.5, 1.4, 0.2, 0.0],
     [4.9, 3.0, 1.4, 0.2, 0.0],
     [4.7, 3.2, 1.3, 0.2, 0.0],
     [4.6, 3.1, 1.5, 0.2, 0.0],
     [5.0, 3.6, 1.4, 0.2, 0.0],
     [5.4, 3.9, 1.7, 0.4, 0.0]]

I've tried to make a loop to scale but i can't figure out how to merge after.

I tried:

from statistics import mean, stdev
for i in range(len(X)):
    valores = []
    for j in range(len(X[i])-1):
        z = []
        coluna = [item[j] for item in X]
        media = mean(coluna)
        desv = stdev(coluna)
        z = [round(((x - media) / desv), 4) for x in coluna]
        valores = valores + z
valores = valores + [valor[-1] for valor in X]

My actual results are:

valores = [0.5207,-0.1736,-0.8678,-1.2149,0.1736,1.562,0.3401,-1.1175,-0.5345,-0.826,0.6316,1.5062,-0.3627,-0.3627,-1.0882,0.3627,-0.3627,1.8137,-0.4082,-0.4082,-0.4082,-0.4082,-0.4082,2.0412,0.0,0.0,0.0,0.0,0.0,0.0]

But i would like to get:

valores = [[0.5207, 0.3401, -0.3627, -0.4082, 0.0],
           [-0.1736, -1.1175, -0.3627, -0.4082, 0.0],
           [-0.8678, -0.5345, -1.0882, -0.4082, 0.0],
           [-1.2149, -0.826, 0.3627, -0.4082, 0.0],
           [0.1736, 0.6316, -0.3627, -0.4082, 0.0],
           [1.562, 1.5062, 1.8137, 2.0412, 0.0]]
1
  • This is a natural data frame; use PANDAS. Commented Sep 25, 2019 at 17:26

2 Answers 2

2

Use pandas:

Data:

X = [[5.1, 3.5, 1.4, 0.2, 0.0],
     [4.9, 3.0, 1.4, 0.2, 0.0],
     [4.7, 3.2, 1.3, 0.2, 0.0],
     [4.6, 3.1, 1.5, 0.2, 0.0],
     [5.0, 3.6, 1.4, 0.2, 0.0],
     [5.4, 3.9, 1.7, 0.4, 0.0]]

Code:

  • Write function def valores to produce the required transformation
  • Create a dataframe with X
  • Apply valores to the appropriate columns in the dataframe
import pandas as pd

def valores(x):
    return [round(((y - x.mean()) / x.std()), 4) for y in x]

df = pd.DataFrame(X)

df[[0, 1, 2, 3]] = df[[0, 1, 2, 3]].apply(lambda x: valores(x))

Output:

      0       1       2       3    4
 0.5207  0.3401 -0.3627 -0.4082  0.0
-0.1736 -1.1175 -0.3627 -0.4082  0.0
-0.8678 -0.5345 -1.0882 -0.4082  0.0
-1.2149 -0.8260  0.3627 -0.4082  0.0
 0.1736  0.6316 -0.3627 -0.4082  0.0
 1.5620  1.5062  1.8137  2.0412  0.0
Sign up to request clarification or add additional context in comments.

Comments

0

Not elegant:

out = []
for i in range(1+len(valores)//len(X)):
    aux = []
    for j in range(len(X[0])):
        aux.append(valores[i+len(X)*j])
    out.append(aux)
print(out)

[[0.5207, 0.3401, -0.3627, -0.4082, 0.0], [-0.1736, -1.1175, -0.3627, -0.4082, 0.0], [-0.8678, -0.5345, -1.0882, -0.4082, 0.0], [-1.2149, -0.826, 0.3627, -0.4082, 0.0], [0.1736, 0.6316, -0.3627, -0.4082, 0.0], [1.562, 1.5062, 1.8137, 2.0412, 0.0]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.