or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

array-creation.mdarray-statistics.mddata-types.mdfft.mdindex.mdinput-output.mdlinear-algebra.mdmasked-arrays.mdmathematical-functions.mdpolynomial.mdrandom-generation.mdsearching-sorting.md

index.mddocs/

0

# NumPy

1

2

The fundamental package for scientific computing with Python. NumPy provides a powerful N-dimensional array object, sophisticated broadcasting functions, tools for integrating C/C++ and Fortran code, and linear algebra, Fourier transform, and random number capabilities. It serves as the foundation for the entire Python scientific computing ecosystem.

3

4

## Package Information

5

6

- **Package Name**: numpy

7

- **Language**: Python

8

- **Installation**: `pip install numpy`

9

10

## Core Imports

11

12

```python

13

import numpy as np

14

```

15

16

For specific submodules:

17

18

```python

19

import numpy.linalg as la

20

import numpy.random as rng

21

import numpy.fft as fft

22

```

23

24

## Basic Usage

25

26

```python

27

import numpy as np

28

29

# Create arrays

30

arr = np.array([1, 2, 3, 4, 5])

31

matrix = np.array([[1, 2], [3, 4]])

32

33

# Array creation

34

zeros = np.zeros((3, 4))

35

ones = np.ones((2, 3))

36

range_arr = np.arange(0, 10, 2)

37

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

38

39

# Mathematical operations

40

result = np.sqrt(arr)

41

sum_result = np.sum(matrix, axis=0)

42

mean_result = np.mean(arr)

43

44

# Array manipulation

45

reshaped = matrix.reshape(4, 1)

46

transposed = matrix.T

47

```

48

49

## Architecture

50

51

NumPy's architecture provides the foundation for scientific computing in Python:

52

53

- **ndarray**: Core N-dimensional array object with optimized C/Fortran implementations

54

- **dtype**: Flexible type system supporting numeric, string, and structured data

55

- **ufuncs**: Universal functions enabling vectorized operations with broadcasting

56

- **Submodules**: Specialized functionality organized into logical domains (linalg, random, fft, polynomial)

57

58

This design enables high-performance numerical computing while maintaining Python's ease of use, serving as the base layer for libraries like pandas, scikit-learn, matplotlib, and the entire scientific Python ecosystem.

59

60

## Capabilities

61

62

### Array Creation and Manipulation

63

64

Core functionality for creating, reshaping, joining, and manipulating N-dimensional arrays. Includes array creation functions, shape manipulation, joining/splitting operations, and element access patterns.

65

66

```python { .api }

67

def array(object, dtype=None, **kwargs): ...

68

def zeros(shape, dtype=float, **kwargs): ...

69

def ones(shape, dtype=None, **kwargs): ...

70

def empty(shape, dtype=float, **kwargs): ...

71

def arange(start, stop=None, step=1, dtype=None): ...

72

def linspace(start, stop, num=50, **kwargs): ...

73

def reshape(a, newshape, order='C'): ...

74

def concatenate(arrays, axis=0, **kwargs): ...

75

```

76

77

[Array Creation and Manipulation](./array-creation.md)

78

79

### Mathematical Functions

80

81

Universal functions (ufuncs) providing element-wise mathematical operations including arithmetic, trigonometric, exponential, logarithmic, and bitwise operations with automatic broadcasting.

82

83

```python { .api }

84

def add(x1, x2, **kwargs): ...

85

def multiply(x1, x2, **kwargs): ...

86

def sin(x, **kwargs): ...

87

def cos(x, **kwargs): ...

88

def exp(x, **kwargs): ...

89

def log(x, **kwargs): ...

90

def sqrt(x, **kwargs): ...

91

```

92

93

[Mathematical Functions](./mathematical-functions.md)

94

95

### Array Statistics and Aggregations

96

97

Statistical and reduction operations for analyzing array data, including basic statistics, cumulative operations, and NaN-aware versions of statistical functions.

98

99

