using System;
using System.Collections.Generic;
using NumSharp;
//using NumpyDotNet;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[,] arr = new int[2, 3] { { 5, 6, 7 }, { 8, 7, 6 } };
NDArray ndarr = np.array(arr);
Console.WriteLine(ndarr.ToString());
Console.WriteLine("------------------------");
//int[,] arr1 = np.arange(0, 9).reshape(3, 3);
NDArray arr1 = np.array(new int[,] { { 1, 2, 3 }, { 4, 5, 6 } });
var arr2 = np.matmul(arr1.T, arr1);
Console.WriteLine(arr2.ToString());
Console.WriteLine("------------------------");
var arr3 = arr2[":,1:3"];
Console.WriteLine(arr3.ToString());
Console.WriteLine("***************");
foreach (var item in arr3)
{
Console.WriteLine(item);
}
Console.WriteLine("***************");
IEnumerable<int> iter = arr3.AsIterator<int>();
foreach (var item in iter)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
结果:
[[5, 6, 7],
[8, 7, 6]]
------------------------
[[17, 22, 27],
[22, 29, 36],
[27, 36, 45]]
------------------------
[[22, 27],
[29, 36],
[36, 45]]
***************
22
27
29
36
36
45
***************
22
27
29
36
36
45
官方例程:
using NumSharp;
var nd = np.full(5, 12); //[5, 5, 5 .. 5]
nd = np.zeros(12); //[0, 0, 0 .. 0]
nd = np.arange(12); //[0, 1, 2 .. 11]
// create a matrix
nd = np.zeros((3, 4)); //[0, 0, 0 .. 0]
nd = np.arange(12).reshape(3, 4);
// access data by index
var data = nd[1, 1];
// create a tensor
nd = np.arange(12);
// reshaping
data = nd.reshape(2, -1); //returning ndarray shaped (2, 6)
Shape shape = (2, 3, 2);
data = nd.reshape(shape); //Tuple implicitly casted to Shape
//or:
nd = nd.reshape(2, 3, 2);
// slicing tensor
data = nd[":, 0, :"]; //returning ndarray shaped (2, 1, 2)
data = nd[Slice.All, 0, Slice.All]; //equivalent to the line above.
// nd is currently shaped (2, 3, 2)
// get the 2nd vector in the 1st dimension
data = nd[1]; //returning ndarray shaped (3, 2)
// get the 3rd vector in the (axis 1, axis 2) dimension
data = nd[1, 2]; //returning ndarray shaped (2, )
// get flat representation of nd
data = nd.flat; //or nd.flatten() for a copy
// interate ndarray
foreach (object val in nd)
{
// val can be either boxed value-type or a NDArray.
}
var iter = nd.AsIterator<int>(); //a different T can be used to automatically perform cast behind the scenes.
while (iter.HasNext())
{
//read
int val = iter.MoveNext();
//write
iter.MoveNextReference() = 123; //set value to the next val
//note that setting is not supported when calling AsIterator<T>() where T is not the dtype of the ndarray.
}
与二维数组的互相转化
// See https://aka.ms/new-console-template for more information
using NumSharp;
//测试numsharp
int[,] ints = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
NDArray dArray1 = np.array(ints);
foreach (var item in dArray1)
{
Console.WriteLine($"{item}");
}
//dArray1.ToArray();
int a = dArray1.size;
int[] b = dArray1.shape;
Console.WriteLine($"{dArray1[0][1]},{a},{b[0]},{b[1]}");
int[,] ints1 = new int[dArray1.shape[0],dArray1.shape[1]];
for (int i = 0; i < ints1.GetLength(0); i++)
{
for (int j = 0; j < ints1.GetLength(1); j++)
{
ints1[i,j] = dArray1[i][j];
}
}
for (int i = 0; i < ints1.GetLength(0); i++)
{
for (int j = 0; j < ints1.GetLength(1); j++)
{
Console.WriteLine($"写入的值为:{ints1[i, j]}");
}
}
//结果:
/*
1
2
3
4
5
6
2,6,2,3
写入的值为:1
写入的值为:2
写入的值为:3
写入的值为:4
写入的值为:5
写入的值为:6
*/