Linear regression compute theta I'm trying to compute the theta for a regression linear exercice.

x = x * 1.0
y = y * 1.0

# add ones
X = np.ones((201, 2))
X[:, 1] = x

# convert to matrix
Y = y[:,np.newaxis]

# compute teta
p1 = (X.dot(X.T))**-1
p2 = (X.T).dot(Y)

theta = p1.dot(p2)


The last line failed with error : 


  ValueError: shapes (201,201) and (2,1) not aligned: 201 (dim 1) != 2
  (dim 0)


I don't understand why. I'm simply trying to implement this:



here my x

[ 69.  95.  21.  20.  33.  13.  17.  27.  32.  26.  25.  31.  18.  24.
  25.  25.  27.  37.  28.  39.  31.  25.  25.  29.  38.  16.  20.  50.
  37.  33.  40.  46.  45.  19.  45.  56.  60.  23.  49.  51.  48.  51.
  41.  47.  45.  47.  37.  52.  45.  43.  26.  49.  49.  39.  60.  65.
  60.  60.  47.  57.  65.  33.  56.  69.  61.  60.  47.  49.  60.  72.
  70.  59.  30.  39.  25.  68.  63.  78.  50.  70.  75.  60.  70.  28.
  62.  90.  78.  80.  72.  78.  62.  80.  80.  75.  68.  76.  81.  92.
  75.  82.  80.  95.  85.  58.  33.  94. 100.  77.  80.  80.  92.  92.
  99.  98.  90.  96.  92.  86.  73.  80.  96.  72.  91.  53.  60.  95.
  97. 103. 105. 112. 110. 107.  65.  97. 110. 102. 101.  98. 109. 120.
 107. 125. 120. 108. 130. 112. 121. 118. 107.  87. 114. 110. 118. 126.
 112.  93.  80. 116. 145.  95. 100.  98.  94. 110. 100. 110. 109. 131.
 133.  86.  84. 145. 113. 130. 108.  94. 136. 140. 125. 156.  91. 130.
 102. 130. 142.  88. 178. 150. 185. 164. 214. 191. 156. 145. 175. 150.
 129. 160. 201. 240. 145.]


And my y :

[1810. 2945.  685.  720.  830.  850.  850.  855.  875.  890.  890.  900.
  900.  900.  920.  930.  950.  955.  960.  970.  980.  980.  980. 1000.
 1015. 1040. 1060. 1100. 1130. 1160. 1200. 1210. 1235. 1250. 1310. 1315.
 1320. 1350. 1370. 1385. 1390. 1400. 1400. 1410. 1410. 1415. 1420. 1445.
 1450. 1450. 1470. 1480. 1490. 1530. 1530. 1580. 1590. 1590. 1595. 1630.
 1640. 1650. 1660. 1690. 1690. 1690. 1700. 1700. 1700. 1715. 1730. 1750.
 1750. 1750. 1780. 1790. 1790. 1790. 1800. 1810. 1830. 1840. 1840. 1850.
 1860. 1870. 1920. 1930. 1940. 1950. 1980. 1990. 2000. 2030. 2040. 2060.
 2080. 2085. 2090. 2130. 2130. 2145. 2160. 2160. 2170. 2190. 2250. 2270.
 2270. 2290. 2320. 2335. 2335. 2358. 2360. 2380. 2380. 2390. 2400. 2400.
 2403. 2410. 2420. 2425. 2490. 2500. 2530. 2550. 2550. 2550. 2560. 2570.
 2570. 2590. 2625. 2635. 2675. 2700. 2710. 2710. 2720. 2725. 2750. 2805.
 2820. 2825. 2830. 2840. 2840. 2850. 2850. 2870. 2875. 2900. 2915. 2945.
 2950. 3050. 3050. 3080. 3090. 3090. 3100. 3150. 3160. 3180. 3220. 3300.
 3300. 3350. 3400. 3450. 3490. 3500. 3525. 3570. 3765. 3765. 3790. 3930.
 3950. 3965. 4061. 4200. 4260. 4310. 4760. 4800. 4900. 5160. 5200. 5229.
 5250. 5383. 5460. 5500. 5560. 5775. 6200. 6700. 7383.]
p1 = (X.dot(X.T))**-1 needs to be p1 = np.linalg.inv((X.T.dot(X))) 

X**-1 doesn't invert a matrix, this is equivalent to 1/X (elementwise) and you need to transpose the first not the second matrix.