```python { .api }

100

def sum(a, axis=None, **kwargs): ...

101

def mean(a, axis=None, **kwargs): ...

102

def std(a, axis=None, **kwargs): ...

103

def min(a, axis=None, **kwargs): ...

104

def max(a, axis=None, **kwargs): ...

105

def median(a, axis=None, **kwargs): ...

106

def percentile(a, q, axis=None, **kwargs): ...

107

```

108

109

[Array Statistics](./array-statistics.md)

110

111

### Linear Algebra Operations

112

113

Core linear algebra functionality including matrix products, decompositions, eigenvalue problems, and solving linear systems through the numpy.linalg module.

114

115

```python { .api }

116

def dot(a, b, out=None): ...

117

def matmul(x1, x2, **kwargs): ...

118

def linalg.inv(a): ...

119

def linalg.solve(a, b): ...

120

def linalg.eig(a): ...

121

def linalg.svd(a, **kwargs): ...

122

```

123

124

[Linear Algebra](./linear-algebra.md)

125

126

### Array Searching and Sorting

127

128

Functions for finding, sorting, and organizing array elements including search operations, sorting algorithms, and set operations for array analysis.

129

130

```python { .api }

131

def where(condition, x=None, y=None): ...

132

def sort(a, axis=-1, **kwargs): ...

133

def argsort(a, axis=-1, **kwargs): ...

134

def unique(ar, **kwargs): ...

135

def searchsorted(a, v, **kwargs): ...

136

```

137

138

[Searching and Sorting](./searching-sorting.md)

139

140

### Random Number Generation

141

142

Comprehensive random number generation capabilities through numpy.random, including various probability distributions, random sampling, and BitGenerator infrastructure.

143

144

```python { .api }

145

def random.random(size=None): ...

146

def random.randint(low, high=None, size=None): ...

147

def random.normal(loc=0.0, scale=1.0, size=None): ...

148

def random.choice(a, size=None, **kwargs): ...

149

def random.default_rng(seed=None): ...

150

```

151

152

[Random Number Generation](./random-generation.md)

153

154

### Fast Fourier Transform

155

156

Discrete Fourier Transform operations through numpy.fft for signal processing and frequency domain analysis, including 1D, 2D, and N-D transforms.

157

158

```python { .api }

159

def fft.fft(a, n=None, axis=-1, **kwargs): ...

160

def fft.ifft(a, n=None, axis=-1, **kwargs): ...

161

def fft.fft2(a, s=None, axes=(-2, -1), **kwargs): ...

162

def fft.rfft(a, n=None, axis=-1, **kwargs): ...

163

```

164

165

[Fast Fourier Transform](./fft.md)

166

167

### Input/Output Operations

168

169

File I/O operations for saving and loading array data in various formats, including binary and text formats with support for compressed files.

170

171

```python { .api }

172

def save(file, arr, **kwargs): ...

173

def load(file, **kwargs): ...

174

def loadtxt(fname, **kwargs): ...

175

def savetxt(fname, X, **kwargs): ...

176

```

177

178

[Input/Output](./input-output.md)

179

180

### Data Types and Type Operations

181

182

NumPy's flexible data type system including scalar types, structured arrays, and type conversion operations for handling diverse data formats.

183

184

```python { .api }

185

class dtype: ...

186

def astype(dtype, **kwargs): ...

187

def can_cast(from_, to, casting='safe'): ...

188

class finfo: ...

189

class iinfo: ...

190

```

191

192

[Data Types](./data-types.md)

193

194

### Polynomial Operations

195

196

Polynomial classes and functions for working with polynomials of different mathematical bases including power series, Chebyshev, Legendre, Laguerre, and Hermite polynomials.

197

198

```python { .api }

199

class Polynomial: ...

200

class Chebyshev: ...

201

class Legendre: ...

202

def poly(seq_of_zeros): ...

203

def polyval(p, x): ...

204

def polyfit(x, y, deg): ...

205

```

206

207

[Polynomial Operations](./polynomial.md)

208

209

### Masked Array Operations

210

211

Array operations that handle missing or invalid data through masking, providing robust statistical computations on incomplete datasets.

212

213

