user11597888
user11597888

Reputation:

What is the best way to calculate the standard deviation

I have a 50x22 matrix/vector, and i need to calculate the standard deviation from each column, precisely it looks like this.enter image description here

For instance, the first column: [245, 244, 247, 243... 239], then second column [115, 106, 99...149] and on...

I did try calculating it using a calculator, i guess it wasn't a good idea, it took me a while, but i figured numpy could help me.

The thing is, what i tried was:

std_vec = np.std(img_feats[0])

The output was all wrong, when i compared to the firt column that i obtained using the calculator.

This is the actual output, since the image picture does not look good.

[247, 111, 162, 47, 39, 42, 47, 42, 204, 215, 50, 57, 196, 209, 199, 184, 219, 201, 204, 218]
[247, 108, 160, 47, 39, 44, 48, 44, 204, 213, 51, 60, 198, 205, 201, 184, 219, 201, 202, 216]
[245, 96, 160, 46, 38, 43, 44, 40, 195, 213, 51, 57, 199, 202, 204, 187, 222, 201, 202, 207]
[245, 102, 161, 46, 40, 41, 46, 40, 198, 213, 51, 55, 199, 210, 202, 187, 220, 202, 204, 210]
[246, 104, 160, 49, 38, 41, 46, 41, 198, 214, 52, 55, 199, 210, 204, 185, 220, 204, 204, 208]
[246, 107, 160, 47, 40, 40, 46, 40, 196, 213, 51, 57, 199, 207, 201, 185, 220, 202, 205, 208]
[246, 116, 161, 48, 40, 43, 45, 41, 201, 213, 51, 58, 201, 201, 204, 184, 219, 204, 205, 210]
[246, 123, 169, 48, 40, 42, 46, 43, 202, 213, 51, 64, 199, 198, 201, 189, 217, 199, 207, 216]
[246, 123, 167, 48, 40, 40, 45, 43, 204, 213, 51, 67, 198, 199, 202, 187, 219, 199, 207, 214]
[246, 122, 167, 48, 40, 43, 46, 43, 202, 213, 51, 64, 199, 197, 200, 185, 219, 199, 208, 213]
[245, 115, 165, 47, 38, 43, 44, 43, 199, 213, 51, 58, 199, 191, 203, 187, 217, 194, 206, 208]
[244, 106, 162, 47, 40, 40, 43, 40, 194, 214, 49, 58, 199, 188, 205, 191, 216, 199, 203, 208]
[247, 99, 160, 46, 42, 40, 46, 42, 201, 213, 51, 55, 198, 204, 201, 185, 220, 199, 202, 216]
[243, 98, 161, 47, 41, 43, 43, 40, 193, 214, 50, 58, 199, 191, 207, 190, 214, 197, 205, 207]
[242, 96, 159, 46, 40, 41, 44, 43, 188, 214, 50, 56, 200, 197, 208, 190, 214, 197, 205, 205]
[243, 99, 164, 47, 40, 41, 44, 41, 189, 213, 49, 55, 201, 198, 208, 192, 215, 198, 204, 205]
[245, 105, 166, 49, 40, 41, 44, 40, 193, 215, 49, 56, 199, 202, 207, 192, 218, 201, 207, 208]
[246, 112, 167, 46, 40, 44, 44, 43, 195, 211, 50, 60, 198, 204, 205, 190, 218, 201, 205, 210]
[245, 112, 166, 47, 40, 43, 44, 43, 199, 214, 49, 59, 202, 205, 205, 188, 219, 201, 208, 210]
[245, 112, 165, 47, 39, 44, 46, 43, 197, 213, 50, 58, 200, 204, 205, 188, 219, 202, 208, 211]
[245, 111, 167, 45, 42, 42, 42, 41, 199, 213, 50, 59, 200, 203, 210, 187, 219, 200, 208, 211]
[245, 115, 167, 48, 41, 42, 44, 42, 199, 212, 51, 62, 200, 205, 206, 187, 219, 202, 209, 211]
[244, 122, 168, 47, 42, 41, 45, 41, 199, 212, 51, 63, 200, 203, 206, 185, 219, 200, 208, 212]
[247, 99, 160, 45, 40, 42, 48, 43, 200, 211, 51, 55, 197, 202, 199, 184, 220, 199, 204, 214]
[244, 121, 168, 50, 39, 42, 43, 40, 202, 211, 50, 63, 200, 199, 208, 183, 220, 203, 208, 212]
[245, 121, 167, 50, 40, 42, 43, 40, 200, 212, 50, 63, 202, 197, 208, 186, 220, 203, 208, 215]
[245, 119, 165, 48, 40, 42, 45, 42, 199, 212, 50, 62, 202, 194, 206, 186, 218, 200, 209, 211]
[245, 124, 165, 47, 37, 42, 45, 40, 202, 211, 50, 63, 202, 194, 206, 185, 217, 199, 209, 215]
[244, 129, 168, 47, 39, 40, 45, 42, 208, 209, 50, 71, 202, 197, 206, 187, 214, 199, 209, 215]
[244, 134, 173, 50, 39, 42, 45, 42, 208, 209, 51, 80, 202, 199, 206, 189, 209, 199, 208, 217]
[243, 140, 176, 54, 39, 40, 45, 40, 209, 211, 50, 83, 205, 201, 208, 189, 205, 198, 212, 220]
[242, 142, 177, 65, 39, 42, 44, 40, 215, 212, 50, 97, 201, 203, 206, 189, 203, 200, 211, 220]
[241, 147, 182, 74, 39, 42, 44, 41, 212, 214, 51, 106, 204, 203, 209, 191, 200, 195, 209, 221]
[241, 151, 182, 78, 39, 39, 45, 42, 212, 214, 51, 108, 206, 203, 206, 192, 197, 194, 206, 220]
[246, 99, 159, 46, 38, 41, 46, 41, 197, 213, 50, 54, 199, 203, 202, 182, 222, 197, 200, 213]
[239, 151, 183, 81, 37, 42, 45, 40, 212, 211, 50, 112, 206, 203, 206, 191, 194, 197, 209, 220]
[238, 149, 185, 78, 41, 41, 47, 41, 211, 211, 51, 111, 207, 204, 206, 192, 192, 195, 207, 220]
[238, 147, 182, 74, 39, 39, 45, 44, 211, 211, 51, 107, 209, 206, 207, 192, 195, 194, 207, 221]
[241, 146, 183, 73, 39, 41, 45, 44, 212, 211, 51, 107, 208, 206, 206, 191, 197, 195, 209, 221]
[237, 147, 182, 80, 41, 41, 45, 44, 210, 212, 51, 108, 209, 209, 209, 195, 195, 197, 210, 221]
[240, 150, 183, 85, 41, 42, 45, 42, 210, 213, 53, 112, 210, 207, 209, 194, 197, 195, 209, 220]
[241, 150, 180, 83, 36, 44, 45, 40, 210, 213, 51, 112, 207, 204, 207, 192, 195, 192, 207, 219]
[239, 149, 180, 83, 42, 42, 46, 42, 210, 213, 51, 109, 209, 204, 209, 192, 198, 190, 209, 221]
[239, 149, 183, 84, 40, 42, 46, 40, 208, 213, 51, 111, 210, 204, 208, 192, 198, 187, 208, 219]
[238, 151, 184, 87, 40, 42, 43, 43, 208, 213, 51, 113, 213, 205, 208, 190, 195, 190, 208, 220]
[246, 99, 158, 44, 38, 40, 44, 41, 196, 213, 54, 55, 199, 205, 200, 182, 220, 196, 200, 213]
[246, 98, 156, 46, 40, 41, 47, 44, 197, 214, 51, 55, 199, 210, 202, 184, 220, 196, 199, 211]
[245, 96, 158, 44, 40, 41, 46, 43, 194, 213, 54, 55, 202, 205, 202, 187, 220, 199, 202, 210]
[244, 92, 157, 44, 38, 40, 46, 41, 191, 213, 51, 54, 199, 205, 205, 187, 219, 198, 202, 208]
[244, 92, 157, 46, 38, 40, 44, 44, 191, 213, 51, 54, 198, 205, 207, 188, 219, 199, 202, 208]

