0

I wrote the following program

import numpy as np
sig=2
sig2=sig^2
n=100
mu=0
vecX=np.random.normal(loc=mu, scale=sig, size=n)

print(vecX.shape)

Running it, I got the following output: (100,)

My question is, why it is showing (100,) instead of (100,1), and how make it show (100,1) as the size/shape of this array? Thanks!

ExcitedSnail
  • 191
  • 8
  • 1
    `vecX.reshape(100, 1)` ? – Psidom Aug 05 '21 at 03:33
  • 1
    You specified size=100, right? Do you understand the (100,) shape? Why do you want 4100,1)? Reread the function's docs with focus on the `size` parameter. – hpaulj Aug 05 '21 at 03:39
  • 2
    just change, ```n= 100``` to ```n=(100,1)``` and refer to this: https://stackoverflow.com/questions/22053050/difference-between-numpy-array-shape-r-1-and-r – A DUBEY Aug 05 '21 at 03:47
  • 4
    Numpy makes a distinction between a vector (which is what you have) and a matrix with one column, which is what you seem to want. Why do you want that? – Tim Roberts Aug 05 '21 at 03:52
  • @hpaulj Thanks, I read those docs of the random number generator again, and changed size to (100,1), and it worked. I want this because I will also have other $a\times b$ matrices, and want to have the same form for size representation. – ExcitedSnail Aug 05 '21 at 03:54
  • 1
    No worries mate, Glad I helped. – A DUBEY Aug 05 '21 at 03:56
  • @TimRoberts I see. Right, matrix with one column is what I want. As I need a uniform representation of the number of rows and columns in my program for further usage. Thank you very much! – ExcitedSnail Aug 05 '21 at 03:58
  • @ADUBEY May I ask another question? I tried to calculate the mean of this vecX. However, I saw that xbar=np.mean(vecX) and mx=sum(vecX)/len(vecX) do not agree exactly (their difference is very small, but not exactly zero, which is the case in MATLAB). Why do we get such result in python? – ExcitedSnail Aug 05 '21 at 04:12
  • 1
    This is due to rounding in python. So, let's say if you want to have consistent results, you should do something like this```xbar=round(np.mean(vecX) ,4) mx=round(sum(vecX)/len(vecX),4)``` This will give you the same answer – A DUBEY Aug 05 '21 at 06:50
  • @ADUBEY I see. Thank you very much! – ExcitedSnail Aug 05 '21 at 06:55

0 Answers0