Convert other data into numpy arrays e.g. asarray: Difference between revisions
From Master of Neuroscience Wiki
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(..." |
No edit summary |
||
| Line 1: | Line 1: | ||
Questions to [mailto:davrot@uni-bremen.de David Rotermund] | Questions to [mailto:davrot@uni-bremen.de David Rotermund] | ||
Latest revision as of 16:35, 17 October 2025
Questions to David Rotermund
numpy.asarray
numpy.asarray(a, dtype=None, order=None, *, like=None)
The importance of asarray:
Convert the input to an array.
import numpy as np
a_list = [[1, 2], [3, 4]]
a_np = np.asarray(a_list)
w = np.asarray(1)
print(w.sum()) # -> 1
print(a_np.sum()) # -> 10
print(a_np > 2)
print(1.sum()) # SyntaxError: invalid decimal literal
print(a_list.sum()) # AttributeError: 'list' object has no attribute 'sum'
print(a_list > 2) # TypeError: '>' not supported between instances of 'list' and 'int'
Output:
[[False False]
[ True True]]
numpy.fromiter
This is an optional topic!
numpy.fromiter(iter, dtype, count=-1, *, like=None)
Create a new 1-dimensional array from an iterable object.
numpy.fromfunction
This is an optional topic!
numpy.fromfunction(function, shape, *, dtype=<class 'float'>, like=None, **kwargs)[source]
Construct an array by executing a function over each coordinate. The resulting array therefore has a value fn(x, y, z) at coordinate (x, y, z).
numpy.array
This is an optional topic!
Don’t confuse asarray with array. array can be used to put a numpy structure around data. asarray converts the data into a numpy array. (As far as I understand…). Thus normally you don’t need to touch array.
numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
Create an array.