User contributions for Davrot
From Master of Neuroscience Wiki
17 October 2025
- 13:0113:01, 17 October 2025 diff hist +7 N File:23897 0.png No edit summary current
- 12:5812:58, 17 October 2025 diff hist +5,861 N Statistics Created page with "== The goal == There are other (more extensive) statistics packages like * [https://docs.scipy.org/doc/scipy/reference/stats.html scipy.stats] * [https://pingouin-stats.org/build/html/index.html pingouin] * [https://www.statsmodels.org/stable/index.html statsmodels] Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.fisher_exact.html#scipy.stats.fisher_exact Fisher Exact Test] == The [ht..." Tag: Visual edit
- 12:5812:58, 17 October 2025 diff hist +9,984 N Random numbers the non-legacy way Created page with "== Goal == If you don’t see something like '''np.random.default_rng()''' in your code then you are probably using the old [https://numpy.org/doc/stable/reference/random/legacy.html#legacy-random-generation Legacy Random Generation]. '''Don’t use the [https://numpy.org/doc/stable/reference/random/legacy.html legacy] methods''' for new source code!!! '''numpy.random.random() == old == bad == don’t use''' Do it like this:<syntaxhighlight lang="python">import num..." Tag: Visual edit
- 12:5712:57, 17 October 2025 diff hist +7,472 N Linear algebra Created page with "== The goal == Overview over the linear algebra functions of Numpy. Questions to [mailto:davrot@uni-bremen.de David Rotermund] '''There more functions in the [https://docs.scipy.org/doc/scipy/reference/linalg.html scipy linalg package]!!!''' == The @ operator == @ => Matrix product * => outer product A[i] * B[i] The @ operator is preferable to other methods when computing the matrix product between 2d arrays. The numpy.matmul function implements the @ oper..." Tag: Visual edit
- 12:5612:56, 17 October 2025 diff hist +17,796 N Math functions Created page with "== The goal == Matrices alone are useless. We need some math functions to act upon them. Questions to [mailto:davrot@uni-bremen.de David Rotermund] '''I will focus on the important ones. Those will get a link.''' == [https://numpy.org/doc/stable/reference/routines.math.html#trigonometric-functions Trigonometric functions] == {| class="wikitable" |[https://numpy.org/doc/stable/reference/generated/numpy.sin.html#numpy.sin sin](x, /[, out, where, casting, order, …]) |T..." Tag: Visual edit
- 12:5512:55, 17 October 2025 diff hist +656 N Constants Created page with " == The goal == Numpy provides plenty of constants. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/constants.html The most important ones] == {| class="wikitable" |numpy.inf |IEEE 754 floating point representation of (positive) infinity. |- |numpy.nan |IEEE 754 floating point representation of Not a Number (NaN). |- |numpy.e |Euler’s constant, base of natural logarithms, Napier’s constant. e = 2.718281828459..." Tag: Visual edit
- 12:5412:54, 17 October 2025 diff hist −22 Available dtypes No edit summary Tag: Visual edit
- 12:5412:54, 17 October 2025 diff hist +3,833 N Available dtypes Created page with "== The goal == A numpy matrix can have differnt dtype or – in other words – differnt types of numbers with different precisions. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/arrays.scalars.html#sized-aliases Signed integer types] == '''Please use numpy.int8, numpy.int16, numpy.int32, and numpy.int64 !!!''' * ''numpy.int8'': ** 8-bit signed integer (-128 to 127). Compatible with C char. ** ''numpy.byte'..." Tag: Visual edit
- 12:5312:53, 17 October 2025 diff hist +1,381 N Ravel and UnRavel Created page with " == Top == Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/generated/numpy.ravel_multi_index.html numpy.ravel_multi_index] == <syntaxhighlight lang="python">numpy.ravel_multi_index(multi_index, dims, mode='raise', order='C')</syntaxhighlight><blockquote>Converts a tuple of index arrays into an array of flat indices, applying boundary modes to the multi-index.</blockquote><syntaxhighlight lang="python">import numpy as..." Tag: Visual edit
- 12:4612:46, 17 October 2025 diff hist +6,796 N Advanced Indexing Created page with "== The goal == Beside slicing there is something called advanced indexing Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/user/basics.indexing.html#boolean-array-indexing Boolean Array] == We can use Boolean arrays for more complicate indexing:<syntaxhighlight lang="python">import numpy as np a = np.arange(1,10).reshape(3,3) b = np.zeros_like(a) b[a.sum(axis=1) > 6, :] = 1 print(a) print() print(b)</syntaxhighlight>Outp..." Tag: Visual edit
- 12:4512:45, 17 October 2025 diff hist +8,634 N Boolean matricies and logic functions Created page with "== The goal == Questions to [mailto:davrot@uni-bremen.de David Rotermund] == Boolean matrices == There are different ways to get a Boolean matrix. For example the result of a '''np.isfinite()''' (checks if the values in a matrix are infite values) is a Boolean matrix.<syntaxhighlight lang="python">import numpy as np a = 1.0 / np.arange(0, 6).reshape((2, 3)) print(a) print() print(np.isfinite(a))</syntaxhighlight>Output:<syntaxhighlight lang="python"> inf 1...." Tag: Visual edit
- 12:4412:44, 17 October 2025 diff hist +6,398 N Extending an existing matrix: tile, repeat, pad Created page with "== The goal == Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/generated/numpy.tile.html numpy.tile] == <syntaxhighlight lang="python">numpy.tile(A, reps)</syntaxhighlight><blockquote>Construct an array by repeating A the number of times given by reps. If reps has length d, the result will have dimension of max(d, A.ndim). If A.ndim < d, A is promoted to be d-dimensional by prepending new axes. So a shape (3,)..." Tag: Visual edit
- 12:4312:43, 17 October 2025 diff hist +1,729 N Piecewise Created page with "== Top == Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/generated/numpy.piecewise.html numpy.piecewise] == <syntaxhighlight lang="python">numpy.piecewise(x, condlist, funclist, *args, **kw)</syntaxhighlight><blockquote>Evaluate a piecewise-defined function. Given a set of conditions and corresponding functions, evaluate each function on the input data wherever its condition is true.</blockquote><syntaxhighlight la..." Tag: Visual edit
- 12:4312:43, 17 October 2025 diff hist +3,791 N Where Created page with "== The goal == '''where''' allows to modifiy or combine matricies based on a given condition. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/generated/numpy.where.html numpy.where] == <syntaxhighlight lang="python">numpy.where(condition, [x, y, ]/)</syntaxhighlight><blockquote>Return elements chosen from x or y depending on condition.</blockquote><blockquote>'''condition''': array_like, bool Where True, yield x, ot..." Tag: Visual edit
- 12:4212:42, 17 October 2025 diff hist +1,747 N Unique Created page with "== The goal == Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/generated/numpy.unique.html numpy.unique] == <syntaxhighlight lang="python">numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None, *, equal_nan=True)</syntaxhighlight><blockquote>Find the unique elements of an array. Returns the sorted unique elements of an array. There are three optional outputs in addition to the uni..." Tag: Visual edit
- 12:4112:41, 17 October 2025 diff hist +8,701 N Merging matrices Created page with "== Top == Questions to [mailto:davrot@uni-bremen.de David Rotermund] Choose vs select: * Choose: One Matrix with integer values 0,…N-1 * Select: N binary matrices == [https://numpy.org/doc/stable/reference/generated/numpy.choose.html#numpy-choose numpy.choose] == <syntaxhighlight lang="python">numpy.choose(a, choices, out=None, mode='raise')</syntaxhighlight><blockquote>Construct an array from an index array and a list of arrays to choose from. First of all,..." Tag: Visual edit
- 12:3912:39, 17 October 2025 diff hist +5,621 N Concatenate Matrices and arrays Created page with "== The goal == Questions to [mailto:davrot@uni-bremen.de David Rotermund] == numpy.concatenate == <syntaxhighlight lang="python">numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")</syntaxhighlight><blockquote>Join a sequence of arrays along an existing axis.</blockquote><syntaxhighlight lang="python">import numpy as np a = np.arange(0, 5) print(a) # -> [0 1 2 3 4] print(a.shape) # -> (5,) b = np.arange(0, 8) print(b) # -> [0 1 2 3 4..." Tag: Visual edit
- 12:3712:37, 17 October 2025 diff hist +6,990 N Slices and Views Created page with "== The goal == Sometimes we want to use or see only a part of the matrix. This can be done via slices and views Questions to [mailto:davrot@uni-bremen.de David Rotermund] == 1-d slices == We assume N as the number of elements and 1d: * A valid index starts at '''0''' and runs until N-1 * [start:stop:step] start = 1, stop=N, step=1 -> this results in the sequence 1,2,3,…,(N-1) * [start:stop:1] can be shortened to [start:stop] * [0:stop] can be shortene..." Tag: Visual edit
- 12:3612:36, 17 October 2025 diff hist +4,623 N Reshape and flatten Created page with "== The goal == Sometimes you have to change the shape of a matrix. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/generated/numpy.reshape.html Reshape] == <syntaxhighlight lang="python">numpy.reshape(a, newshape, order='C')</syntaxhighlight><blockquote>Gives a new shape to an array without changing its data.</blockquote><blockquote>'''a''': array_like Array to be reshaped.</blockquote><blockquote>'''newshape''': in..." Tag: Visual edit
- 12:3612:36, 17 October 2025 diff hist +12 N File:98khj image0.png No edit summary current
- 12:3412:34, 17 October 2025 diff hist +5,764 N Save and load Created page with "== The goal == Let’s [https://numpy.org/doc/stable/reference/routines.io.html save and load data under numpy]. This can be more complicated than expected. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/generated/numpy.save.html np.save] and [https://numpy.org/doc/stable/reference/generated/numpy.load.html np.load] == A normal np.save and np.load cycle may look like this:<syntaxhighlight lang="python">import numpy..." Tag: Visual edit
- 12:3312:33, 17 October 2025 diff hist +5,177 N New matrix Created page with "== The goal == Making a new matrix… Questions to [mailto:davrot@uni-bremen.de David Rotermund] Using '''import numpy as np''' is the standard. == Simple example – new [https://numpy.org/doc/stable/reference/generated/numpy.zeros.html np.zeros()] == Define the size of your new matrix with a tuple, e.g.<syntaxhighlight lang="python">M = numpy.zeros((DIM_0, DIM_1, DIM_2, …))</syntaxhighlight> === 1d === <syntaxhighlight lang="python">import numpy as np M = np...." Tag: Visual edit
- 12:2912:29, 17 October 2025 diff hist +2,160 N Convert other data into numpy arrays e.g. asarray Created page with "== The goal == Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/generated/numpy.asarray.html numpy.asarray] == <syntaxhighlight lang="python">numpy.asarray(a, dtype=None, order=None, *, like=None)</syntaxhighlight>The importance of '''asarray''':<blockquote>Convert the input to an array.</blockquote><syntaxhighlight lang="python">import numpy as np a_list = [[1, 2], [3, 4]] a_np = np.asarray(a_list) w = np.asarray(..." Tag: Visual edit
16 October 2025
- 17:5017:50, 16 October 2025 diff hist +5,191 N Making a matrix from numerical ranges Created page with "== The goal == Making a new matrix… Questions to [mailto:davrot@uni-bremen.de David Rotermund] Using '''import numpy as np''' is the standard. == Simple example – new [https://numpy.org/doc/stable/reference/generated/numpy.zeros.html np.zeros()] == Define the size of your new matrix with a tuple, e.g.<syntaxhighlight lang="python">M = numpy.zeros((DIM_0, DIM_1, DIM_2, …))</syntaxhighlight> === 1d === <syntaxhighlight lang="python">import numpy as np M = np...." Tag: Visual edit
- 17:4917:49, 16 October 2025 diff hist 0 N File:Broadcasting 4.png No edit summary current
- 17:4817:48, 16 October 2025 diff hist 0 N File:Broadcasting 3.png No edit summary current
- 17:4817:48, 16 October 2025 diff hist 0 N File:Broadcasting 2.png No edit summary current
- 17:4817:48, 16 October 2025 diff hist 0 N File:Broadcasting 1.png No edit summary current
- 17:4817:48, 16 October 2025 diff hist +7,002 N Broadcasting: Automatic adaption of dimensions Created page with "== The goal == Broadcasting: Automatic adaption of dimensions Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/devdocs/user/basics.broadcasting.html General broadcasting rules] == <blockquote>When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing (i.e. '''''rightmost''''') dimension and works its way left. '''Two dimensions are compatible when''' * '''they are equal''', or * '''one of them is..." Tag: Visual edit
- 16:0616:06, 16 October 2025 diff hist +6,155 N Dimensions and shape Created page with "== The goal == Matrices have dimensions. But how to add and remove extra dimensions (i.e. dimensions with length 1)? Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html numpy.ndarray.shape] == <syntaxhighlight lang="python">ndarray.shape</syntaxhighlight><blockquote>Tuple of array dimensions. The shape property is usually used to get the current shape of an array, but may also be used..." Tag: Visual edit
- 16:0516:05, 16 October 2025 diff hist +987 N Set printoptions Created page with " == Top == Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://numpy.org/doc/stable/reference/generated/numpy.set_printoptions.html numpy.set_printoptions] == <syntaxhighlight lang="python">numpy.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, formatter=None, sign=None, floatmode=None, *, legacy=None)[source]</syntaxhighlight><blockquote>Set printing options.</blockquote><syntax..." Tag: Visual edit
- 16:0416:04, 16 October 2025 diff hist +35,310 N The N-dimensional array (ndarray) Created page with " == The goal == Class has a very important job as a core container type in Python. It is really hard to find a good overview how to use them in a good practice manner. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == Chaining of (ndarray) methods == <syntaxhighlight lang="python">import numpy as np a = np.ones((3, 3)) b = a.mean(axis=1).max() print(b) # -> 1.0</syntaxhighlight> == [https://numpy.org/doc/stable/reference/generated/numpy.ndarray.fill.html n..." Tag: Visual edit
- 15:5915:59, 16 October 2025 diff hist +1,934 Python Tutorial No edit summary Tag: Visual edit
- 15:4715:47, 16 October 2025 diff hist +4,816 N Collection of distinct hashable objects -- set and frozenset Created page with " == Top == Questions to [mailto:davrot@uni-bremen.de David Rotermund] '''This is an optional topic!''' = [https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset Set Types — set, frozenset] = <blockquote>A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference...." Tag: Visual edit
- 15:3215:32, 16 October 2025 diff hist +4,940 N Python Scopes and Namespaces Created page with " == The goal == Questions to [mailto:davrot@uni-bremen.de David Rotermund] Shortened quotes from https://docs.python.org/3/tutorial/classes.html == [https://docs.python.org/3/tutorial/classes.html#scopes-and-namespaces-example Scopes and Namespaces Example] == <blockquote>This is an example demonstrating how to reference the different scopes and namespaces, and how [https://docs.python.org/3/reference/simple_stmts.html#global global] and [https://docs.python.org/3/refe..." Tag: Visual edit
- 15:2515:25, 16 October 2025 diff hist +7,371 N Logging Created page with " == The goal == We want to log either or onto the screen or file. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://docs.python.org/3/library/logging.html#logging.getLogger logging.getLogger] == <syntaxhighlight lang="python">logging.getLogger(name=None)</syntaxhighlight><blockquote>Return a logger with the specified name or, if name is None, return a logger which is the root logger of the hierarchy. If specified, the name is typically a dot-separa..." Tag: Visual edit
- 15:2415:24, 16 October 2025 diff hist +4,579 N ProcessPoolExecutor: A fast way to implement multiprocessing Created page with " == The goal == A fast way (measured in source code length) for multi-processing a function. Is it the best way? No, but it is easily accessable. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == An example == We want to run 4 processes (number_of_cpu_processes = 4) at the same time. The first step is to get rid of most of the arguments of the function function_a. We use [https://docs.python.org/3/library/functools.html#functools.partial functools.partial]..." Tag: Visual edit
- 15:1115:11, 16 October 2025 diff hist +5,707 N Organizing parameters: dataclasses and dataconf Created page with " == The goal == In data science we have a lot of parameters in our simulations. Often the parameters are distributed over the whole source code. The combination of dataclass and dataconf allows it very easily to put the parameters in to a config file. dataconf supports many different config types. Among those are json and yaml. Questions to [mailto:davrot@uni-bremen.de David Rotermund]<syntaxhighlight lang="shell">pip install dataconf</syntaxhighlight> == Defining the..." Tag: Visual edit
- 15:1015:10, 16 October 2025 diff hist +14,734 N Dataclass Created page with " == The goal == There is a new build-in [https://docs.python.org/3/library/dataclasses.html dataclass] class which is highly interesting for data scientists. Obviously it is a class for storing your data. Who would have guessed… Questions to [mailto:davrot@uni-bremen.de David Rotermund] '''Type annotations required!!!''' This is the first construct in Python that requires type annotation. If we do this, we get this nice error!<syntaxhighlight lang="python">from dat..." Tag: Visual edit
- 15:0915:09, 16 October 2025 diff hist +22,813 N The Python Standard Library Created page with " == The goal == More than the standard instruction set… Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://docs.python.org/3/library/text.html Text Processing Services] == <blockquote>The modules described in this chapter provide a wide range of string manipulation operations and other text processing services.</blockquote> {| class="wikitable" |'''[https://docs.python.org/3/library/string.html string]''' |'''Common string operations''' |..." Tag: Visual edit
15 October 2025
- 15:2815:28, 15 October 2025 diff hist +4,074 N Pickle: save and load Python objects Created page with " == The goal == How to store Python objects in files and how to restore them. Questions to [mailto:davrot@uni-bremen.de David Rotermund] '''Warning The pickle module is not secure. Only unpickle data you trust.''' == [https://docs.python.org/3/library/pickle.html#module-pickle pickle] == <blockquote>The [https://docs.python.org/3/library/pickle.html#module-pickle pickle] module implements binary protocols for serializing and de-serializing a Python object structure...." Tag: Visual edit
- 15:2715:27, 15 October 2025 diff hist +365 Python Tutorial →Python: Special topics Tag: Visual edit
- 13:4613:46, 15 October 2025 diff hist +1,128 N Built-in Keywords Created page with " == The goal == You need to know the keywords for Python because you should not use them as variable, class, or function names. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://docs.python.org/3/library/keyword.html Keywords] == <syntaxhighlight lang="python">import keyword print(keyword.kwlist) print() print(keyword.softkwlist) # -> ['_', 'case', 'match']</syntaxhighlight>Output:<syntaxhighlight lang="python">['False', 'None', 'True', 'and', 'as..." Tag: Visual edit
- 13:4513:45, 15 October 2025 diff hist +12,024 N Built-in Functions Created page with " == The goal == Do I need to know what all these functions are doing? No, but you are shouldn’t use these function names for your own functions! However… I marked the one in bold font that you should know. Questions to [mailto:davrot@uni-bremen.de David Rotermund] {| class="wikitable" |[https://docs.python.org/3/library/functions.html#abs '''abs()'''] |Return the absolute value of a number. |- |[https://docs.python.org/3/library/functions.html#aiter aiter()] |Retur..." Tag: Visual edit
- 13:4413:44, 15 October 2025 diff hist +7,725 N Importing Modules Created page with " == The goal == Collections of functions can be organized into modules. These modules are then imported into a programm or another module. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://docs.python.org/3/tutorial/modules.html Basics] == <blockquote>A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the v..." Tag: Visual edit
- 13:3913:39, 15 October 2025 diff hist +11,207 N Exceptions (try / except) Created page with " == The goal == The “modern” way (the concept is from the 1960) to deal with errors is to use exceptions. “Normal” errors just kill your program. Exceptions allow you to react to the error (if you want to). Plus, it is standardized and you don’t need to invent your own approach, which is important if source code is exchanged. An exception is thrown (i.e. raised under Python). And the program has to catch and deal with the exception. Questions to [mailto:davro..." Tag: Visual edit
- 13:3213:32, 15 October 2025 diff hist +31,973 N Class Created page with " == The goal == Class has a very important job as a core container type in Python. It is really hard to find a good overview how to use them in a good practice manner. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == Basics == Class is the core component of object-oriented programming (OOP). A class is an [https://docs.python.org/3/reference/datamodel.html object] that can..." Tag: Visual edit
- 13:2913:29, 15 October 2025 diff hist +2,635 N Finding files in a directory: glob Created page with " == Goal == We want to deal with many files in a directory. What is an easy way to get the filename in a directory? Questions to [mailto:davrot@uni-bremen.de David Rotermund] == Creating test files == <syntaxhighlight lang="python">from pathlib import Path Path("Testfile_1.mat").touch() Path("Testfile_2.mat").touch() Path("Testfile_10.mat").touch() Path("Testfile_3.mat").touch()</syntaxhighlight> == Using glob in a for-loop == <syntaxhighlight lang="python">import gl..." Tag: Visual edit
- 13:2813:28, 15 October 2025 diff hist +2,022 N Creating order via sub-directories: os.makedirs Created page with " == The goal == How can I make sure that the directory for my output data is always there? We just create it automatically every time via [https://docs.python.org/3/library/os.html#os.makedirs os.makedirs]! Questions to [mailto:davrot@uni-bremen.de David Rotermund] == Creating a directory == <syntaxhighlight lang="python">import os some_path: str = "a_home_for_my_data" os.makedirs(some_path, exist_ok=True)</syntaxhighlight> == Creating a sub-sub-sub-directory == Firs..." Tag: Visual edit
- 13:2713:27, 15 October 2025 diff hist +1,999 N JSON and dict for parameter files Created page with " == The goal == A combination of JSON (JavaScript Object Notation) file and dictionaries allows to organize parameters in an external file. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == [https://docs.python.org/3/library/json.html json – JSON encoder and decoder] == === [https://docs.python.org/3/library/json.html#json.dump dump] === <syntaxhighlight lang="python">import json a = dict(one=1, two=2, three=3) with open("data_out.json", "w") as file:..." Tag: Visual edit