Tag Archives: numpy

Warning when using numpy: runtimewarning: numpy.dtype size changed, may indicate binary incompatibility

You may encounter the following warnings when running Python programs after a new numpy installation:

/usr/local/lib/python2.7/dist-packages/scipy/linalg/basic.py:17: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from ._solve_toeplitz import levinson
/usr/local/lib/python2.7/dist-packages/scipy/linalg/__init__.py:207: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from ._decomp_update import *
/usr/local/lib/python2.7/dist-packages/scipy/special/__init__.py:640: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from ._ufuncs import *
/usr/local/lib/python2.7/dist-packages/scipy/special/_ellip_harm.py:7: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from ._ellip_harm_2 import _ellipsoid, _ellipsoid_norm
/usr/local/lib/python2.7/dist-packages/scipy/interpolate/_bsplines.py:10: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from . import _bspl
/usr/local/lib/python2.7/dist-packages/scipy/sparse/lil.py:19: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from . import _csparsetools
/usr/local/lib/python2.7/dist-packages/scipy/sparse/csgraph/__init__.py:165: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from ._shortest_path import shortest_path, floyd_warshall, dijkstra,\
/usr/local/lib/python2.7/dist-packages/scipy/sparse/csgraph/_validation.py:5: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from ._tools import csgraph_to_dense, csgraph_from_dense,\
/usr/local/lib/python2.7/dist-packages/scipy/sparse/csgraph/__init__.py:167: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from ._traversal import breadth_first_order, depth_first_order, \
/usr/local/lib/python2.7/dist-packages/scipy/sparse/csgraph/__init__.py:169: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from ._min_spanning_tree import minimum_spanning_tree
/usr/local/lib/python2.7/dist-packages/scipy/sparse/csgraph/__init__.py:170: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from ._reordering import reverse_cuthill_mckee, maximum_bipartite_matching, \
/usr/local/lib/python2.7/dist-packages/scipy/spatial/__init__.py:95: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from .ckdtree import *
/usr/local/lib/python2.7/dist-packages/scipy/spatial/__init__.py:96: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from .qhull import *
/usr/local/lib/python2.7/dist-packages/scipy/spatial/_spherical_voronoi.py:18: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from . import _voronoi
/usr/local/lib/python2.7/dist-packages/scipy/spatial/distance.py:122: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  from . import _hausdorff

At this point, you can check the version of numpy below

python
>>> import numpy
>>> numpy.__version__

If version 1.15.0 or above is displayed, the warning is caused by too high numpy version. Downgrade numpy version, such as 1.14.5

sudo pip uninstall numpy
sudo pip install numpy==1.14.5

Lingerror last 2 dimensions of the array must be square

Lingerror last 2 dimensions of the array must be square

reason

Because numpy uses X directly= numpy.linalg.solve (a, b) we must make sure that a is a square matrix, but my matrix is not a square matrix

solve

The least square method: C= np.linalg.lstsq (A, B, rcond=None)[0]

problem

It seems to be an equation with infinite solutions, and I don’t know how to output a unique solution in a specific range

Python TypeError: return arrays must be of ArrayType

from numpy import *
np.log(1.1, 2)

The above code will appear at run time

Typeerror: return arrays must be of arraytype, because the second parameter of log is not base but out array. If you just want to perform normal log operations, you can choose to use numpy.math.log (1.1, 2) or use the log function of Python’s math module

Error in testing keras


nning Keras under Python is reporting an error — it tells me that one way to fix this problem is to uninstall Numpy repeatedly until I can’t find it, and then reinstall this version. Installation keras
no problem, baidu, a lot of reasons, all have been adjusted, still not solve the
I finally find a suitable way to
find their numpy location
below is I save position (for reference only, everyone is different)

where will use the red pen circle below are deleted, then reinstall the


this is solved!

[Python] numpy library array splicing np.concatenate Detailed explanation and examples of official documents

In practice, we often encounter array splicers, and concatenate is a very useful array manipulation function based on the Numpy library.
1, Concatenate (A1, A2…) Axis =0) official document details

concatenate(...)
    concatenate((a1, a2, ...), axis=0)

    Join a sequence of arrays along an existing axis.

    Parameters
    ----------
    a1, a2, ... : sequence of array_like
        The arrays must have the same shape, except in the dimension
        corresponding to `axis` (the first, by default).
    axis : int, optional
        The axis along which the arrays will be joined.  Default is 0.

    Returns
    -------
    res : ndarray
        The concatenated array.

    See Also
    --------
    ma.concatenate : Concatenate function that preserves input masks.
    array_split : Split an array into multiple sub-arrays of equal or
                  near-equal size.
    split : Split array into a list of multiple sub-arrays of equal size.
    hsplit : Split array into multiple sub-arrays horizontally (column wise)
    vsplit : Split array into multiple sub-arrays vertically (row wise)
    dsplit : Split array into multiple sub-arrays along the 3rd axis (depth).
    stack : Stack a sequence of arrays along a new axis.
    hstack : Stack arrays in sequence horizontally (column wise)
    vstack : Stack arrays in sequence vertically (row wise)
    dstack : Stack arrays in sequence depth wise (along third dimension)

2. Parameters
The parameter passed in must be a tuple or list of multiple arrays. In addition, the direction of splicing should be specified. The default is Axis = 0, which means longitudinal splicing of array objects on axis 0 (longitudinal splicing along axis= 1). Note: Generally, Axis = 0 means to operate on the array of this axis, and the operation direction is another axis, namely Axis =1.

In [23]: a = np.array([[1, 2], [3, 4]])

In [24]: b = np.array([[5, 6]])

