How do I call ipython modules in a short-hand way? - numpy

I'm learning python, and using notebooks;
My tutorial is telling my to use
randn(5)
but this only works for me when I use the fully qualified method; ie:
np.random.randn(5)
I imported numpy as np. Is there something else I need to do to make this work? I also wanted shorthand notation when calling plot() as well.

The tutorial likely assume that %pylab was called, or IPython was started with --pylab. The pylab magic does:
from pylab import *
which includes
from numpy import *
and other things, hiding where functions come from. The tutorial should probably not assume that you have done this, but if it does, it should be very clear about that fact, and mention what has happened and where these functions are coming from.
These days, it is typically considered prudent in teaching materials to make imports like this explicit, e.g.
import numpy as np
np.random.randn(x)
or
from numpy.random import randn
randn(x)
especially in a notebook, where saving a few characters of typing is much less precious than in a terminal.

When I use %pylab inline it works! Thanks

Related

How to import numkt (kotlin's numpy wrapper) into jupyter notebook to be used with kotlin kernel

I installed the kotlin kernel into my jupyter notebook environment using Anaconda. There are several libraries available by default like I can %use lets-plot
I'd really like to use the numpy wrapper which is sometimes called knumpy, kotlin-numpy or numkt
Import statements I see for the package include:
import org.jetbrains.numkt.*
import org.jetbrains.numkt.core.*
import org.jetbrains.numkt.math.*
from places like here
I can currently use the statement: %use numpy
and I get the error: Unresolved reference: numkt
I'd really like to get this functionality into my environment so I can actually do things.
How would I install/import this functionality into/from my anaconda environment.
Thnx
Remove all those imports and replace them with:
%use numpy
Here you can find knumpy very basic example.

Is "import numpy as cp" good practice to handle non-GPU situations?

I'm writing code that is ideally run on the GPU for speed, using CuPy. However, I want the code to be able run (albeit more slowly) using a numpy implementation.
At the moment I am doing the following:
import numpy as np
if gpu_present:
import cupy as cp
else:
import numpy as cp
I am worried that I might run into problems later on. Is this good practice?
When the script is small and the namespace to use can be fixed at the startup, I often use a global variable named xp (same as your solution). A similar pattern that I also sometimes use is to make it an instance attribute of a class (again named xp); it is more tolerable for future extensions because each instance can have a distinct value for that attribute. A similar, much more robust, but cumbersome approach is to make xp the first argument of every function.
When writing a library that may be used in any circumstances (e.g. multithreaded code, using both NumPy and CuPy in a single process), it is better to make each function/class handle the namespace appropriately for the arguments. I often use get_array_module utility for that purpose. CuPy has this function, though it requires CuPy to be installed. Chainer also has it. It is also simple to write by yourself. Using this utility, you can make the code usable with either NumPy or CuPy arrays without a global switch.
Also note that NumPy>=1.17 can dispatch CuPy arrays to appropriate CuPy routines, so you can pass CuPy arrays directly to numpy.* functions in most cases. If your code only does computation on given arrays, you even do not need to use cupy namespace at all (you still need to use it for creating a new array without giving another one, like cupy.ones and cupy.random.*).

Pyinstaller returns fatal error when clicking on the built exe

I've built a digital LogBook for the laboratory that I work in as a PhD in Physics, so it's tailored to our needs. The code runs fine using tkinter for GUI , pandas and openpyxl so that the inserted data can be stored to an excel file. The problem is that I need to turn it into an exe (and specifically an exe for 32-bit from a 64-bit machine). I've used for other apps I've built, the pyinstaller and they work great, but for this one nothing seems to work. I've been searching a week now for a possible solution but I cannot seem to find a way out. Not to mention that many of what I'm reading as answers to similar question is like Greek to me, since I'm a newbie in Python. So, if there is anyone that can give me a clean-cut answer so that I can understand what I'm doing in the process, I would be grateful.
I'm working with Python 2.7.15 and Windows 10
Here are my imports:
from openpyxl import load_workbook
from openpyxl import Workbook
import pandas as pd
from openpyxl.utils.dataframe import dataframe_to_rows
from datetime import *
from tkinter import messagebox
import os
import tkinter.ttk

How to silence the UserWarning from scipy.ndimage.zoom

I'm using scipy.ndimage.zoom and I get this annoying warning:
UserWarning: From scipy 0.13.0, the output shape of zoom() is calculated with round() instead of int() - for these inputs the size of the returned array has changed.
I'm not sure what I should get from it, I started using it with SciPy 1.0.0 so I don't believe it really affects me.
I guess calling it UserWarning is a bit questionable given it's not intended for user consumption, but maybe the intended user is the developer importing the library.
I'm using multiprocessing and I get one warning per process, even more annoying.
Is there a sane way to silent it?
It was easier than I thought, leaving the question for future reference in case anyone needs this.
import warnings
warnings.filterwarnings('ignore', '.*output shape of zoom.*')
Your proposed solution did not work for me. But what does work is:
import warnings
# other nice code
with warnings.catch_warnings():
warnings.simplefilter("ignore")
x = scipy.ndimage.interpolation.zoom(...)

How to retrieve help for Pandas methods using '??'

I am new to Pandas, trying to learn the basics from lecture videos. In one of these the presenter demonstrates that one can call help on methods using ??.
For example if I have loaded a dataframe df then typing df.getitem?? should print the docstring as well as the source code to the console. This would be really great to have but it doesn't work for me! I tried different variants of the command and also tried to find a comment online on this, without success.
What do I need to type in order to retrieve the docstring as well as the source code of a Pandas method? Thanks a lot for your help !
(I am using Python 3.5 and PyCharm in case that makes a difference)
I believe that your lecturer was using ipython as this does support dynamic object information. For instance this is the output in ipython when you do df.__getitem__?? you see the following:
I strongly recommend ipython for interactive python development, you'll find a lot of devs using this for data exploration and analysis, the workbook is really useful for saving your commands and the output