Funny Rank 1 matrix

While implementing Logistic Regression, I encountered an issue with Numpy rank-1 matrix.
The issue is better describe in code. I am Attaching below the code snippet.

Issue:

when you subtract rank-1 matrix of shape-->(10,) with a column vector of shape-->(10,1). We get a result with dimension -->(10,10).
Solution:
  • Always reshape your matrix before operating on them by arr.reshape(dim1,dim2)
  • Always check matrix shape if unsure by arr.shape

** "arr" is the matrix name




Code Snippet:

g1=numpy.arange(10)
g1.shape #(10,)
g2=numpy.arange(10)
g2=g2.reshape(10,1)
g3=np.arange(10)
g3.shape #(10,)
result1=g1-g2
result1.shape #(10,10)
result2=g1-g3
result2.shape #(10,)

No comments:

Post a Comment

Pages

Ankan Sharma

Mobile Developer | AI Enthusiast | Kaggle 2x Expert

Flutter | Python | Nodejs | React | Mongodb

Tech Blog Writer @Medium

Get in touch

Contact Us FAQs Support
Under Maintenance