In [25]: np.concatenate((a, b), axis=0)
Out[25]:
array([[1, 2],
       [3, 4],
       [5, 6]])

The incoming array must have the same shape, and the same shape here is sufficient for the same shape between the arrays on the axis axis in the splicing direction
If the array object is splicing axis= 1, the direction is horizontal axis 0, a is a 2*2 dimensional array, Axis = 0 is 2, B is a 1*2 dimensional array, axis= 0 is 1, the shapes of the two are different, then an error will be reported

In [27]: np.concatenate((a,b),axis = 1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-aa1228decc36> in <module>()
----> 1 np.concatenate((a,b),axis = 1)

ValueError: all the input array dimensions except for the concatenation axis must match exactly

Transpose B, and b is a 2*1 dimensional array:

In [28]: np.concatenate((a,b.T),axis = 1)
Out[28]:
array([[1, 2, 5],
       [3, 4, 6]])

Unable to call numpy in pychar, module notfounderror: no module named ‘numpy’

Python was installed before, and then Anaconda was installed because I was practicing using Python to write scientific calculations. However, after installing Anaconda, another problem occurred. When I wrote Python commands to call numpy on the command line, it could be used normally, but the call in PyCharm would report No module named ‘numpy’, that is, numpy could not be found, as shown below.
Numpy can be used normally on the command line:

However, numpy cannot be used properly in PyCharm:

This problem occurs because the interpreter that Pycharm USES is not the installed Python 3.6, but comes with the Python.exe interpreter, and there are only two modules PIP and SetupTools, so that many third-party libraries cannot be used in Pycharm. Only the Settings on PyCharm (File-> settings-> Project: Current project name -& GT; Set Interpreter in the Project Interpreter. Set it to Anaconda, as shown in the figure below.


Then the program will run normally

The experimental procedure for this example is as follows

from numpy import *
import operator

a = random.rand(4,4)
print(a)

Python’s direct method for solving linear equations (5) — square root method for solving linear equations

The square root method solves systems of linear equations

import numpy as np


def pingfagenfa(a):
    n = a.shape[0]
    g = np.mat(np.zeros((n, n), dtype=float))
    g[0, 0] = np.sqrt(a[0, 0])
    g[1:, 0] = a[1:, 0]/g[0, 0]
    for j in range(1, n-1):
        g[j, j] = np.sqrt((a[j, j] - np.sum(np.multiply(g[j, 0:j], g[j, 0:j]))))
        for i in range(j+1, n):
            g[i, j] = (a[i, j] - np.sum(np.multiply(g[i, 0:j], g[j, 0:j])))/g[j, j]
    g[n-1, n-1] = np.sqrt((a[n-1, n-1] - np.sum(np.multiply(g[n-1, 0:n-1], g[n-1, 0:n-1]))))
    return g


if __name__ == "__main__":
    a = np.mat([[9, 18, 9, -27],
                [18, 45, 0, -45],
                [9, 0, 126, 9],
                [-27, -45, 9, 135]], dtype=float)
    print('G:')
    G = pingfagenfa(a)
    print(G)

The solution results are shown in the figure below:

column
Python is the direct method of solving linear equations (1) — – gaussian elimination
Python the direct method of solving linear equations (2) — – gaussian column main element elimination
Python the direct method of solving linear equations (3) — – column if primary element gauss – when elimination
Python the direct method of solving linear equations (4) — – LU decomposition of the matrix
Python the direct method of solving linear equations (5) — –
square root method to solve linear equations Python direct method for solving systems of linear equations (6) — chase method for solving tridiagonal equations
Python direct method for solving systems of linear equations (7) — givens transformation based QR decomposition
Python direct method for solving systems of linear equations (8) — haushalde transformation based QR decomposition

ImportError: Importing the multiarray numpy extension module failed.

Numpy of

pycharm suddenly became unavailable. At first, I thought numpy version was a problem. Uninstall, reinstall, upgrade, and degrade were not allowed.

then I tested the numpy of anaconda and found that it could be used, but pycharm could not. However, I checked the setting repeatedly, and there was no problem.

finally put solution : system environment variable Path to the bin level C:\ProgramData\Anaconda3\Library\bin, and then restart the computer.

at first I just introduced Anaconda3, but before using numpy I had no problem, maybe I updated conda, the package is confused, so the environment variable introduced must be bin level.

Numpy adds a new dimension: newaxis

Newaxis contained in

numpy can add one dimension to the original array

np.newaxis produces a different array

depending on where it is placed

one-dimensional array

x = np.random.randint(1, 8, size=5)

x
Out[48]: array([4, 6, 6, 6, 5])

x1 = x[np.newaxis, :]

x1
Out[50]: array([[4, 6, 6, 6, 5]])

x2 = x[:, np.newaxis]

x2
Out[52]: 
array([[4],
       [6],
       [6],
       [6],
       [5]])

as you can see from the above code,

when putting newaxis first

, which used to be 5, now becomes 1

x
< script type=”math/tex” id=”MathJax-Element-124″> \times< /script> 5, so the first dimension has changed, the second dimension has changed

and when you put newaxis in the end, the shape of the new array that you output is 5

x
< script type=”math/tex” id=”MathJax-Element-125″> \times< /script> So 1, that’s another dimension that’s less than /p>
So, where you put newaxis, you’ll see an extra dimension in your shape that’s less than /p b>

is as follows:

general problem

is often a problem where you need to take a portion of the data out of the array, that is, take a “slice” or a “strip”

, for example, you need to extract a column

from a two-dimensional array

when you take out the dimension becomes one

if we want to reduce it to two dimensions, we need the above method