Questions tagged [scipy]

SciPy is an open source library of algorithms and mathematical tools for the Python programming language.

SciPy is an open-source library for the programming language consisting of mathematical algorithms and functions for manipulating and visualizing data, often used in science and engineering. SciPy includes algorithms and tools for tasks such as optimization, clustering, discrete Fourier transforms, linear algebra, signal processing and multi-dimensional image processing.

SciPy is closely related to NumPy and depends on many functions, including a multidimensional array that is used as the basic data structure in SciPy.

SciPy is currently distributed under the BSD license.

Latest stable version:

1.7.3 (2021-11-25)

Documentation:

https://docs.scipy.org/doc/scipy/reference/

21123 questions
47
votes
4 answers

Difference between scipy.spatial.KDTree and scipy.spatial.cKDTree

What is the difference between these two algorithms?
Benjamin
  • 11,560
  • 13
  • 70
  • 119
47
votes
3 answers

How to elementwise-multiply a scipy.sparse matrix by a broadcasted dense 1d array?

Suppose I have a 2d sparse array. In my real usecase both the number of rows and columns are much bigger (say 20000 and 50000) hence it cannot fit in memory when a dense representation is used: >>> import numpy as np >>> import scipy.sparse as…
ogrisel
  • 39,309
  • 12
  • 116
  • 125
46
votes
4 answers

Computing cross-correlation function?

In R, I am using ccf or acf to compute the pair-wise cross-correlation function so that I can find out which shift gives me the maximum value. From the looks of it, R gives me a normalized sequence of values. Is there something similar in Python's…
Legend
  • 113,822
  • 119
  • 272
  • 400
46
votes
2 answers

Experience with using h5py to do analytical work on big data in Python?

I do a lot of statistical work and use Python as my main language. Some of the data sets I work with though can take 20GB of memory, which makes operating on them using in-memory functions in numpy, scipy, and PyIMSL nearly impossible. The…
Josh Hemann
  • 940
  • 10
  • 12
46
votes
9 answers

Distance between point and a line (from two points)

I'm using Python+Numpy (can maybe also use Scipy) and have three 2D points (P1, P2, P3); I am trying to get the distance from P3 perpendicular to a line drawn between P1 and P2. Let P1=(x1,y1), P2=(x2,y2) and P3=(x3,y3) In vector notation this…
user1185675
  • 495
  • 1
  • 4
  • 4
46
votes
1 answer

What's the difference between KFold and ShuffleSplit CV?

It seems like KFold generates the same values every time the object is iterated over, while Shuffle Split generates different indices every time. Is this correct? If so, what are the uses for one over the other? cv = cross_validation.KFold(10,…
rb612
  • 5,280
  • 3
  • 30
  • 68
46
votes
2 answers

Structure of inputs to scipy minimize function

I have inherited some code that is trying to minimize a function using scipy.optimize.minimize. I am having trouble understanding some of the inputs to the fun and jac arguments. The call to minimize looks something like this: result =…
sedavidw
  • 11,116
  • 13
  • 61
  • 95
46
votes
2 answers

Use Distance Matrix in scipy.cluster.hierarchy.linkage()?

I have a distance matrix n*n M where M_ij is the distance between object_i and object_j. So as expected, it takes the following form: / 0 M_01 M_02 ... M_0n\ | M_10 0 M_12 ... M_1n | | M_20 M_21 0 ... …
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
46
votes
2 answers

kalman 2d filter in python

My input is 2d (x,y) time series of a dot moving on a screen for a tracker software. It has some noise I want to remove using Kalman filter. Does someone can point me for a python code for Kalman 2d filter? In scipy cookbook I found only a 1d…
Noam Peled
  • 4,484
  • 5
  • 43
  • 48
45
votes
4 answers

Python web hosting: Numpy, Matplotlib, Scientific Computing

I write scientific software in Numpy/Scipy/Matplotlib. Having developed applications on my home computer, I am now interested in writing simple web applications. Example: user uploads image or audio file, my program processes it using Numpy/Scipy,…
Steve Tjoa
  • 59,122
  • 18
  • 90
  • 101
45
votes
2 answers

Python p-value from t-statistic

I have some t-values and degrees of freedom and want to find the p-values from them (it's two-tailed). In the real world I would use a t-test table in the back of a Statistics textbook; how do I do the equivalent in Python? e.g. t-lookup(5, 7) =…
Andrew Latham
  • 5,982
  • 14
  • 47
  • 87
45
votes
2 answers

how to plot and annotate hierarchical clustering dendrograms in scipy/matplotlib

I'm using dendrogram from scipy to plot hierarchical clustering using matplotlib as follows: mat = array([[1, 0.5, 0.9], [0.5, 1, -0.5], [0.9, -0.5, 1]]) plt.subplot(1,2,1) plt.title("mat") dist_mat = mat linkage_matrix =…
user248237
44
votes
7 answers

Fast tensor rotation with NumPy

At the heart of an application (written in Python and using NumPy) I need to rotate a 4th order tensor. Actually, I need to rotate a lot of tensors many times and this is my bottleneck. My naive implementation (below) involving eight nested loops…
Andrew Walker
  • 2,451
  • 2
  • 18
  • 15
44
votes
4 answers

How to convert a column or row matrix to a diagonal matrix in Python?

I have a row vector A, A = [a1 a2 a3 ..... an] and I would like to create a diagonal matrix, B = diag(a1, a2, a3, ....., an) with the elements of this row vector. How can this be done in Python? UPDATE This is the code to illustrate the…
Tom Kurushingal
  • 6,086
  • 20
  • 54
  • 86
44
votes
7 answers

Calculate Matrix Rank using scipy

I'd like to calculate the mathematical rank of a matrix using scipy. The most obvious function numpy.rank calculates the dimension of an array (ie. scalars have dimension 0, vectors 1, matrices 2, etc...). I am aware that the numpy.linalg.lstsq…
Hooked
  • 84,485
  • 43
  • 192
  • 261