What function does this code call? - python

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.

Related

How to pre-load modules into python scripts?

I would like to automatically import some Python modules that I regularly use into my scripts. E.g. instead of having to type every time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
have them automatically imported in every script.
Any tips on how to do this will be appreciated.
Thanks,
Anna
Use the PYTHONSTARTUP environment variable to point to a script that has your imports written.

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

module 'scipy.stats' has no attribute 'nanmean'

I'm getting the error mentioned in the title. I have all of the following three imports in my code:
import scipy as sc
import scipy.stats
from scipy import stats
But still get the error. I'm sure it has something to do with the version, but can't figure out either how to make it work or a workaround for "nanmean". Any suggestions would be appreciated.
nanmean was a deprecated function that was removed from scipy.stats in version 0.18.0. You will have to either use an older version of SciPy or use the equivalent function from NumPy.
from numpy import nanmean
nanmean is also available from scipy module itself, so this might work if you replace
from scipy.stats import nanmean
with
from scipy import nanmean

bpython configuration - importing numpy and matplotlib by default

Is it possible to start the bpython interpreter so that it always runs some custom commands when it launches?
In my case I simply want to do:
import numpy as np
import matplotlib.pyplot as plt
I can't see anything in the docs. Anyone know a way?
It is written in the docs, just not clearly labelled as such at: http://docs.bpython-interpreter.org/django.html
Gist of it is you can have an environment variable called PYTHONSTARTUP. bpython will execute this file before you get dropped in the interpreter.
While ikanobori's answer is the way to go here, I thought I show another simple alternative.
import numpy as np
import matplotlib.pyplot as plt
import bpython
bpython.embed(locals_=locals())
This will start up the bpython REPL and load in the local variables and other symbols. This would be useful if you wanted to have more than one customized shell.

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

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.

Categories