These results are comming from features extracted from my images, not sure if its relevant though, but here's my actual code:

for img1 in imgs1:
    img_feats = []
    for coords in coord_list:
        std_vec = np.std(img_feats[0])
        img_feats.append(img1[coords[0], coords[1]]) # extracts the features that composes my matrix
    print(img_feats)

The output should look like this [1.879 3.0284 5.9333 2.0156 2.2467 2.0092 4.7983 4.3554 3.6372 1.3159 2.6174 2.2336 0.9625 5.6285 5.4040 2.7887 0 3.4632 0 2.7370]

Upvotes: 1

Views: 100

Answers (3)

J.Warren
J.Warren

Reputation: 778

Like most numpy functions, the std function has an axis parameter. np.std(img_feats, axis=0) will return the standard deviation of each column. axis=1 will give the standard deviation across of each row.

In your example this would read:

feat_vec_1 = []
for img1 in imgs1:
    img_feats = []
    for coords in coord_list:
        img_feats.append(img1[coords[0], coords[1]])
    feat_vec_1.append(img_feats) 

std_colomns = np.std(feat_vec_1, axis=0)

Upvotes: 1

Igor Rivin
Igor Rivin

Reputation: 4864

I am not sure what you are really asking, but if your array is foo, then

np.std(foo, axis=0)

Will compute the standard deviations of all the columns.

Upvotes: 1

NKULTRA
NKULTRA

Reputation: 26

for img1 in imgs1:
    img_feats = []
    for coords in coord_list:
        std_vec = np.std(img_feats[0])
        img_feats.append(img1[coords[0], coords[1]])
    feat_vec_1.append(img_feats) 
    print(img_feats) 

I don't get this actually, maybe you should calculate the deviation after you built the whole matrix? And the column you get with img_feats[0,:] if it is a numpy array

Upvotes: 0

Related Questions