```python { .api }

214

class MaskedArray: ...

215

def masked_array(data, mask=False, **kwargs): ...

216

def masked_where(condition, a): ...

217

def masked_invalid(a): ...

218

```

219

220

[Masked Arrays](./masked-arrays.md)

221

222

## Constants and Configuration

223

224

```python { .api }

225

# Mathematical constants

226

pi = 3.141592653589793

227

e = 2.718281828459045

228

euler_gamma = 0.5772156649015329

229

inf = float('inf')

230

nan = float('nan')

231

NINF = float('-inf')

232

PINF = float('inf')

233

NZERO = -0.0

234

PZERO = 0.0

235

newaxis = None

236

237

# Version information

238

__version__: str # NumPy version string

239

__array_api_version__: str # Array API standard version

240

241

# Configuration

242

def show_config():

243

"""Display NumPy build configuration information."""

244

```

245

246

## Types

247

248

```python { .api }

249

class ndarray:

250

"""N-dimensional array object."""

251

def __init__(self, shape, dtype=float, **kwargs): ...

252

def reshape(self, newshape, order='C'): ...

253

def astype(self, dtype, **kwargs): ...

254

def sum(self, axis=None, **kwargs): ...

255

def mean(self, axis=None, **kwargs): ...

256

def transpose(self, axes=None): ...

257

@property

258

def shape: tuple

259

@property

260

def dtype: dtype

261

@property

262

def size: int

263

@property

264

def ndim: int

265

@property

266

def T: ndarray

267

268

class dtype:

269

"""Data type object describing array element type."""

270

def __init__(self, obj, **kwargs): ...

271

@property

272

def name: str

273

@property

274

def kind: str

275

@property

276

def itemsize: int

277

278

class ufunc:

279

"""Universal function object."""

280

def __call__(self, *args, **kwargs): ...

281

def reduce(self, a, axis=0, **kwargs): ...

282

def accumulate(self, a, axis=0, **kwargs): ...

283

284

# Scalar types

285

class generic: ... # Base class for all scalar types

286

class number(generic): ... # Base class for all number types

287

class integer(number): ... # Base class for all integer types

288

class signedinteger(integer): ... # Base class for signed integers

289

class unsignedinteger(integer): ... # Base class for unsigned integers

290

class inexact(number): ... # Base class for inexact types

291

class floating(inexact): ... # Base class for floating types

292

class complexfloating(inexact): ... # Base class for complex types

293

class flexible(generic): ... # Base class for flexible types

294

class character(flexible): ... # Base class for character types

295

296

# Integer scalar types

297

class int8(signedinteger): ...

298

class int16(signedinteger): ...

299

class int32(signedinteger): ...

300

class int64(signedinteger): ...

301

class uint8(unsignedinteger): ...

302

class uint16(unsignedinteger): ...

303

class uint32(unsignedinteger): ...

304

class uint64(unsignedinteger): ...

305

306

# Platform-dependent integer types

307

class int_(signedinteger): ... # Platform integer (usually int64)

308

class intc(signedinteger): ... # C int type

309

class intp(signedinteger): ... # Pointer-sized integer

310

class uint(unsignedinteger): ... # Platform unsigned integer

311

class uintc(unsignedinteger): ... # C unsigned int

312

class uintp(unsignedinteger): ... # Pointer-sized unsigned integer

313

314

# Floating point types

315

class float16(floating): ... # Half precision

316

class float32(floating): ... # Single precision

317

class float64(floating): ... # Double precision

318

class longdouble(floating): ... # Extended precision

319

320

# Complex types

321

class complex64(complexfloating): ... # Single precision complex

322

class complex128(complexfloating): ... # Double precision complex

323

class clongdouble(complexfloating): ... # Extended precision complex

324

325

# Other types

326

class bool_(generic): ... # Boolean type

327

class bytes_(character): ... # Bytes type

328

class str_(character): ... # Unicode string type

329

class void(flexible): ... # Void type for structured arrays

330

class object_(generic): ... # Python object type

331

332

# Date/time types

333

class datetime64(generic): ... # Date and time

334

class timedelta64(generic): ... # Time differences

335

```