# A quick start guide to the Point and Sample classes¶

## Abstract¶

In this example, we present the Point and Sample classes, two fundamental objects in the library. We present the principles behind these classes and the way to create and use these objects. We show how to extract a row or a column with the slicing operator. We show how these objects interacts with Python variables and with the numpy module.

## Introduction¶

Two fundamental objects in the library are:

• Point: a multidimensional point in dimensions () ;

• Sample: a multivariate sample made of points in dimensions.

[1]:

import openturns as ot


## The Point class¶

In this section, we see how to:

• create a point in ,

• access its components,

• update its components.

By default, points are filled with zeros.

[2]:

p = ot.Point(3)
p

[2]:


[0,0,0]

The following statement returns the value of the second component (with index 1). Python beginners should remember that Python indices start at zero.

[3]:

p[1]

[3]:

0.0


The following statements sets the second component.

[4]:

p[1] = 2
p

[4]:


[0,2,0]

[5]:

p.getDimension ()

[5]:

3


## The Sample class¶

The Sample class represents a multivariate sample made of points in .

• is the dimension of the sample,

• is the size of the sample.

A Sample can be seen as an array of with rows and columns.

Remark. The ProcessSample class can be used to manage a sample of stochastic processes.

The script below creates a Sample with size and dimension .

[6]:

data = ot.Sample(5, 3)
data

[6]:

 v0 v1 v2 0 0.0 0.0 0.0 1 0.0 0.0 0.0 2 0.0 0.0 0.0 3 0.0 0.0 0.0 4 0.0 0.0 0.0
[7]:

data.getSize()

[7]:

5

[8]:

data.getDimension()

[8]:

3


The following statement sets the third component (with index 2) of the fourth point (with index 3) in the Sample.

[9]:

data [3, 2] = 32
data

[9]:

 v0 v1 v2 0 0.0 0.0 0.0 1 0.0 0.0 0.0 2 0.0 0.0 0.0 3 0.0 0.0 32.0 4 0.0 0.0 0.0

## Get a row or a column of a Sample¶

As with numpy arrays, we can extract a row or a column with the : slicing operator. As a reminder for Python beginners, slicing is the fact of extracting a part of an array with one single statement; this avoids for loops and improves performance and readability.

[10]:

row = data [3, :]
row

[10]:


[0,0,32]

[11]:

type ( row )

[11]:

openturns.typ.Point

[12]:

column = data [:, 2]
column

[12]:

 v2 0 0.0 1 0.0 2 0.0 3 32.0 4 0.0
[13]:

type ( column )

[13]:

openturns.typ.Sample


We see that:

• the row is a Point,

• the column is a Sample.

This is consistent with the fact that, in a dimension Sample, a row is a -dimensional Point.

The following statement extracts several columns (with indices 0 and 2) and creates a new Sample.

[14]:

data.getMarginal ([0 , 2])

[14]:

 v0 v1 0 0.0 0.0 1 0.0 0.0 2 0.0 0.0 3 0.0 32.0 4 0.0 0.0

## Create a Point or a Sample from a Python list¶

The following statement creates a Point from a Python list.

[15]:

p1 = ot.Point ([2 , 3])
p1

[15]:


[2,3]

[16]:

p2 = ot.Point(range(2))
p2

[16]:


[0,1]

The first useful Pythonism that we will review is the list comprehension. This creates a list from a for loop. This kind of statements is often used in the the examples, so that they can be as short as possible.

In the following statement, we create a point by iterating over the components of a Point.

[17]:

p3 = ot.Point ([i*i for i in p1])
p3

[17]:


[4,9]

The second useful Pythonism is the repetition with the * operator.

The following statements creates a list with three 5s.

[18]:

p4 = [5] * 3
p4

[18]:

[5, 5, 5]


We can also create a Sample from a list of Points.

[19]:

sample = ot.Sample ([p1 , p2 , p3 ])
sample

[19]:

 v0 v1 0 2.0 3.0 1 0.0 1.0 2 4.0 9.0

We can loop over the points in a sample, using a list comprehension. In the following example, we compute the Euclidian norm of the points in the previous sample.

[20]:

[point.norm() for point in sample]

[20]:

[3.605551275463989, 1.0, 9.848857801796104]


We can also create a Sample based on a Point, repeated three times.

[21]:

sample = ot.Sample ([p4] * 3)
sample

[21]:

 v0 v1 v2 0 5.0 5.0 5.0 1 5.0 5.0 5.0 2 5.0 5.0 5.0

A nested list of floats is the easiest way to create a non-trivial Sample.

[22]:

sample = ot.Sample ([[0 , 1], [2, 3], [4, 5]])
sample

[22]:

 v0 v1 0 0.0 1.0 1 2.0 3.0 2 4.0 5.0

## Interactions with Numpy¶

The Python classes defined in Python modules are unknown to OpenTURNS and hence cannot be used by the library. This is why it is useful to know how to convert to and from more basic Python variable types, especially Numpy arrays.

The following statement creates a Sample and converts it into a bidimensional Numpy array.

[23]:

import numpy as np
sample = ot.Sample (5, 3)
array = np.array (sample)
array

[23]:

array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])

[24]:

type(array)

[24]:

numpy.ndarray


Conversely, the following script creates a Numpy array, then converts it into a Sample.

[25]:

array = 3.14 * np.ones((5 , 3))
sample = ot.Sample ( array )
sample

[25]:

 v0 v1 v2 0 3.14 3.14 3.14 1 3.14 3.14 3.14 2 3.14 3.14 3.14 3 3.14 3.14 3.14 4 3.14 3.14 3.14
[26]:

sample.getSize()

[26]:

5

[27]:

sample.getDimension()

[27]:

3


There is an ambiguous situation: a Sample based on several scalar values.

For example, is a Sample based on 5 values:

• a Sample with size 5 in 1 dimension or

• a Sample with size 1 in 5 dimensions?

In order to solve the case, we can use the second input argument of the Sample constructor, which specifies the dimension.

The following statement creates an array containing 5 values from 0 to 1.

[28]:

u = np.linspace (0, 1, 5)
u

[28]:

array([0.  , 0.25, 0.5 , 0.75, 1.  ])


Choice A: we create a Sample with size 5 in 1 dimension.

[29]:

sample = ot.Sample (u, 1)
sample

[29]:

 v0 0 0.0 1 0.25 2 0.5 3 0.75 4 1.0

Choice B: we create a Sample with size 1 in 5 dimensions.

[30]:

sample = ot.Sample (u, 5)
sample

[30]:

 v0 v1 v2 v3 v4 0 0.0 0.25 0.5 0.75 1.0

If we do not set the optional size parameter, the library cannot solve the case and an exception is generated:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-48-8c4ed687c6a9> in <module>()
1 # Generates an expected exception
----> 2 sample = ot.Sample (u)
[...]
TypeError: InvalidArgumentException : Invalid array dimension: 1

[31]:

# Generates an expected exception
# sample = ot.Sample (u)