As output of a script, I have numpy masked array and standard numpy array. How do I easily check while running the script if an array is a masked (has data, mask attributes) one or not?
You can check explicitly if it is a masked array by isinstance(arr, np.ma.MaskedArray), or you can check for the attributes hasattr(arr, 'mask'). I'd probably recommend the first approach in general.
Related
Why are numpy arrays called homogeneous when you can have elements of different type in the same numpy array like this?
np.array([1,2,3,4,"a"])
I understand that I cannot perform some types of broadcasting operations like I cannot perform
np1*4 here and it results in an error.
but my question really is when it can have elements of different types, why it is called homogeneous?
Numpy automatically converts them to most applicable datatype.
e.g.,
>>> np.array([1,2,3,4,"a"]).dtype.type
numpy.str_
In short this means all elements are of string.
>>> np.array([1,2,3,4]).dtype.type
numpy.int64
Let's suppose I have an image such that
image.shape=(280,280,3)
If I do img[[[1,2],[1,2],[1,2]]].shape, I obtain (2,). But I expect to obtain (2,2,2)...
How can slicing be performed simultaneously on several dimensions in numpy?
You are using the Array of Indices syntax, when you probably want slices.
Try something like this:
img[1:3, 1:3, 1:3]
I would like to store a image represented as a numpy array in a Pyspark data frame.
When I try the I get an error data type not supported.
looking at the data types supported in Pyspark I don't see numpy, wondering if there's a way to store array.
I also tried numpy as string but the string for some reason is truncated contains ...
Any suggestions or solutions?
I want to save the result of TfidfVectorizer in sklearn.feature_extraction.text into a text file for future use. As I found, it is a sparse matrix of type ''. However when I try to save it using the following code
np.savetxt('Feature_TfIdf.txt', X_Tfidf, fmt='%2.6f')
I get an error like this
IndexError: tuple index out of range
Use joblib.dump or sklearn.externals.joblib.dump for this. NumPy doesn't get SciPy sparse matrices.
Simple example:
np.save('TfIdf.pkl',tfidf)
I manage to solve the problem by converting the sparse matrix to full matrix and then save matrix and save the results. This approach however is not useful for large arrays so it is better to save the matrix in .pkl format.
I was printing one list of values in Python, when I got this:
[ 0.00020885 0.00021386 0.0002141 ..., 0.0501399 0.12051606
0.12359095]
What is the problem here? The list should have at least size 20. What happened to the elements shown as ...?
The problem is that you are not printing a Python list, but a NumPy array. NumPy output can be configured using numpy.set_printoptions().
Data types matter. If you wonder about the behaviour of some object, first check its type.