給出一個graphlab.SArray
命名coef
:矩陣乘法與SFrame和SArray與Graphlab和/或numpy的
+-------------+----------------+
| name | value |
+-------------+----------------+
| (intercept) | 87910.0724924 |
| sqft_living | 315.403440552 |
| bedrooms | -65080.2155528 |
| bathrooms | 6944.02019265 |
+-------------+----------------+
[4 rows x 2 columns]
和一個名爲x
graphlab.SFrame
(下圖1 10所示):
+-------------+----------+-----------+-------------+
| sqft_living | bedrooms | bathrooms | (intercept) |
+-------------+----------+-----------+-------------+
| 1430.0 | 3.0 | 1.0 | 1 |
| 2950.0 | 4.0 | 3.0 | 1 |
| 1710.0 | 3.0 | 2.0 | 1 |
| 2320.0 | 3.0 | 2.5 | 1 |
| 1090.0 | 3.0 | 1.0 | 1 |
| 2620.0 | 4.0 | 2.5 | 1 |
| 4220.0 | 4.0 | 2.25 | 1 |
| 2250.0 | 4.0 | 2.5 | 1 |
| 1260.0 | 3.0 | 1.75 | 1 |
| 2750.0 | 4.0 | 2.0 | 1 |
+-------------+----------+-----------+-------------+
[1000 rows x 4 columns]
怎麼辦我操縱SArray和SFrame,這樣乘法將返回一個具有第一行的矢量SArray,計算如下?:
87910.0724924 * 1
+ 315.403440552 * 1430.0
+ -65080.2155528 * 3.0
+ 6944.02019265 * 1.0
= 350640.36601600994
我目前在做愚蠢的事情轉化SFrame/SArray成列表,然後將其轉換成numpy的陣列做np.multiply
。即使轉換成numpy數組後,它也沒有給出正確的矩陣向量乘法。我目前的嘗試:
import numpy as np
coef # as should in SArray above.
x # as should in the SFrame above.
intercept = list(x['(intercept)'])
sqftliving = list(x['sqft_living'])
bedrooms = list(x['bedrooms'])
bathrooms = list(x['bathrooms'])
x_new = np.column_stack((intercept, sqftliving, bedrooms, bathrooms))
coef_new = np.array(list(coef['value']))
np.multiply(coef_new, x_new)
(錯)[出]:
[[ 87910.07249236 451026.91998949 -195240.64665846 6944.02019265]
[ 87910.07249236 930440.14962867 -260320.86221128 20832.06057795]
[ 87910.07249236 539339.88334408 -195240.64665846 13888.0403853 ]
...,
[ 87910.07249236 794816.67019127 -260320.86221128 17360.05048162]
[ 87910.07249236 728581.94767533 -260320.86221128 17360.05048162]
[ 87910.07249236 321711.50936313 -130160.43110564 5208.01514449]]
我嘗試的輸出是錯的太多,它應該返回一個矢量的標量值。必須有一個更簡單的方法來做到這一點。
我該如何操作SArray和SFrame,使乘法運算返回一個具有第一行的矢量SArray,計算如下?
和numpy
數據幀,應該如何執行矩陣向量乘法?
; P是啊,從coursera的華盛頓大學課程!我通過投入numpy做了它。 'predictions = [np.dot(row,weights)for feature_matrix]' – alvas
這也是一個選項。祝你好運學習:-)課程非常有趣。 –