## Fix Python – Numpy: Get random set of rows from 2D array

I have a very large 2D array which looks something like this:
a=
[[a1, b1, c1],
[a2, b2, c2],
…,
[an, bn, cn]]

Using numpy, is there an easy way to get a new 2D array with, e.g., 2 random rows from the initial array a (without replacement)?
e.g.
b=
[[a4, b4, c4],
[a99, b99, c99]]

….

## Fix Python – How to add a new row to an empty numpy array

Using standard Python arrays, I can do the following:
arr = []
arr.append([1,2,3])
arr.append([4,5,6])
# arr is now [[1,2,3],[4,5,6]]

However, I cannot do the same thing in numpy. For example:
arr = np.array([])
arr = np.append(arr, np.array([1,2,3]))
arr = np.append(arr, np.array([4,5,6]))
# arr is now [1,2,3,4,5,6]

I also looked into vstack, b….

## Fix Python – What is the inverse function of zip in python? [duplicate]

Closed 9 years ago.

Possible Duplicate:
A Transpose/Unzip Function in Python

I’ve used the zip() function from the numpy library to sort tuples and now I have a list containing all the tuples. I had since modified that list and now I would like to r….

## Fix Python – Numpy – add row to array

How does one add rows to a numpy array?
I have an array A:
A = array([[0, 1, 2], [0, 2, 0]])

I wish to add rows to this array from another array X if the first element of each row in X meets a specific condition.
Numpy arrays do not have a method ‘append’ like that of lists, or so it seems.
If A and X were lists I would merely do:
for i in X:
….

## Fix Python – Plotting a 2D heatmap with Matplotlib

Using Matplotlib, I want to plot a 2D heat map. My data is an n-by-n Numpy array, each with a value between 0 and 1. So for the (i, j) element of this array, I want to plot a square at the (i, j) coordinate in my heat map, whose color is proportional to the element’s value in the array.
How can I do this?
….

## Fix Python – How do you get the magnitude of a vector in Numpy?

In keeping with the “There’s only one obvious way to do it”, how do you get the magnitude of a vector (1D array) in Numpy?
def mag(x):
return math.sqrt(sum(i**2 for i in x))

The above works, but I cannot believe that I must specify such a trivial and core function myself.
….

## Fix Python – How do I catch a numpy warning like it’s an exception (not just for testing)?

I have to make a Lagrange polynomial in Python for a project I’m doing. I’m doing a barycentric style one to avoid using an explicit for-loop as opposed to a Newton’s divided difference style one. The problem I have is that I need to catch a division by zero, but Python (or maybe numpy) just makes it a warning instead of a normal exception.
So, wh….

## Fix Python – Counting unique values in a column in pandas dataframe like in Qlik?

If I have a table like this:
df = pd.DataFrame({
‘hID’: [101, 102, 103, 101, 102, 104, 105, 101],
‘dID’: [10, 11, 12, 10, 11, 10, 12, 10],
‘uID’: [‘James’, ‘Henry’, ‘Abe’, ‘James’, ‘Henry’, ‘Brian’, ‘Claude’, ‘James’],
‘mID’: [‘A’, ‘B’, ‘A’, ‘B’, ‘A’, ‘A’, ‘A’, ‘C’]
})

I can do count(distinct hID) in Qlik to ….

## Fix Python – How to install python modules without root access?

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?
….

## Fix Python – How to count the number of true elements in a NumPy bool array

I have a NumPy array ‘boolarr’ of boolean type. I want to count the number of elements whose values are True. Is there a NumPy or Python routine dedicated for this task? Or, do I need to iterate over the elements in my script?
….