1. 程式人生 > >機器學習實戰logistics迴歸語法

機器學習實戰logistics迴歸語法

陣列和矩陣計算的區別。通過getA()可以把矩陣轉化為陣列 

#
from numpy import*
w = ones((3, 1))         #建立陣列
weights = mat(w)            #轉換為numpy矩陣
s = weights.getA()          #將numpy矩陣轉換為陣列
x = arange(-3.0, 3.0, 0.1)
y1 = (-weights[0] - weights[1]*x)/weights[2] #矩陣
y2 = (s[0] - s[1] *x)/s[2]                   #陣列
print(len(y1))
print(y1)
print(len(y2))
print(y2)


1
[[ 2.00000000e+00  1.90000000e+00  1.80000000e+00  1.70000000e+00
   1.60000000e+00  1.50000000e+00  1.40000000e+00  1.30000000e+00
   1.20000000e+00  1.10000000e+00  1.00000000e+00  9.00000000e-01
   8.00000000e-01  7.00000000e-01  6.00000000e-01  5.00000000e-01
   4.00000000e-01  3.00000000e-01  2.00000000e-01  1.00000000e-01
  -1.77635684e-15 -1.00000000e-01 -2.00000000e-01 -3.00000000e-01
  -4.00000000e-01 -5.00000000e-01 -6.00000000e-01 -7.00000000e-01
  -8.00000000e-01 -9.00000000e-01 -1.00000000e+00 -1.10000000e+00
  -1.20000000e+00 -1.30000000e+00 -1.40000000e+00 -1.50000000e+00
  -1.60000000e+00 -1.70000000e+00 -1.80000000e+00 -1.90000000e+00
  -2.00000000e+00 -2.10000000e+00 -2.20000000e+00 -2.30000000e+00
  -2.40000000e+00 -2.50000000e+00 -2.60000000e+00 -2.70000000e+00
  -2.80000000e+00 -2.90000000e+00 -3.00000000e+00 -3.10000000e+00
  -3.20000000e+00 -3.30000000e+00 -3.40000000e+00 -3.50000000e+00
  -3.60000000e+00 -3.70000000e+00 -3.80000000e+00 -3.90000000e+00]]
60
[ 4.00000000e+00  3.90000000e+00  3.80000000e+00  3.70000000e+00
  3.60000000e+00  3.50000000e+00  3.40000000e+00  3.30000000e+00
  3.20000000e+00  3.10000000e+00  3.00000000e+00  2.90000000e+00
  2.80000000e+00  2.70000000e+00  2.60000000e+00  2.50000000e+00
  2.40000000e+00  2.30000000e+00  2.20000000e+00  2.10000000e+00
  2.00000000e+00  1.90000000e+00  1.80000000e+00  1.70000000e+00
  1.60000000e+00  1.50000000e+00  1.40000000e+00  1.30000000e+00
  1.20000000e+00  1.10000000e+00  1.00000000e+00  9.00000000e-01
  8.00000000e-01  7.00000000e-01  6.00000000e-01  5.00000000e-01
  4.00000000e-01  3.00000000e-01  2.00000000e-01  1.00000000e-01
 -3.55271368e-15 -1.00000000e-01 -2.00000000e-01 -3.00000000e-01
 -4.00000000e-01 -5.00000000e-01 -6.00000000e-01 -7.00000000e-01
 -8.00000000e-01 -9.00000000e-01 -1.00000000e+00 -1.10000000e+00
 -1.20000000e+00 -1.30000000e+00 -1.40000000e+00 -1.50000000e+00
 -1.60000000e+00 -1.70000000e+00 -1.80000000e+00 -1.90000000e+00]

#
from numpy import*
a=[[1,2,3],[4,5,6]]
print(a)
print(array(a))
print(mat(a))
print(array(a[1]))
print(array(a)[1].shape)
print(mat(a)[1].shape)

[[1, 2, 3], [4, 5, 6]]
[[1 2 3]
 [4 5 6]]
[[1 2 3]
 [4 5 6]]
[4 5 6]
(3,)
(1, 3)