python-inverse-of-a-matrix failure to import - python

I thought that the code in the python-inverse-of-a-matrix was extremely interesting, particularly since I have used numpy for several years in computations that involve matrices. I was disappointed as the 2 imports from numpy failed. Here are the imports:
from numpy import matrix
from numpy import linalg
Neither matrix nor linalg were found in the numpy package. Clearly I miss something that is quite obvious (not for me, though :) ).
I use Linux (kubuntu) and downloaded the numpy package as a debian package. Are there other packages for "matrix" and for "linalg", if so, what are they?
Thank you in anticipation,
OldAl.

Most likely, you have a numpy.py or numpy.pyc file in your local directory... and python is finding it and importing it instead of the numpy package you expect.
Try this before importing.
import numpy
print(numpy.__file__)
You'll probably find that numpy.__file__ is pointing not to the numpy package, but to something you did not intend to import.
In general, it's a good idea to name your own modules with different names from known/popular packages.

SOLVED
The deb package numpy simply does not have the matrix and linalg sub-packages.
In ubuntu or kubuntu one needs to import scipy as well. Scipy expands the name space of numpy and adds matrix and linalg packages.
OldAl.

Related

Avoid sequential import

I am trying to make a custom module, where modules as numpy, scipy, etc. are imported. I am facing a problem of "deep" sequential import. I see that it is a general behaviour:
import numpy
print(numpy.sys)
Does not provide error (so I can access sys through numpy which is quite weird in my opinion). Can I avoid this somehow in my own package?

python module that imports another python module

I'm new to python and I'm having an issue importing a module that imports numpy,PIL and os packages. I'll try and be as clear as possible with my problem
So I have a module lets call it preprocessing.py in which I've written a class to process an image imported from PIL using Image and converting it to a numpy array so the structure looks like the following (note method1 converts a jpg to numpy array)
----- preprocessing.py
import numpy as np
import os
from PIL import Image
Class process_object:
method1
Now I want to use this module as follows I want to import process_object from preprocessing.py and use method1 to process an image again imported using Image in PIL. So my script computation.py looks like the following
---computation.py
import os
import numpy as np
from PIL import Image
a = process_image(input)
a.method1()
However, when I do this I get the following error message
ImportError: No module named numpy
Could someone explain to me what is going on and how to fix it? I'd really appreciate an explanation which allows me to understand what is going on under the hood, so I can avoid situations like this. I really appreciate any help! Thanks!!
Check in which version of Python pip is installing numpy. It could be that when pip installs it, it's pointing to a different Python version on your system.
For problems like these, I would recommend using:
https://github.com/pyenv/pyenv-virtualenv
Will handle Python versions for you, so that you can differentiate which packages are being installed.
I will also recommend using PyCharm's Community Edition.
https://www.jetbrains.com/pycharm/download
Excellent tool and lets you create your own environment.
Hope this helps.
https://sourceforge.net/projects/numpy/files//NumPy/1.5.0/NOTES.txt/view. This is the support for numpy in Python 3.0. You probably need a newer version of numpy. You can also use:
pip install numpy
or
pip3 install numpy

Python - Considering the official definition Numpy should be a module

I'm studying Python as beginner, please just don't blast me.
I've just studied that a module in Python is a collection of classes and functions; a package, instead, is just a way to identify modules in directories and subdirectories. So, a package in Python should not contain any classes and functions, and NumPy should calle d "module". Am I correct ?
The fact is that official doc for NumPy says :
NumPy is the fundamental package for scientific computing with Python
NumPy is a package. A package is represented by the file __init__.py:
>>> import numpy as np
>>> np
<module 'numpy' from '.../site-packages/numpy/__init__.py'>
When you look into this file, which is pretty long, you will find a lot of imports:
from . import core
from .core import *
from . import compat
from . import lib
from .lib import *
Therefore, all the names imported directly into __init__.py are available at the package level:
>>> np.array
<function numpy.core.multiarray.array>
But as you can see the function array is actually located deep down in the package directory hierarchy. Since always typing numpy.core.multiarray.array is pretty tiresome, importing this function into __init__.py makes a lot of sense becaues now you can type np.array instead.
These terms are often used quite vaguely, but in theory, yes - a module is a collection of classes and functions, while a package is a collection of (one or more) modules. However there are very few cases where a package just contains a module and no supporting code - since any package may want to provide e.g. __version__, __all__ etc, or with subpackages offer methods that provide helper functions relating to imports etc.
So numpy is definitely a package, since it includes several sub-packages (doc, random, fft etc). Of course, it is also a module since it has 'top-level' classes and functions (e.g. numpy.array).
As the other answers make clear, numpy is technically a package (a directory of importable things), but in this case I think the sentence you cite is using the term in its other sense: A package is a thing you can install. PyPI is the Python Package Index, and pip stands for Pip Installs Packages. Both PyPI and pip can deal with things that are single-file modules. Package in this case is the general term for anything installable into your Python environment.

Single module from scipy into python project

My situation:
need to upload solution .zip file to some server where my solution.py file gets executed. However, I need to use scipy method from package spatial, which is not available on server.
Simple attach whole scipy lib ? - No, I can't beacuse it won't fit in .zip file size regulations, however, if I upload only spatial package from scipy (not using any other) everything would fit (size).
Is it possible ?
Because when pasted simply spatial package into my project folder (files from github repo) it didn't work because of errors like 'Couldn't find foofile' mainly in import instructions.
Thanks, any help appreciated
To see one reason why this does not work, you can do from the (i)python commandline
from scipy import spatial
import inspect
inspect.getsourcelines(spatial)
Under the comment text, you can see several imports. If you also look at the first of those,
inspect.getsourcelines(spatial.kdtree)
there is another import line
import scipy.sparse
This is just the first example I found, there are surely many more dependencies and scipy.spatial won't work as is if you don't have access to them. Note also that numpy is imported, and if your server does not have scipy, it probably does not have numpy either.
My suggestion is to either copy/rewrite the scipy code you need (if this is allowed), write your code without scipy or ask for the server admin to either allow larger files or install scipy.

What function does this code call?

I have this code from scikit docs:
import numpy as np
np.random.seed(1)
I understand what this code does semantically, but I cannot understand what this code actually calls.
Ok, numpy is a name of python module and np is just an alias for that. But what is np.random? Is it module inside of another module?
I found source code on GitHub and random is just a folder inside of numpy directory. So numpy should be a package, not a module?
Numpy is a package, that contains module random which contains method seed.

Categories