array 1 « numpy « Python Data Type Q&A

Home
Python Data Type Q&A
1.array
2.date
3.dictionary
4.Format
5.integer
6.List
7.numpy
8.regex
9.string
10.tuple
Python Data Type Q&A » numpy » array 1 

1. python.array versus numpy.array    stackoverflow.com

If you are creating a 1d array in Python is there any benefit to using the NumPy package?

2. How do I build a numpy array from a generator?    stackoverflow.com

How can I build a numpy array out of a generator object? Let me illustrate the problem:

>>> import numpy
>>> def gimme():
...   for x in xrange(10):
...     yield ...

3. Identifying numeric and array types in numpy    stackoverflow.com

Is there an existing function in numpy that will tell me if a value is either a numeric type or a numpy array? I'm writing some data-processing code which needs to ...

4. Why are 0d arrays in Numpy not considered scalar?    stackoverflow.com

Surely a 0d array is scalar, but Numpy does not seem to think so... am I missing something or am I just misunderstanding the concept?

>>> foo = numpy.array(1.11111111111, numpy.float64)
>>> numpy.ndim(foo)
0
>>> ...

5. Detect if a NumPy array contains at least one non-numeric value?    stackoverflow.com

I need to write a function which will detect if the input contains at least one value which is non-numeric. If a non-numeric value is found I will raise an error ...

6. Adding a field to a structured numpy array    stackoverflow.com

What is the cleanest way to add a field to a structured numpy array? Can it be done destructively, or is it necessary to create a new array and copy ...

7. Numpy: Is there an array size limit?    stackoverflow.com

I'm learning to use Numpy and I wanted to see the speed difference in the summation of a list of numbers so I made this code:

np_array = numpy.arange(1000000)
start = time.time()
sum_ = ...

8. Definition of mathematical operations (sin…) on NumPy arrays containing objects    stackoverflow.com

I would like to provide "all" mathematical functions for the number-like objects created by a module (the uncertainties.py module, which performs calculations with error propagation)—these objects are numbers with ...

9. Returning array of data mapping values to parameters in python    stackoverflow.com

I have a few functions that return an array of data corresponding to parameters ranges. Example: for a 2d array a, the a_{ij} value corresponds to the parameter set (param1_i, param2_j). How ...

10. Growing matrices columnwise in NumPy    stackoverflow.com

In pure Python you can grow matrices column by column pretty easily:

data = []
for i in something:
    newColumn = getColumnDataAsList(i)
    data.append(newColumn)
NumPy's array doesn't ...

11. Numpy - show decimal values in array results    stackoverflow.com

how do I calculate that an array of python numpy or me of all the calculate decimals and not skip like.

>> A = numpy.array ([[1,2,3], [4,5,6], [7,8,9]]).

