Database Reference
In-Depth Information
# Multiplies each cell with the corresponding
# value in another cell
> print sample_array * sample_array
[[ 9. 16. 25.]
[ 36. 49. 64.]]
These examples just scratch the surface of what is possible with NumPy arrays,
including the ability to concatenate, copy, slice, and reshape data.
SciPy: Scientific Computing for Python
It's easy to see why the multidimensional array data type provided by NumPy can be
so useful for scientific analysis. NumPy's use of in-memory arrays makes calculations
and data manipulation with Python very fast. Fortunately, many of the most com-
mon computational algorithms have been already created and packaged into a related
Python module called SciPy. NumPy handles the underlying data structures needed
for scientific computing, whereas SciPy provides functions for regularly used algo-
rithms, constants, and graphical tools used in mathematics and science applications.
SciPy's huge number of available features are organized into various packages. The
statistics package provides dozens of probability distributions and statistical tests. For
the visual and audio analysts, there's an entire package devoted to Fast Fourier Trans-
formation algorithms. There's even a package full of constants, featuring the greatest
hits of science rocks stars like Avogadro, Planck, Boltzmann, and Faraday.
Listing 12.2 demonstrates a few simple use cases for SciPy. A common use case in
the business world is using clustering algorithms to attempt to bucket customers into
broad groups. SciPy provides several useful cluster algorithms, including the popular
k-means method (also illustrated in Listing 12.2).
Listing 12.2 Using SciPy
> import scipy.constants
# Value of PI
> print scipy.constants.pi
3.141592653589793
# Run a K-means clustering operation on a NumPy array
import numpy
from scipy.cluster.vq import kmeans2
my_values = numpy.array([[4.0,5.3],
[4.1,9.1],
[2.4,7.4],
[2.1,3.5]])
clusters = 2
centroids, index_of_closest_centroid = kmeans2(my_values,clusters)
 
 
Search WWH ::




Custom Search