numpy interview questions
Top numpy frequently asked interview questions
Suppose I have:
test = numpy.array([[1, 2], [3, 4], [5, 6]])
test[i]
gets me ith line of the array (eg [1, 2]
). How can I access the ith column? (eg [1, 3, 5]
). Also, would this be an expensive operation?
Source: (StackOverflow)
When I print a numpy array, I get a truncated representation, but I want the full array.
Is there any way to do this?
Examples:
>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(10000).reshape(250,40)
array([[ 0, 1, 2, ..., 37, 38, 39],
[ 40, 41, 42, ..., 77, 78, 79],
[ 80, 81, 82, ..., 117, 118, 119],
...,
[9880, 9881, 9882, ..., 9917, 9918, 9919],
[9920, 9921, 9922, ..., 9957, 9958, 9959],
[9960, 9961, 9962, ..., 9997, 9998, 9999]])
Source: (StackOverflow)
What are the advantages and disadvantages of each?
From what I've seen, either one can work as a replacement for the other if need be, so should I bother using both or should I stick to just one of them?
Will the style of the program influence my choice? I am doing some machine learning using numpy, so there are indeed lots of matrices, but also lots of vectors (arrays).
Source: (StackOverflow)
How can I sort an array in numpy by the nth column? e.g.
a = array([[1,2,3],[4,5,6],[0,0,1]])
I'd like to sort by the second column, such that I get back:
array([[0,0,1],[1,2,3],[4,5,6]])
thanks.
Source: (StackOverflow)
Is it worth my learning NumPy?
I have approximately 100 financial markets series, and I am going to create a cube array of 100x100x100 = 1 million cells. I will be regressing (3-variable) each x with each y and z, to fill the array with standard errors.
I have heard that for "large matrices" I should use NumPy as opposed to Python lists, for performance and scalability reasons. Thing is, I know Python lists and they seem to work for me.
Is the scale of the above problem worth moving to NumPy?
What if I had 1000 series (that is, 1 billion floating point cells in the cube)?
Source: (StackOverflow)
Is there a way to dump a NumPy array into a CSV file? I have a 2D NumPy array and need to dump it in human-readable format.
Source: (StackOverflow)
I'm curious, whether there is any way to print formatted numpy.arrays, e.g., in the way similar to this:
x = 1.23456
print '%.3f' % x
If I want to print the numpy.array of floats, it prints several decimals, often in 'scientific' format, which is rather hard to read even for low-dimensional arrays. However, numpy.array apparently has to be printed as a string, i.e., with %s. Is there any solution for this purpose?
Source: (StackOverflow)
I have two points in 3D:
(xa, ya, za)
(xb, yb, zb)
And I want to calculate the distance:
dist = sqrt((xa-xb)^2 + (ya-yb)^2 + (za-zb)^2)
What's the best way to do this with Numpy, or with Python in general? I have:
a = numpy.array((xa ,ya, za))
b = numpy.array((xb, yb, zb))
Source: (StackOverflow)
I know there is a method for python list to return the first index of something
l = list(1,2,3)
l.index(2)
>>> 1
Is there something like that for numpy arrays?
Source: (StackOverflow)
How to convert real numpy array to int numpy array?
Tried using map directly to array but it did not work.
Source: (StackOverflow)
I'm taking some university classes and have been given an 'instructional account', which is a school account I can ssh into to do work. I want to run my computationally intensive Numpy, matplotlib, scipy code on that machine, but I cannot install these modules because I am not a system administrator.
How can I do the installation?
Source: (StackOverflow)
I'm currently trying to learn Numpy and Python. Given the following array:
import numpy as N
a = N.array([[1,2],[1,2]])
Is there a function that returns the dimensions of a (e.g.a is a 2 by 2 array).
size()
returns 4 and that doesn't help very much.
Source: (StackOverflow)
I wonder if there is a direct way to import the contents of a csv file into a record array, much in the way that R's read.table(), read.delim(), and read.csv() family imports data to R's data frame? Or is the best way to use csv.reader() and then apply something like numpy.core.records.fromrecords()?
Source: (StackOverflow)