我想將一些Python轉換爲F#,特別是numpy.random.randn。從一個函數返回不同尺寸的數組;在F#中可能嗎?
該函數接受可變數量的int參數,並根據參數的數量返回不同維度的數組。
我認爲這是不可能的,因爲一個不能返回不同類型的函數(int[]
,int[][]
,int[][][]
等),除非它們是歧視工會的一部分,但要承諾一個解決辦法之前,以確保。
的健全性檢查:
member self.differntarrays ([<ParamArray>] dimensions: Object[]) =
match dimensions with
| [| dim1 |] ->
[|
1
|]
| [| dim1; dim2 |] ->
[|
[| 2 |],
[| 3 |]
|]
| _ -> failwith "error"
原因錯誤:
This expression was expected to have type
int
but here has type
'a * 'b
與expression
感:[| 2 |], [| 3 |]
和int
參照1 [| 1 |]
即1
類型是不一樣的[| 2 |], [| 3 |]
TLDR;從交互式Python會話
numpy.random.randn(d0, d1, ..., dn)
Return a sample (or samples) from the 「standard normal」 distribution.
If positive, int_like or int-convertible arguments are provided, randn generates an array of shape (d0, d1, ..., dn), filled with random floats sampled from a univariate 「normal」 (Gaussian) distribution of mean 0 and variance 1 (if any of the d_i are floats, they are first converted to integers by truncation). A single float randomly sampled from the distribution is returned if no argument is provided.
例子:
np.random.randn(1) - array([-0.28613356])
np.random.randn(2) - array([-1.7390449 , 1.03585894])
np.random.randn(1,1)- array([[ 0.04090027]])
np.random.randn(2,3)- array([[-0.16891324, 1.05519898, 0.91673992],
[ 0.86297031, 0.68029926, -1.0323683 ]])
代碼爲Neural Networks and Deep Learning,並因爲這些值需要可變因爲性能原因,使用不可變列表是不是一種選擇。
你可能會需要使用DU –
可能有不同的解決方法比杜:成員self.differntarrays([]尺寸:對象[]):對象[] - 請注意它返回的對象[]。這可能會導致下游出現問題,因此尚未提交。 –
注意:[如何使函數返回fsharp中真正不同的類型?](http://stackoverflow.com/q/24218051/1243762) –