Matrix Loop with multiple vectors: Pyhton

Asked

Viewed 158 times

3

Good evening, everyone

I’m a beginner in Python and I would like your help, I did this program but the answer is in the form of several vectors or 24 vectors, and in reality I wanted only a Matrix(24,1), follows below what I did, I thank you already:

import numpy as np
import pandas as pd

ex = pd.read_excel('3.1.xlsx')
meanX1 = np.mean(ex.X1)
meanX2 = np.mean(ex.X2)
meanI = np.mean(ex.I)
stdX1 =np.std(ex.X1)
stdX2 =np.std(ex.X2)
stdI =np.std(ex.I)

for i in range(0, 25):
        aux = np.zeros([1,1])
        X1std = aux + (ex.X1[i] - meanX1)/stdX1
        print(X1std)

the answer is this with 24 vectors, I would like a column Matrix with 24 lines:

[[-0.26102763]]
[[-0.85427226]]
[[-0.85427226]]
[[-0.7059611]]
[[-0.40933879]]
[[-0.26102763]]
[[-1.00258342]]
[[-0.26102763]]
[[3.15012896]]
[[-0.55764995]]
[[1.07377277]]
[[0.18390583]]
[[-0.7059611]]
[[-0.40933879]]
[[0.03559468]]
[[0.18390583]]
[[-0.40933879]]
[[-0.26102763]]
[[-0.85427226]]
[[1.22208393]]
[[0.18390583]]
[[2.55688433]]
[[0.03559468]]
[[-0.11271648]]
[[-0.7059611]]

1 answer

1


It’s very quiet to do, just need to initialize an array of zeros of the size you want and then go filling in with the values you get.

import numpy as np

Matriz = np.zeros((24,1)) # Inicializa a matriz no formato de 24 linhas por 1 coluna,                               com zeros

for item in range(24):
    Matriz[item][0] = item  # Preenche a matriz com o índice do for

print(Matriz)

print(Matriz[2][0]) # Imprime o valor da 3ª linha

I couldn’t run your code because of dependencies from other places, but the code I did generates the output:

[[ 0.]
 [ 1.]
 [ 2.]
 [ 3.]
 [ 4.]
 [ 5.]
 [ 6.]
 [ 7.]
 [ 8.]
 [ 9.]
 [10.]
 [11.]
 [12.]
 [13.]
 [14.]
 [15.]
 [16.]
 [17.]
 [18.]
 [19.]
 [20.]
 [21.]
 [22.]
 [23.]]
2.0
  • Thank you very much !!! his idea led me to other paths and tried his also unsuccessfully, I went to find the answer in Pandas as as follows:Xistd = pd.Series([ex.XI[i] - meanXI)/stdXI for i in range(0, 25)]) XI = Xistd.values

  • For nothing, consider evaluating the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.