Questions tagged [numpy]

NumPy is one of the many modules in Python that adds support of large multidimensional arrays and matrixes, along with a large library of high-level mathematical functions for operations with these arrays. --- To install the numpy module enter this command line: ~~~ python -m pip install numpy ~~~ It may require the wheel and pip package.

About NumPy

From the NumPy homepage:

NumPy is the fundamental package for scientific computing with . It contains among other things:

  • a powerful N-dimensional array object
  • built-in universal functions (ufuncs)
  • sophisticated (broadcasting) operation
  • tools for integrating C/C++ and Fortran code
  • useful linear algebra, Fourier transform, and random number capabilities

Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.

NumPy provides reliable and efficient methods of data storage, manipulation, and analysis as it also integrates easily with other methods of data manipulation, notably Pandas and scikit-learn.

NumPy is released under the BSD license, enabling reuse with few restrictions.

Resources

Official Resources

Books

112481 questions
28
votes
3 answers

How to split an array according to a condition in numpy?

For example, I have a ndarray that is: a = np.array([1, 3, 5, 7, 2, 4, 6, 8]) Now I want to split a into two parts, one is all numbers <5 and the other is all >=5: [array([1,3,2,4]), array([5,7,6,8])] Certainly I can traverse a and create two new…
Clippit
  • 856
  • 1
  • 9
  • 20
28
votes
2 answers

A question about "ModuleNotFoundError: No module named 'numpy.typing'"

I am trying to import ArrayLike doing from numpy.typing import ArrayLike, and I get the error mentioned in the title: ModuleNotFoundError: No module named 'numpy.typing' I know I could simply write import numpy.typing as npt as the documentation…
condosz
  • 474
  • 1
  • 5
  • 10
28
votes
2 answers

python+numpy: why does numpy.log throw an attribute error if its operand is too big?

Running np.log(math.factorial(21)) throws an AttributeError: log. Why is that? I could imagine a ValueError, or some sort of UseYourHighSchoolMathsError, but why the attribute error?
Mike Dewar
  • 10,945
  • 14
  • 49
  • 65
28
votes
3 answers

Python: Making numpy default to float32

Is there any clean way of setting numpy to use float32 values instead of float64 globally?
Bolster
  • 7,460
  • 13
  • 61
  • 96
28
votes
2 answers

How to iterate over columns of a matrix?

In python if a define: a = arange(9).reshape(3,3) as a 3x3 matrix and iterate: for i in a: It'll iterate over the matrix's rows. Is there any way to iterate over columns?
Rodrigo Souza
  • 7,162
  • 12
  • 41
  • 72
28
votes
3 answers

Numpy import error Python3 on Raspberry Pi?

When I try to import numpy in Python3 i get an error. I installed it via pip3 and it got installed succesfully. sudo pip3 install numpy Here is the error message when i try to import numpy: Python 3.5.3 (default, Sep 27 2018, 17:25:39) [GCC 6.3.0…
AVX-42
  • 755
  • 2
  • 13
  • 21
28
votes
3 answers

FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]` instead of `arr[seq]`

I would like not to use the non-tuple sequence for multidimensional indexing so that the script will support future release of Python when this changes. Below is the code that i am using for plotting the graph: data =…
yajant b
  • 396
  • 1
  • 4
  • 12
28
votes
3 answers

Python: Is there an inverse for ndarray.flatten('F')?

For example: from numpy import * x = array([[1,2], [3, 4], [5, 6]]) print x.flatten('F') >>>[1 3 5 2 4 6] Is it possible to get [[1,2], [3, 4], [5, 6]] from [1 3 5 2 4 6]?
chimichanga
  • 303
  • 1
  • 3
  • 6
28
votes
6 answers

How do I pass large numpy arrays between python subprocesses without saving to disk?

Is there a good way to pass a large chunk of data between two python subprocesses without using the disk? Here's a cartoon example of what I'm hoping to accomplish: import sys, subprocess, numpy cmdString = """ import sys, numpy done = False while…
Andrew
  • 2,842
  • 5
  • 31
  • 49
28
votes
4 answers

Feeding .npy (numpy files) into tensorflow data pipeline

Tensorflow seems to lack a reader for ".npy" files. How can I read my data files into the new tensorflow.data.Dataset pipline? My data doesn't fit in memory. Each object is saved in a separate ".npy" file. each file contains 2 different ndarrays as…
Sluggish Crow
  • 383
  • 1
  • 3
  • 5
28
votes
4 answers

Applying a function along a numpy array

I've the following numpy ndarray. [ -0.54761371 17.04850603 4.86054302] I want to apply this function to all elements of the array def sigmoid(x): return 1 / (1 + math.exp(-x)) probabilities = np.apply_along_axis(sigmoid, -1, scores) This is…
Melissa Stewart
  • 3,483
  • 11
  • 49
  • 88
28
votes
7 answers

No speed gains from Cython

I am trying to define a function that contains an inner loop for simulating an integral. The problem is speed. Evaluating the function once can take up to 30 seconds on my machine. Since my ultimate goal is to minimize this function, some extra…
Randall J
  • 471
  • 2
  • 6
  • 9
28
votes
5 answers

Error "TypeError: type numpy.ndarray doesn't define __round__ method"

import numpy ...... # Prediction predictions = model.predict(X_test) # round predictions rounded = [round(x) for x in predictions] print(rounded) "predictions" is a list of decimals between [0,1] with sigmoid output. Why does it always report…
user697911
  • 10,043
  • 25
  • 95
  • 169
28
votes
2 answers

What is the difference between numpy var() and statistics variance() in python?

I was trying one Dataquest exercise and I figured out that the variance I am getting is different for the two packages. e.g for [1,2,3,4] from statistics import variance import numpy as…
Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106
28
votes
10 answers

numpy random choice in Tensorflow

Is there an equivalent function to numpy random choice in Tensorflow. In numpy we can get an item randomly from the given list with its weights. np.random.choice([1,2,3,5], 1, p=[0.1, 0, 0.3, 0.6, 0]) This code will select an item from the given…
seleucia
  • 1,046
  • 4
  • 17
  • 30
1 2 3
99
100