There is the following code:

import numpy as np X = ((1,10),(1,7),(1,12)) step = np.dot(XT,X) 

but the XT method does not work and throws out an error

step1 = np.dot (XT, X) Traceback (most recent call last): File "", line 1, in AttributeError: 'tuple' object has no attribute 'T'

What am I doing wrong?

  • 2
    Tuple has no attribute T - Alexander Chernin

1 answer 1

As indicated in the error message, the tuple object has no .T attribute. This attribute is in numpy.ndarray , so create an NDArray :

 In [17]: X = np.array(((1,10),(1,7),(1,12))) In [18]: np.dot(XT, X) Out[18]: array([[ 3, 29], [ 29, 293]]) 

Alternative solution using tuple :

 In [21]: X = ((1,10),(1,7),(1,12)) In [22]: np.dot(np.transpose(X), X) Out[22]: array([[ 3, 29], [ 29, 293]]) 

A few more Numpy style Numpy :

 In [27]: X = np.array(((1,10),(1,7),(1,12))) In [28]: XT @ X Out[28]: array([[ 3, 29], [ 29, 293]]) In [29]: XTdot(X) Out[29]: array([[ 3, 29], [ 29, 293]])