>> C = numpy.array ([[7,8,9], ...

12. replace values in an array    stackoverflow.com

as a replacement value for another within an operation with arrays, or how to search within an array and replace a value by another for example:

array ([[NaN, 1., 1., 1., 1., 1., ...

13. make operations excluding the diagonal of an array    stackoverflow.com

as I can perform operations on arrays so that does nothing on the diagonal is calculated such that all but the diagonal

array ([[0.,  1.37, 1.,   1.37, 1.,   ...

14. efficient way to compress a numpy array (python)    stackoverflow.com

I am looking for an efficient way to compress a numpy array. I have an array like: dtype=[(name, (np.str_,8), (job, (np.str_,8), (income, np.uint32)] (my favourite example). if I'm doing something like this: my_array.compress(my_array['income'] ...

15. Euclidean distance between points in two different Numpy arrays, not within    stackoverflow.com

I have two arrays of x-y coordinates, and I would like to find the minimum Euclidean distance between each point in one array with all the points in the other array. ...

16. Python: print the full array    stackoverflow.com

When I print an array i get the following stuff, but I want the full array. Is there any way to do this? Thanks in advance, and everybody a happy, lucky, ...

17. Numpy masked array modification    stackoverflow.com

Currently I have a code that checks if given element in array is equal = 0 and if so then set the value to 'level' value (temp_board is 2D numpy array, ...

18. Custom data types in numpy arrays    stackoverflow.com

I'm creating a numpy array which is to be filled with objects of a particular class I've made. I'd like to initialize the array such that it will only ever contain ...

19. MemoryError when running Numpy Meshgrid    stackoverflow.com

I have 8823 data points with x,y coordinates. I'm trying to follow the answer on how to get a scatter dataset to be represented as a heatmap but when I ...

20. Reducing size of a character array in Numpy    stackoverflow.com

Given a character array:

In [21]: x = np.array(['a     ','bb   ','cccc    '])
One can remove the whitespace using:
In [22]: np.char.strip(x)
Out[22]: 
array(['a', 'bb', 'cccc'], 
 ...

21. Numpy: Creating a complex array from 2 real ones?    stackoverflow.com

I swear this should be so easy... Why is it not? :( In fact, I want to combine 2 parts of the same array to make a complex array:

Data[:,:,:,0] , Data[:,:,:,1]
These don't ...

22. Extending a series of nonuniform netcdf data in a numpy array    stackoverflow.com

I am new to python, apologies if this has been asked already. Using python and numpy, I am trying to gather data across many netcdf files into a single array by iteratively ...

23. Several numpy arrays with SWIG    stackoverflow.com

I am using SWIG to pass numpy arrays from Python to C++ code:

%include "numpy.i"
%init %{
import_array();
%}

%apply (float* INPLACE_ARRAY1, int DIM1) {(float* data, int n)};

class Class 
{
  public: 
  void test(float* ...

24. Objects array with numpy    stackoverflow.com

are there any way to create an object form any class inside a numpy array?. Something like:

a = zeros(4)

for i in range(4):
   a[i]=Register()
Thanks

25. Python/Numpy: Divide array    stackoverflow.com

I have some data represented in a 1300x1341 matrix. I would like to split this matrix in several pieces (e.g. 9) so that I can loop over and process them. The ...

26. How does one wrap numpy array types?    stackoverflow.com

I'd like to make a class extending the numpy array base type,

class LemmaMatrix(numpy.ndarray):
    @classmethod
    def init_from_corpus(cls, ...): cls(numpy.empty(...))
But apparently, it will not allow multi-dimensional array ...

27. Pretty-printing of numpy.array    stackoverflow.com

I'm curious, whether there is any way to print formated 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, ...

28. linear combinations in python/numpy    stackoverflow.com

greetings, I'm not sure if this is a dumb question or not. Lets say I have 3 numpy arrays, A1,A2,A3, and 3 floats, c1,c2,c3 and I'd like to evaluate B = A1*c1+ A2*c2+ A3*c3 will ...

29. store/load numpy array from binary files    stackoverflow.com

I would like to store and load numpy arrays from binary files. For that purposes, I created two small functions. Each binary file should contain the dimensionality of the given matrix.

def ...

30. numpy array assignment problem    stackoverflow.com

I have a strange problem in Python 2.6.5 with Numpy. I assign a numpy array, then equate a new variable to it. When I perform any operation to the new ...

31. how to display a numpy array with pyglet?    stackoverflow.com

I have a label matrix with dimension (100*100), stored as a numpy array, and I would like to display the matrix with pyglet. My original idea is to use this matrix to ...

32. How to use NumPy array with ctypes?    stackoverflow.com

I am still writing on a python interface for my c code with ctypes. Today I substituted my file reading function with a python version, which was programmed by somebody else ...

33. Numpy object array of numerical arrays    stackoverflow.com

I want to create an array with dtype=np.object, where each element is an array with a numerical type, e.g int or float. For example:

>>> a = np.array([1,2,3])
>>> b = np.empty(3,dtype=np.object)
>>> b[0] ...

34. Splicing NumPy arrays    stackoverflow.com

I am having a problem splicing together two arrays. Let's assume I have two arrays:

a = array([1,2,3])
b = array([4,5,6])
When I do vstack((a,b)) I get
[[1,2,3],[4,5,6]]
and if I do hstack((a,b)) I get:
[1,2,3,4,5,6]
But what ...

35. Fast replacement of values in a numpy array    stackoverflow.com

I have a very large numpy array (containing up to a million elements) like the one below:

[ 0  1  6  5  1  2  7  ...

36. Array broadcasting with numpy    stackoverflow.com

How do I write the following loop using Python's implicit looping?

def kl(myA, myB, a, b):
    lots of stuff that assumes all inputs are scalars

x, y = meshgrid(inclusive_arange(0.0, xsize, ...

37. SimpleJSON and NumPy array    stackoverflow.com

What is the most efficient way of serializing a numpy array using simplejson?

38. What is the preferred way to preallocate NumPy arrays?    stackoverflow.com

I am new to NumPy/SciPy. From the documentation, it seems more efficient to preallocate a single array rather than call append/insert/concatenate. For example, to add a column of 1's to an array, ...

39. Using Numpy arrays as lookup tables    stackoverflow.com

I have a 2D array of Numpy data read from a .csv file. Each row represents a data point with the final column containing a a 'key' which corresponds uniquely to ...

40. Adding an array in numpy at a specified location    stackoverflow.com

Is there a fast way in numpy to add array A to array B at a specified location? For instance, if

B = [
    [0, 1, 2],
   ...

41. Numpy broadcast array    stackoverflow.com

I have the following array in NumPy:

A = array([1, 2, 3])
How can I obtain the following matrices (without an explicit loop)?
B = [ 1 1 1
      ...

42. Arithmetic Progression in Python without storing all the values    stackoverflow.com

I'm trying to represent an array of evenly spaced floats, an arithmetic progression, starting at a0 and with elements a0, a0 + a1, a0 + 2a1, a0 + 3a1, ... This is ...

43. Select cells randomly from NumPy array - without replacement    stackoverflow.com

I'm writing some modelling routines in NumPy that need to select cells randomly from a NumPy array and do some processing on them. All cells must be selected without replacement (as ...

44. How to access numpy array within PyCXX    stackoverflow.com

I would like to convert numpy array to some double* or stl vector on the c++ side. I'm actually using PyCXX for this and I can't figure out the way to ...

45. calculating means of many matrices in numpy    stackoverflow.com

I have many csv files which each contain roughly identical matrices. Each matrix is 11 columns by either 5 or 6 rows. The columns are variables and the rows ...

46. Can you change the way numpy prints arrays?    stackoverflow.com

I have a 3d, 3x3x3 array of integers. Numpy will print these as a block of the first 3x3, then below it the 2nd 3x3, then below that the 3rd ...

47. How to detect if the values in array are in a certain range and return a binary array in Python?    stackoverflow.com

So I am trying to detect if the values in an array is in a certain range and then return a binary logical array i.e. one for true and zero for ...

48. Resizing and stretching a NumPy array    stackoverflow.com

I am working in Python and I have a NumPy array like this:

[1,5,9]
[2,7,3]
[8,4,6]
How do I stretch it to something like the following?
[1,1,5,5,9,9]
[1,1,5,5,9,9]
[2,2,7,7,3,3]
[2,2,7,7,3,3]
[8,8,4,4,6,6]
[8,8,4,4,6,6]
These are just some example arrays, I will ...

49. How to get running counts for numpy array values?    stackoverflow.com

OK, I think this will be fairly simple, but my numpy-fu is not quite strong enough. I've got a an array A of ints; it's tiled N times. I want a ...

50. Error when re-sizing data in numpy array    stackoverflow.com

I have two arrays that I want to re-size, but I also want to retain the original values. The code below re-sizes the arrays, but the problem is that it ...

51. Getting data from ctypes array into numpy    stackoverflow.com

I am using a Python (via ctypes) wrapped C library to run a series of computation. At different stages of the running, I want to get data into Python, and ...

52. View onto a numpy array?    stackoverflow.com

I have a 2D numpy array. Is there a way to create a view onto it that would include the first k rows and all columns? The point is to avoid copying ...

53. trouble populating a numpy array    stackoverflow.com

My code below is not populating the numpy/pylab array called RRmeanArray. Can anyone show me how to fix the code so that it populates the array?

    import ...

54. Replace part of numpy 1D array with shorter array    stackoverflow.com

I have a 1D numpy array containing some audio data. I'm doing some processing and want to replace certain parts of the data with white noise. The noise should, however, be ...

55. Splice arrays in Numpy?    stackoverflow.com

I'd like to perform a splice of sorts in Numpy. Let's say I have two arrays, a and b:

>>> a
array([[ 1, 10],
       [ 2, 20],
 ...

56. Why can't I create a numpy array like this: array([1, 2], 3)    stackoverflow.com

from numpy import array
test_list = [[1,2],3]
x = array(test_list) #ValueError: setting an array element with a sequence.
Basically, I have a point with 2 coordinates and a scale and I was trying to ...

57. Numpy : Please help, my array can't be printed completely    stackoverflow.com

I am beginner to Python and right now i'm writing a code to process some sensors data in python and store it in two arrays with the size of 1001x1 each. ...

58. initialize a numpy array    stackoverflow.com

Is there way to initialize a numpy array of a shape and add to it? I will explain what I need with a list example. If I want to create a ...

59. numpy array mapping and take average    stackoverflow.com

I have three arrays

import numpy as np
value = np.array ([1, 3, 3, 5, 5, 7, 3])
index = np.array ([1, 1, 3, 3, 6, 6, 6])
data  = np.array ([1, 2, 3, ...

60. first differences in array    stackoverflow.com

#compute first differences of 1d array
from numpy import *

x = arange(10)
y = zeros(len(x))

for i in range(1,len(x)):
    y[i] = x[i] - x[i-1]
print y
The above code works but there must ...

61. numpy array of objects    stackoverflow.com

I'm trying to implement a simulation for a lattice model (lattice boltzmann) in Python. Each site of the lattice has a number of properties, and interact with neighboring sites according to ...

62. PEP 3118 warning when using ctypes array as numpy array    stackoverflow.com

I'm getting the following warning message when I try to use a ctypes array as a numpy array:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", ...

63. Python: calculate the magnetic field of a wire using biot-savart-law    stackoverflow.com

I want to calculate the magnetic field of a wire using biot-savart-law. Some people recommend to use numpy arrays. At first i did it with vpython and it worked. But know ...

64. Shear a numpy array    stackoverflow.com

I'd like to 'shear' a numpy array. I'm not sure I'm using the term 'shear' correctly; by shear, I mean something like: Shift the first column by 0 places
Shift the second column ...

65. How do I pass large numpy arrays between python subprocesses without saving to disk?    stackoverflow.com

Is there a good way to pass a large chunk of data between two python subprocesses without using the disk? Here's a cartoon example of what I'm hoping to accomplish:

import sys, ...

66. numpy how to add items into an array    stackoverflow.com

I'm new to numpy and require to accomplish the following task: from:

a = array([[1,3,4],[1,2,3]...[1,2,1]])
(add one element to each row) to:
a = array([[1,3,4,x],[1,2,3,x]...[1,2,1,x]])
How can i accomplish this? I have tried doing stuff like a[n] ...

67. Using numpy.bincount with array weights    stackoverflow.com

I would like to use bincount to sum arrays, however it supports only doubles. For example this works:

np.bincount([1, 1, 0],weights=np.array([1, 2, 4]))
Out: array([ 4.,  3.])
However I would like to use a ...

68. pyplot of array crashes    stackoverflow.com

I am attempting to plot array values obtained from summing individual columns of a numpy array. Working on Win XP, Python 2.5, matplotlib-1.0.1, numpy-1.5.1, PIL-1.1.7 Here is the code:

import Image
import numpy as ...

69. Python/numpy: all values in array up to x?    stackoverflow.com

I have an ordered array like this: numpy.array([1, 2, 5, 10, 25, 36, 66, 90, 121, 230, 333, 500]) Suppose I want all values up to 60 (if 60 isn't in, i ...

70. Split array at value in numpy    stackoverflow.com

I have a file containing data in the format:

0.0 x1
0.1 x2
0.2 x3
0.0 x4
0.1 x5
0.2 x6
0.3 x7
...
The data consists of multiple datasets, each starting with 0 in the first column (so x1,x2,x3 ...

71. Array of ranges with numpy    stackoverflow.com

I have the following array:

>>> x = numpy.array([2,4,2,3,1])
>>> x
array([2, 4, 2, 3, 1])
I would like an array of ranges of these values. I can create it like this:
>>> numpy.hstack( (numpy.arange(v) for ...

72. Load a numpy array into C from a file that was saved with numpy.save(...)    stackoverflow.com

I'd like to access data in a C program that resides in a file that was written from python using numpy.save(...). So far, I was looking for a way to call ...

73. Interweaving two numpy arrays    stackoverflow.com

Assume the following arrays are given:

a = array([1,3,5])
b = array([2,4,6])
How would one interweave them efficiently so that one gets a third array like this
c = array([1,2,3,4,5,6])
It can be assumed that length(a)==length(b). ...

74. Numpy: Joining structured arrays?    stackoverflow.com

Input

I have many numpy structured arrays in a list like this example:
import numpy

a1 = numpy.array([(1, 2), (3, 4), (5, 6)], dtype=[('x', int), ('y', int)])

a2 = numpy.array([(7,10), (8,11), (9,12)], dtype=[('z', ...

75. Why does this PIL call crash python?    stackoverflow.com

import Image
from numpy import zeros, asarray
YUV = zeros((240, 320, 3), dtype='uint8')
im = Image.fromarray(YUV, mode="YCbCr")
blah = asarray(im)
When I run this (IPython 0.10.1 on Py 2.7.1) it seems to make python read some ...

76. Coordinates of item on numpy array    stackoverflow.com

I have a numpy array:

[[  0.   1.   2.   3.   4.]
 [  7.   8.   9.  10.  ...

77. Replacing values greater than a limit in a numpy array    stackoverflow.com

I have an array n x m, and maximum values for each column. What's the best way to replace values greater than the maximum, besides checking each element? For example:

def check_limits(bad_array, maxs):
 ...

78. Immutable numpy array?    stackoverflow.com

Is there a simple way to create an immutable NumPy array? If one has to derive a class from ndarray to do this, what's the minimum set of methods that one has ...

79. numpy array subclass unexpedly shares attributes across instances    stackoverflow.com

I am having a weird subclass numpy.ndarray issue that feels like
Values of instance variables of superclass persist across instances of subclass
But I have not been able to understand fully or ...

80. Drawing polygons in numpy arrays    stackoverflow.com

I'm trying to draw polygons like this:

In [1]: canvas = numpy.zeros((12, 12), dtype=int)

In [2]: mahotas.polygon.fill_polygon(
   ...: [(1, 1), (1, 10), (10, 10), (10, 1)],
   ...: canvas)

In [3]: ...

81. How to speed up numpy array-filling in python?    stackoverflow.com

I'm trying to fill a preallocated bytearray using the following code:

# preallocate a block array
dt = numpy.dtype('u8')
in_memory_blocks = numpy.zeros(_AVAIL_IN_MEMORY_BLOCKS, dt)

...

# write all the blocks out, flushing only as desired
blocks_per_flush_xrange = xrange(0, ...

82. Python/Numpy - Masked Arrays are Very Slow    stackoverflow.com

Is there anything I can do to speed up masked arrays in numpy? I had a terribly inefficient function that I re-wrote to use masked arrays (where I could just ...

83. Running Cumulative sum of 1d NumPy Array    stackoverflow.com

I have a numpy array like x=np.array([1,2,3,4]) I want to create another numpy array y which is the cumulative sum of x, so that y=np.array([1,3,6,10]) What is a good num-Pythonic way to ...

84. Consistenly create same random numpy array    stackoverflow.com

I am waiting for another developer to finish a piece of code that will return an np array of shape (100,2000) with values of either -1,0, or 1. In the meantime, I ...

85. Shift all indices in NumPy Array    stackoverflow.com

I have a numpy array like
x=np.array([0,1,2,3,4]) and want to create an array where the value in index 0 is in index 1, index 1 is in index 2, etc. The output I want ...

86. numpy array initialization (fill with identical values)    stackoverflow.com

I need to create a numpy array of length n, each element of which is v. Is there anything better than:

a = empty(n)
for i in range(n):
    a[i] = v
I ...

87. Get SQL headers from Numpy Array in python    stackoverflow.com

With below, I can get the row and col data from SQL:
How would I get the table headers as part of the result set or array.?

    ...

88. NumPy: programmatically modify dtype of a structured array    stackoverflow.com

I have a structured array, for example:

import numpy as np
orig_type = np.dtype([('Col1', '<u4'), ('Col2', '<i4'), ('Col3', '<f8')])
sa = np.empty(4, dtype=orig_type)
where sa looks like (random data):
array([(11772880L, 14527168, 1.079593371731406e-307),
     ...

89. Passing numpy arrays to Java using Jpype    stackoverflow.com

I am trying to convert a numpy array into a Java-like array using JPype's JArray command.

values = valBD.ReadAsArray()
JArray(float, values.ndim)(values)
leads to the following error message:
JArray(float, values.ndim)(values) File ...

90. numpy array of python objects    stackoverflow.com

Since when did numpy allow you to define an array of python objects? Objects array with numpy. Is there any fundamental difference between these arrays and a python list? What is the ...

91. Populate arrays in python (numpy)?    stackoverflow.com

Given a file in the format below:

a a 0
a b 1
a c 1
b b 0
b a 1
b c 1
c c 0
c a 1
c b 1
The third column is the distance between ...

92. Subtotals in numpy    stackoverflow.com

I have a numpy array:

[[1 3 1]
 [8 9 0]
 [1 3 1]
 [8 4 1]
 [5 1 0]]
and I want to produce subtotals (count, sums, averages) for columns 0,1 for ...

93. numpy.savetxt Problems with 1D array writing    stackoverflow.com

I'm trying to use numpy's savetxt function to generate a bunch of files as inputs for another piece of software. I'm trying to write an array of the form:

a=np.array([1,2,3,4,...])
a.shape=>(1,n)
to a text file ...

94. How to copy data from a numpy array to another    stackoverflow.com

What is the fastest way to copy data from array b to array a, without modifying the address of array a. I need this because an external library (PyFFTW) uses a ...

95. Numpy array from cStringIO object and avoiding copies    stackoverflow.com

This to understand things better. It is not an actual problem that I need to fix. A cstringIO object is supposed to emulate a string, file and also an iterator over ...

96. ctypes pointer into the middle of a numpy array    stackoverflow.com

I know how to get a ctypes pointer to the beginning of a numpy array:

a = np.arange(10000, dtype=np.double)
p = a.ctypes.data_as(POINTER(c_double))
p.contents
c_double(0.0)
however, I need to pass the pointer to, let's say, element 100, ...

97. ctypes pointer to numpy array with custom dtype    stackoverflow.com

I have a numpy array with a custom dtype:

a = np.zeros(100, dtype=np.dtype([('one',np.double),('two',np.int)]))
a['one']=np.arange(100)
a['two']=np.arange(100)*-1
I want to create a ctypes pointer that I can pass to a C library. The problem is that the C ...

98. Create numpy array from C-ptr aquired via ctypes    stackoverflow.com

I have allocated a chunk of double in a C library and I would like to create a numpy 1D array based on that data; ideally I would like two versions ...

99. Numpy masked arrays - indicating missing values    stackoverflow.com

import numpy as np
import numpy.ma as ma

"""This operates as expected with one value masked"""
a = [0., 1., 1.e20, 9.]
error_value = 1.e20
b = ma.masked_values(a, error_value)
print b

"""This does not, all values are masked ...

100. Python -- Share a Numpy Array Between Processes?    stackoverflow.com

I'd like to use python's multiprocessing module to utilize a multi-core Linux server. I need all processes to have read/write access to the same shared memory. Instead of using a list or ...

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.