Maximizing interpolated matrix - python

Say I need to max_(a', m') f(a, m, e, m', a'), and I have approximated f with a grid V1. This is a numpy matrix with shape (nA, nM, nE, nM, nA) (attached in the end).
I want to first interpolate and then do the maximization. The following is my current code (I paste code to recreate Grid in the end):
# takes grid indices (first three dimensions) idx and interpolates on V
def interpolateV(idx, V, Grid):
from scipy.interpolate import interp2d
f = interp2d(Grid.mGrid, Grid.aGrid, V[idx])
return f
# (somewhere else:)
s2 = (Grid.nM, Grid.nA, Grid.nE)
v1Max = np.empty(s2)
v1ArgMaxA = np.empty(s2)
v1ArgMaxM = np.empty(s2)
from scipy import optimize
for idx in np.ndindex(V1[..., 0,0].shape):
V1i = interpolateV(idx, V1, Grid)
x, f, d = optimize.fmin_l_bfgs_b(lambda x: -V1i(x[0], x[1]), np.array([1, 1]), bounds=[(Grid.aMin, Grid.aMax), (Grid.mMin, Grid.mMax)], approx_grad=True)
v1Max[idx] = f
v1ArgMaxA[idx], v1ArgMaxM[idx] = x
# let's compare with standard grid-wise optimization (without interpolation):
temp = V1.max(axis=-1)
# maximize over m
v1Max = temp.max(axis=-1)
# now max over a, given optimal m
v1ArgMaxAGrid = temp.argmax(axis=-1)
So far, so good. However, the values of the interpolated maximized are way off:
In[51]: v1ArgMaxAGrid[:,:,0]
Out[51]:
array([[0, 0, 0, 0, 2],
[0, 0, 0, 0, 2],
[0, 0, 0, 2, 2],
[0, 0, 0, 2, 3],
[0, 0, 0, 2, 3]], dtype=int64)
In[54]: Grid.aGrid[v1ArgMaxAGrid[:,:,0]]
Out[54]:
array([[ 0. , 0. , 0. , 0. , 3.5 ],
[ 0. , 0. , 0. , 0. , 3.5 ],
[ 0. , 0. , 0. , 3.5 , 3.5 ],
[ 0. , 0. , 0. , 3.5 , 5.25],
[ 0. , 0. , 0. , 3.5 , 5.25]])
In[52]: v1ArgMaxA[:,:,0]
Out[52]:
array([[ 0. , 0.75 , 2.25 , 7. , 7. ],
[ 0. , 1.5 , 4.247, 7. , 7. ],
[ 0.75 , 2.25 , 7. , 7. , 7. ],
[ 1.5 , 1.5 , 7. , 7. , 7. ],
[ 2.25 , 4.939, 7. , 7. , 7. ]])
What is happening here; why are the values so off? Am I doing a mistake?
following the copy paste to recreate Grid, V1:
class Grids(object):
nE = 2
nA = 5
nM = 5
M = 3
A = 7
mMin = 0
mMax = M
aMin = 0
aMax = A
def __init__(self):
self.reset();
def reset(self):
self.mGrid = np.linspace(self.mMin, self.mMax, self.nM)
self.aGrid = np.linspace(self.aMin, self.aMax, self.nA)
self.eGrid = np.array([0.318, 3.149])
self.transitionE = np.array([[1., 0.],
[0., 1.]])
import numpy as np
Grid = Grids()
V1 = np.array([[[[[ 1.19 , 0.975, -0.371, -2.848, -6.456],
[ -1.463, -4.313, -8.294, -13.407, -19.65 ],
[ -9.888, -15.377, -21.997, -29.748, -38.63 ],
[-24.574, -32.701, -41.958, -52.347, -63.866],
[-45.562, -56.325, -68.218, -81.242, -95.397]],
[[ 64.724, 64.672, 64.567, 64.358, 54.127],
[ 64.247, 63.964, 53.759, 52.687, 50.487],
[ 53.526, 52.078, 49.501, 45.799, 40.969],
[ 48.389, 44.307, 39.105, 32.769, 25.314],
[ 37.062, 30.347, 22.52 , 13.553, 3.47 ]]],
[[[ 12.624, 12.704, 12.591, 2.602, 1.618],
[ 2.237, 2.011, 0.655, -1.832, -5.45 ],
[ -0.064, -2.928, -6.923, -12.049, -18.306],
[ -8.624, -14.126, -20.759, -28.522, -37.416],
[-23.488, -31.625, -40.894, -51.293, -62.822]],
[[ 65.686, 65.695, 65.679, 65.631, 65.537],
[ 65.401, 65.342, 65.23 , 65.014, 54.778],
[ 65.174, 64.881, 54.667, 53.59 , 51.385],
[ 54.43 , 52.973, 50.396, 46.685, 41.855],
[ 49.228, 45.138, 39.936, 33.594, 26.136]]],
[[[ 13.681, 13.872, 14.024, 14.117, 14.093],
[ 13.671, 13.74 , 13.617, 3.617, 2.624],
[ 3.636, 3.397, 2.027, -0.474, -4.106],
[ 1.2 , -1.677, -5.684, -10.823, -17.092],
[ -7.538, -13.051, -19.694, -27.468, -36.373]],
[[ 66.553, 66.597, 66.623, 66.631, 66.614],
[ 66.362, 66.364, 66.342, 66.287, 66.188],
[ 66.327, 66.259, 66.138, 65.917, 55.676],
[ 66.077, 65.776, 55.562, 54.476, 52.271],
[ 55.269, 53.804, 51.227, 47.51 , 42.677]]],
[[[ 14.6 , 14.839, 15.054, 15.242, 15.394],
[ 14.728, 14.909, 15.05 , 15.133, 15.098],
[ 15.07 , 15.126, 14.989, 4.975, 3.968],
[ 4.9 , 4.648, 3.265, 0.752, -2.892],
[ 2.286, -0.601, -4.62 , -9.769, -16.048]],
[[ 67.36 , 67.427, 67.481, 67.52 , 67.543],
[ 67.229, 67.266, 67.286, 67.287, 67.265],
[ 67.288, 67.281, 67.25 , 67.19 , 67.085],
[ 67.231, 67.154, 67.033, 66.803, 56.562],
[ 66.917, 66.607, 56.393, 55.301, 53.093]]],
[[[ 15.442, 15.71 , 15.96 , 16.191, 16.4 ],
[ 15.647, 15.875, 16.08 , 16.257, 16.399],
[ 16.128, 16.294, 16.422, 16.491, 16.443],
[ 16.334, 16.377, 16.227, 6.201, 5.182],
[ 5.986, 5.723, 4.33 , 1.806, -1.849]],
[[ 68.123, 68.207, 68.28 , 68.342, 68.391],
[ 68.036, 68.096, 68.143, 68.176, 68.194],
[ 68.155, 68.183, 68.195, 68.19 , 68.163],
[ 68.192, 68.176, 68.145, 68.076, 67.971],
[ 68.07 , 67.984, 67.864, 67.628, 57.384]]]],
[[[[ 11.877, 1.81 , 1.59 , 0.238, -2.246],
[ 0.873, -0.853, -3.709, -7.696, -12.814],
[ -4.928, -9.292, -14.787, -21.413, -29.17 ],
[-16.988, -23.99 , -32.123, -41.386, -51.78 ],
[-35.352, -44.989, -55.758, -67.657, -80.686]],
[[ 65.151, 65.131, 65.075, 64.966, 64.753],
[ 64.779, 64.647, 64.36 , 54.151, 53.076],
[ 54.24 , 53.917, 52.465, 49.888, 46.183],
[ 51.728, 48.771, 44.694, 39.483, 33.153],
[ 43.026, 37.436, 30.734, 22.892, 13.934]]],
[[[ 13.101, 13.245, 13.318, 13.2 , 3.204],
[ 12.924, 2.847, 2.616, 1.253, -1.24 ],
[ 2.272, 0.533, -2.337, -6.338, -11.47 ],
[ -3.664, -8.041, -13.548, -20.187, -27.956],
[-15.902, -22.915, -31.058, -40.332, -50.737]],
[[ 66.066, 66.092, 66.098, 66.078, 66.025],
[ 65.827, 65.8 , 65.738, 65.622, 65.403],
[ 65.706, 65.564, 65.268, 55.054, 53.974],
[ 55.144, 54.812, 53.36 , 50.774, 47.069],
[ 52.567, 49.602, 45.525, 40.308, 33.975]]],
[[[ 14.087, 14.302, 14.487, 14.632, 14.72 ],
[ 14.148, 14.281, 14.344, 14.216, 4.21 ],
[ 14.323, 4.232, 3.987, 2.611, 0.104],
[ 3.536, 1.784, -1.099, -5.112, -10.256],
[ -2.578, -6.965, -12.484, -19.133, -26.912]],
[[ 66.905, 66.96 , 66.999, 67.022, 67.025],
[ 66.742, 66.762, 66.76 , 66.734, 66.676],
[ 66.754, 66.718, 66.646, 66.525, 66.301],
[ 66.609, 66.459, 66.163, 55.94 , 54.86 ],
[ 55.983, 55.643, 54.191, 51.599, 47.891]]],
[[[ 14.969, 15.221, 15.454, 15.663, 15.844],
[ 15.134, 15.338, 15.513, 15.648, 15.725],
[ 15.548, 15.667, 15.716, 15.574, 5.554],
[ 15.587, 5.483, 5.226, 3.837, 1.318],
[ 4.622, 2.859, -0.034, -4.058, -9.213]],
[[ 67.691, 67.766, 67.829, 67.879, 67.915],
[ 67.581, 67.629, 67.662, 67.678, 67.676],
[ 67.669, 67.679, 67.669, 67.637, 67.574],
[ 67.658, 67.612, 67.541, 67.411, 67.187],
[ 67.449, 67.29 , 66.994, 56.765, 55.682]]],
[[[ 15.786, 16.063, 16.324, 16.569, 16.794],
[ 16.016, 16.258, 16.48 , 16.679, 16.85 ],
[ 16.533, 16.724, 16.884, 17.006, 17.07 ],
[ 16.811, 16.918, 16.954, 16.8 , 6.768],
[ 16.673, 6.559, 6.29 , 4.891, 2.362]],
[[ 68.439, 68.529, 68.61 , 68.679, 68.737],
[ 68.368, 68.436, 68.492, 68.535, 68.566],
[ 68.507, 68.546, 68.571, 68.581, 68.574],
[ 68.572, 68.573, 68.563, 68.523, 68.46 ],
[ 68.497, 68.443, 68.372, 68.236, 68.009]]]],
[[[[ 12.453, 12.498, 2.425, 2.198, 0.84 ],
[ 2.083, 1.483, -0.248, -3.111, -7.104],
[ -1.092, -4.331, -8.701, -14.202, -20.834],
[-10.528, -16.405, -23.412, -31.551, -40.82 ],
[-26.266, -34.779, -44.422, -55.196, -67.101]],
[[ 65.555, 65.558, 65.534, 65.474, 65.36 ],
[ 65.252, 65.179, 65.043, 64.752, 54.54 ],
[ 64.974, 54.631, 54.304, 52.852, 50.272],
[ 53.942, 52.11 , 49.158, 45.072, 39.867],
[ 47.865, 43.4 , 37.823, 31.106, 23.273]]],
[[[ 13.541, 13.722, 13.859, 13.927, 13.802],
[ 13.5 , 13.534, 3.451, 3.214, 1.846],
[ 3.482, 2.868, 1.123, -1.753, -5.76 ],
[ 0.172, -3.08 , -7.463, -12.976, -19.62 ],
[ -9.442, -15.329, -22.348, -30.497, -39.776]],
[[ 66.433, 66.473, 66.495, 66.496, 66.472],
[ 66.231, 66.227, 66.196, 66.13 , 66.011],
[ 66.178, 66.096, 65.952, 65.655, 55.438],
[ 65.877, 55.526, 55.199, 53.738, 51.158],
[ 54.781, 52.941, 49.989, 45.897, 40.689]]],
[[[ 14.475, 14.708, 14.917, 15.095, 15.235],
[ 14.588, 14.759, 14.886, 14.943, 14.808],
[ 14.899, 14.92 , 4.823, 4.572, 3.19 ],
[ 4.746, 4.119, 2.362, -0.527, -4.546],
[ 1.258, -2.005, -6.398, -11.922, -18.577]],
[[ 67.247, 67.312, 67.362, 67.398, 67.417],
[ 67.109, 67.142, 67.158, 67.152, 67.123],
[ 67.158, 67.145, 67.105, 67.033, 66.909],
[ 67.082, 66.991, 66.847, 66.541, 56.324],
[ 66.717, 56.357, 56.03 , 54.563, 51.98 ]]],
[[[ 15.325, 15.59 , 15.836, 16.062, 16.265],
[ 15.522, 15.744, 15.943, 16.111, 16.24 ],
[ 15.987, 16.144, 16.257, 16.301, 16.152],
[ 16.163, 16.171, 6.061, 5.798, 4.404],
[ 5.832, 5.195, 3.426, 0.527, -3.502]],
[[ 68.016, 68.098, 68.169, 68.228, 68.274],
[ 67.924, 67.981, 68.025, 68.054, 68.067],
[ 68.036, 68.059, 68.066, 68.056, 68.021],
[ 68.061, 68.039, 68. , 67.919, 67.794],
[ 67.921, 67.822, 67.677, 67.366, 57.146]]],
[[[ 16.121, 16.406, 16.678, 16.933, 17.171],
[ 16.372, 16.626, 16.862, 17.078, 17.271],
[ 16.921, 17.13 , 17.314, 17.469, 17.585],
[ 17.251, 17.395, 17.496, 17.527, 17.366],
[ 17.249, 17.246, 7.126, 6.852, 5.447]],
[[ 68.749, 68.846, 68.932, 69.008, 69.074],
[ 68.692, 68.768, 68.832, 68.884, 68.925],
[ 68.85 , 68.898, 68.934, 68.957, 68.965],
[ 68.939, 68.954, 68.961, 68.941, 68.906],
[ 68.901, 68.87 , 68.831, 68.744, 68.617]]]],
[[[[ 12.947, 13.074, 13.112, 3.034, 2.8 ],
[ 12.693, 2.693, 2.087, 0.35 , -2.518],
[ 1.618, -0.496, -3.741, -8.117, -13.624],
[ -5.192, -9.944, -15.827, -22.84 , -30.984],
[-18.306, -25.693, -34.212, -43.861, -54.64 ]],
[[ 65.941, 65.962, 65.961, 65.932, 65.868],
[ 65.688, 65.652, 65.575, 65.435, 65.141],
[ 65.537, 65.364, 55.018, 54.691, 53.236],
[ 55.031, 54.324, 52.497, 49.536, 45.456],
[ 51.579, 48.239, 43.787, 38.195, 31.487]]],
[[[ 13.954, 14.162, 14.337, 14.468, 14.529],
[ 13.994, 14.11 , 14.139, 4.049, 3.806],
[ 14.093, 4.079, 3.459, 1.708, -1.174],
[ 2.882, 0.755, -2.502, -6.891, -12.41 ],
[ -4.106, -8.869, -14.762, -21.786, -29.941]],
[[ 66.789, 66.84 , 66.876, 66.893, 66.891],
[ 66.617, 66.631, 66.623, 66.588, 66.519],
[ 66.614, 66.569, 66.484, 66.339, 66.039],
[ 66.441, 66.259, 55.913, 55.577, 54.122],
[ 55.87 , 55.155, 53.328, 50.361, 46.278]]],
[[[ 14.847, 15.096, 15.323, 15.525, 15.698],
[ 15.001, 15.198, 15.363, 15.484, 15.535],
[ 15.394, 15.496, 15.51 , 5.407, 5.15 ],
[ 15.356, 5.33 , 4.697, 2.934, 0.04 ],
[ 3.968, 1.831, -1.438, -5.837, -11.366]],
[[ 67.582, 67.654, 67.714, 67.761, 67.792],
[ 67.465, 67.509, 67.538, 67.549, 67.542],
[ 67.543, 67.548, 67.532, 67.492, 67.417],
[ 67.518, 67.463, 67.379, 67.224, 66.925],
[ 67.28 , 67.09 , 56.744, 56.402, 54.944]]],
[[[ 15.672, 15.946, 16.204, 16.444, 16.665],
[ 15.894, 16.132, 16.349, 16.541, 16.703],
[ 16.4 , 16.584, 16.734, 16.842, 16.879],
[ 16.657, 16.746, 16.749, 6.633, 6.364],
[ 16.443, 6.405, 5.762, 3.988, 1.083]],
[[ 68.334, 68.423, 68.501, 68.568, 68.623],
[ 68.258, 68.324, 68.377, 68.417, 68.443],
[ 68.391, 68.426, 68.447, 68.453, 68.439],
[ 68.447, 68.443, 68.427, 68.378, 68.302],
[ 68.357, 68.294, 68.209, 68.049, 67.747]]],
[[[ 16.448, 16.742, 17.021, 17.286, 17.535],
[ 16.719, 16.983, 17.23 , 17.46 , 17.67 ],
[ 17.294, 17.517, 17.72 , 17.899, 18.048],
[ 17.664, 17.835, 17.973, 18.068, 18.093],
[ 17.744, 17.822, 17.813, 7.687, 7.408]],
[[ 69.055, 69.156, 69.248, 69.331, 69.403],
[ 69.01 , 69.092, 69.163, 69.224, 69.273],
[ 69.184, 69.241, 69.286, 69.32 , 69.341],
[ 69.295, 69.321, 69.341, 69.339, 69.325],
[ 69.286, 69.274, 69.257, 69.203, 69.125]]]],
[[[[ 13.398, 13.568, 13.688, 13.721, 3.636],
[ 13.32 , 13.303, 3.298, 2.685, 0.942],
[ 3.204, 2.215, 0.095, -3.156, -7.538],
[ -0.982, -4.609, -9.366, -15.255, -22.274],
[-11.47 , -17.733, -25.126, -33.65 , -43.305]],
[[ 66.312, 66.348, 66.364, 66.359, 66.327],
[ 66.099, 66.088, 66.048, 65.967, 65.825],
[ 66.025, 65.928, 65.752, 55.405, 55.075],
[ 65.656, 55.413, 54.711, 52.875, 49.92 ],
[ 54.168, 51.953, 48.626, 44.159, 38.576]]],
[[[ 14.347, 14.575, 14.776, 14.945, 15.07 ],
[ 14.445, 14.605, 14.714, 14.737, 4.642],
[ 14.72 , 14.689, 4.669, 4.043, 2.286],
[ 4.468, 3.466, 1.333, -1.93 , -6.324],
[ 0.104, -3.533, -8.302, -14.201, -21.23 ]],
[[ 67.134, 67.195, 67.243, 67.274, 67.288],
[ 66.988, 67.017, 67.027, 67.015, 66.978],
[ 67.025, 67.005, 66.956, 66.871, 66.722],
[ 66.929, 66.822, 66.646, 56.291, 55.961],
[ 66.496, 56.244, 55.542, 53.7 , 50.742]]],
[[[ 15.208, 15.468, 15.71 , 15.931, 16.128],
[ 15.394, 15.611, 15.802, 15.961, 16.076],
[ 15.844, 15.99 , 16.086, 16.095, 5.986],
[ 15.983, 15.94 , 5.908, 5.269, 3.5 ],
[ 5.554, 4.541, 2.398, -0.876, -5.281]],
[[ 67.909, 67.988, 68.057, 68.113, 68.155],
[ 67.81 , 67.865, 67.905, 67.93 , 67.939],
[ 67.915, 67.934, 67.936, 67.919, 67.875],
[ 67.929, 67.9 , 67.851, 67.756, 67.608],
[ 67.768, 67.653, 67.477, 57.116, 56.783]]],
[[[ 16.01 , 16.293, 16.561, 16.813, 17.047],
[ 16.255, 16.505, 16.736, 16.947, 17.133],
[ 16.794, 16.997, 17.174, 17.319, 17.42 ],
[ 17.108, 17.241, 17.324, 17.321, 7.2 ],
[ 17.07 , 17.015, 6.972, 6.323, 4.544]],
[[ 68.647, 68.741, 68.825, 68.899, 68.962],
[ 68.585, 68.658, 68.719, 68.769, 68.806],
[ 68.737, 68.782, 68.814, 68.834, 68.836],
[ 68.818, 68.829, 68.83 , 68.804, 68.761],
[ 68.768, 68.731, 68.682, 68.581, 68.431]]],
[[[ 16.769, 17.069, 17.356, 17.63 , 17.888],
[ 17.057, 17.329, 17.587, 17.829, 18.053],
[ 17.654, 17.89 , 18.108, 18.305, 18.477],
[ 18.057, 18.248, 18.412, 18.545, 18.634],
[ 18.194, 18.316, 18.389, 18.375, 8.243]],
[[ 69.355, 69.461, 69.559, 69.647, 69.725],
[ 69.323, 69.41 , 69.488, 69.555, 69.613],
[ 69.511, 69.575, 69.628, 69.672, 69.704],
[ 69.64 , 69.677, 69.708, 69.719, 69.722],
[ 69.658, 69.66 , 69.661, 69.63 , 69.584]]]]])

Related

How to use all the elements of the array using for loop?

actually, I need to put the returned values of the function (global_displacement(X)) into another running loop.
can someone please tell me how to obtain the required output?
and what idiotic mistake I have been doing.
as every time it gives me only the first([ 0, 0, X[0], X[1]]) OR
the last value([ X[20], X[21], X[53], X[54]]) in the output,
because of wrong indendation of "return j" in the below written code .
import numpy as np
X = [ 0.19515612, 0.36477665, 0.244737, 0.42873321, 0.16864666, 0.08636661, 0.05376605, -0.57201897, -0.00935055, -1.24923862, 0., -1.53111525, 0.00935055, -1.24923862, -0.05376605, -0.57201897, -0.1686466,
0.08636661, -0.244737, 0.42873321, -0.19515612, 0.36477665, 0.02279911, 0. , 0.3563355 , 0.01379104, 0. , 0.42289958, -0.00747999, 0. , 0.0825908, -0.02949519 , 0. , -0.57435396,
-0.04074819, 0. , -1.25069528 ,-0.02972642, 0. , -1.53227704, -0. , 0. , -1.25069528 , 0.02972642 , 0. , -0.57435396 , 0.04074819 , 0. , 0.0825908, 0.02949519, 0. ,
0.42289958, 0.00747999 , 0. , 0.3563355 , -0.01379104, -0.02279911]
def global_displacement(X):
global_displacements = np.array( [[ 0, 0, X[0], X[1]], [ X[0], X[1], X[2], X[3]], [ X[2], X[3],X[4], X[5]], [ X[4],X[5],X[6], X[7]],[ X[6],X[7],X[8],X[9]], [ X[8],X[9],X[10], X[11] ], [ X[10], X[11],X[12], X[13]], [ X[12], X[13],X[14], X[15]],[ X[14], X[15],X[16], X[17]],[ X[16], X[17],X[18], X[19]], [ X[18], X[19],X[20], X[21]],[ X[20], X[21], 0, 0],
[ X[0], X[1], X[23], X[24]], [ X[2], X[3], X[26],X[27]], [ X[4], X[5], X[29],X[30]], [ X[6], X[7], X[32],X[33]], [ X[8],X[9],X[35], X[36]], [ X[10], X[11], X[38], X[39]], [ X[12], X[13], X[41], X[42]] ,[ X[14], X[15], X[44], X[45]],[ X[16], X[17], X[47], X[48]],[ X[18], X[19], X[50], X[51]], [ X[20], X[21], X[53], X[54]] ] )
for i in (global_displacements):
j = i.reshape(4,1)
return j
print(global_displacement(X))
this is the expected output, and I need to put these values in another loop, by calling this function.
[[0. ]
[0. ]
[0.19515612]
[0.36477665]]
[[0.19515612]
[0.36477665]
[0.244737 ]
[0.42873321]]
[[0.244737 ]
[0.42873321]
[0.16864666]
[0.08636661]]
[[ 0.16864666]
[ 0.08636661]
[ 0.05376605]
[-0.57201897]]
[[ 0.05376605]
[-0.57201897]
[-0.00935055]
[-1.24923862]]
[[-0.00935055]
[-1.24923862]
[ 0. ]
[-1.53111525]]
[[ 0. ]
[-1.53111525]
[ 0.00935055]
[-1.24923862]]
[[ 0.00935055]
[-1.24923862]
[-0.05376605]
[-0.57201897]]
[[-0.05376605]
[-0.57201897]
[-0.1686466 ]
[ 0.08636661]]
[[-0.1686466 ]
[ 0.08636661]
[-0.244737 ]
[ 0.42873321]]
[[-0.244737 ]
[ 0.42873321]
[-0.19515612]
[ 0.36477665]]
[[-0.19515612]
[ 0.36477665]
[ 0. ]
[ 0. ]]
[[0.19515612]
[0.36477665]
[0. ]
[0.3563355 ]]
[[0.244737 ]
[0.42873321]
[0. ]
[0.42289958]]
[[0.16864666]
[0.08636661]
[0. ]
[0.0825908 ]]
[[ 0.05376605]
[-0.57201897]
[ 0. ]
[-0.57435396]]
[[-0.00935055]
[-1.24923862]
[ 0. ]
[-1.25069528]]
[[ 0. ]
[-1.53111525]
[ 0. ]
[-1.53227704]]
[[ 0.00935055]
[-1.24923862]
[ 0. ]
[-1.25069528]]
[[-0.05376605]
[-0.57201897]
[ 0. ]
[-0.57435396]]
[[-0.1686466 ]
[ 0.08636661]
[ 0. ]
[ 0.0825908 ]]
[[-0.244737 ]
[ 0.42873321]
[ 0. ]
[ 0.42289958]]
[[-0.19515612]
[ 0.36477665]
[ 0. ]
[ 0.3563355 ]]
Your function already converts everything into the right format except that the inner values should be stored into a list. For this you can use numpy.newaxis. It is used to add a new dimension to your array (good post about its functionality).
import numpy as np
def global_displacement(X):
global_displacements = np.array( [[ 0, 0, X[0], X[1]], [ X[0], X[1], X[2], X[3]], [ X[2], X[3],X[4], X[5]], [ X[4],X[5],X[6], X[7]],[ X[6],X[7],X[8],X[9]], [ X[8],X[9],X[10], X[11] ], [ X[10], X[11],X[12], X[13]], [ X[12], X[13],X[14], X[15]],[ X[14], X[15],X[16], X[17]],[ X[16], X[17],X[18], X[19]], [ X[18], X[19],X[20], X[21]],[ X[20], X[21], 0, 0],
[ X[0], X[1], X[23], X[24]], [ X[2], X[3], X[26],X[27]], [ X[4], X[5], X[29],X[30]], [ X[6], X[7], X[32],X[33]], [ X[8],X[9],X[35], X[36]], [ X[10], X[11], X[38], X[39]], [ X[12], X[13], X[41], X[42]] ,[ X[14], X[15], X[44], X[45]],[ X[16], X[17], X[47], X[48]],[ X[18], X[19], X[50], X[51]], [ X[20], X[21], X[53], X[54]] ] )
new_structure = global_displacements[:, :, np.newaxis]
return new_structure
X = [ 0.19515612, 0.36477665, 0.244737, 0.42873321, 0.16864666, 0.08636661, 0.05376605, -0.57201897, -0.00935055, -1.24923862, 0., -1.53111525, 0.00935055, -1.24923862, -0.05376605, -0.57201897, -0.1686466,
0.08636661, -0.244737, 0.42873321, -0.19515612, 0.36477665, 0.02279911, 0. , 0.3563355 , 0.01379104, 0. , 0.42289958, -0.00747999, 0. , 0.0825908, -0.02949519 , 0. , -0.57435396,
-0.04074819, 0. , -1.25069528 ,-0.02972642, 0. , -1.53227704, -0. , 0. , -1.25069528 , 0.02972642 , 0. , -0.57435396 , 0.04074819 , 0. , 0.0825908, 0.02949519, 0. ,
0.42289958, 0.00747999 , 0. , 0.3563355 , -0.01379104, -0.02279911]
result = global_displacement(X)
print(result)
Output:
[[[ 0. ]
[ 0. ]
[ 0.19515612]
[ 0.36477665]]
[[ 0.19515612]
[ 0.36477665]
[ 0.244737 ]
[ 0.42873321]]
[[ 0.244737 ]
[ 0.42873321]
[ 0.16864666]
[ 0.08636661]]
[[ 0.16864666]
[ 0.08636661]
[ 0.05376605]
[-0.57201897]]
[[ 0.05376605]
[-0.57201897]
[-0.00935055]
[-1.24923862]]
[[-0.00935055]
[-1.24923862]
[ 0. ]
[-1.53111525]]
[[ 0. ]
[-1.53111525]
[ 0.00935055]
[-1.24923862]]
[[ 0.00935055]
[-1.24923862]
[-0.05376605]
[-0.57201897]]
[[-0.05376605]
[-0.57201897]
[-0.1686466 ]
[ 0.08636661]]
[[-0.1686466 ]
[ 0.08636661]
[-0.244737 ]
[ 0.42873321]]
[[-0.244737 ]
[ 0.42873321]
[-0.19515612]
[ 0.36477665]]
[[-0.19515612]
[ 0.36477665]
[ 0. ]
[ 0. ]]
[[ 0.19515612]
[ 0.36477665]
[ 0. ]
[ 0.3563355 ]]
[[ 0.244737 ]
[ 0.42873321]
[ 0. ]
[ 0.42289958]]
[[ 0.16864666]
[ 0.08636661]
[ 0. ]
[ 0.0825908 ]]
[[ 0.05376605]
[-0.57201897]
[ 0. ]
[-0.57435396]]
[[-0.00935055]
[-1.24923862]
[ 0. ]
[-1.25069528]]
[[ 0. ]
[-1.53111525]
[ 0. ]
[-1.53227704]]
[[ 0.00935055]
[-1.24923862]
[ 0. ]
[-1.25069528]]
[[-0.05376605]
[-0.57201897]
[ 0. ]
[-0.57435396]]
[[-0.1686466 ]
[ 0.08636661]
[ 0. ]
[ 0.0825908 ]]
[[-0.244737 ]
[ 0.42873321]
[ 0. ]
[ 0.42289958]]
[[-0.19515612]
[ 0.36477665]
[ 0. ]
[ 0.3563355 ]]]
First off, you don't need .reshape to transform a 1D array of N elements into a 2D array that's N by 1. You can just add a dimension to the array.
Second, you generally don't want to write loops to handle a Numpy array. You want to use Numpy tools to process everything at once. Just think about the problem in the full number of dimensions: you want to transform a 2D array that's M by N, into a 3D one that's M by N by 1. That's... still just adding a dimension to the array.
So:
global_displacements = np.array(...)
return global_displacements[..., np.newaxis]

Single value decomposition(SVD) issue on 3D data (Python)

I have a trajectory which contains several frames of some 3D data, which looks like the following (I am posting the whole frame for the sake of reproducibilty of my problem):
data1= [[ 89.29, 57.35 , 107.81999 ],
[ 91.37 , 60.39 , 109.119995],
[ 89.130005 , 61.289997 , 112.12999 ],
[ 89.45 , 57.729996 ,113.509995],
[ 93.149994 , 58.199997 ,114.20999 ],
[ 92.03999 , 61.21 ,116.44 ],
[ 89.48999 , 58.71 ,117.979996],
[ 92.42 , 56.399998 ,118.84 ],
[ 94.25 , 59.14 ,120.73999 ],
[ 91.44 , 59.62 ,123.28999 ],
[ 91.25 , 55.739998 ,124. ],
[ 94.95 , 55.829998 ,124.67 ],
[ 94.36 , 58.699997 ,127.079994],
[ 91.5 , 57.05 ,129.05 ],
[ 93.16 , 53.57 ,129.28 ],
[ 96.36 , 55.35 ,130.5 ],
[ 94.15 , 57.53 ,133. ],
[ 92.24 , 54.42 ,134.18 ],
[ 95.479996 , 52.35 ,134.88 ],
[ 96.81 , 55.429996 ,136.77 ],
[ 93.509995 , 55.73 ,138.76 ],
[ 94.06999 , 51.98 ,139.5 ],
[ 97.63 , 52.929996 ,140.72 ],
[ 96.08 , 55.72 ,142.92 ],
[ 93.63999 , 53.269997 ,144.65 ],
[ 96.149994 , 50.45 ,144.79999 ],
[ 99.10999 , 52.1 ,146.4 ],
[ 97.369995 , 54.16 ,149. ],
[ 94.2 , 55.65 ,150.56 ]]
I want to fit the line to my data. After checking answers of previous question (1), I borrowed/wrote the following code :
def Range(data):
x=[]
y=[]
for i in range(0, len(data.T)):
x.append(np.mean(data.T[i])- np.std(data.T[i]))
y.append(np.mean(data.T[i]) + np.std(data.T[i]))
normx,normy=np.linalg.norm(x),np.linalg.norm(y)
minimum=normx-(normx+normy)/2
maximum=normy-(normx+normy)/2
return(minimum,maximum)
def axis(data):
minimum,maximum=Range(data)
datamean = data.mean(axis=0)
uu, dd, vv = np.linalg.svd(data - datamean)
linepts = vv[0] * np.mgrid[minimum:maximum:2j][:, np.newaxis]
linepts += datamean
return(linepts)
The value of vv[0] turns out to be [ 0.15970461,-0.17264067,0.9719515 ], which looks perfect for my further usage. However, for following the data :
data2= [ 90.993996 , 62.075 ,108.487 ],
[ 88.036995 , 59.8525 ,109.3855 ],
[ 90.5945 , 57.614998 ,111.061005],
[ 92.17 , 60.6205 ,112.681496],
[ 88.934006 , 61.622 ,114.4255 ],
[ 88.077995 , 57.929 ,115.34 ],
[ 91.642 , 57.3 ,116.81049 ],
[ 91.431496 , 60.4655 ,118.813 ],
[ 88.269 , 59.22 ,120.685 ],
[ 89.883995 , 55.7975 ,121.2585 ],
[ 93.115 , 57.497 ,122.68849 ],
[ 91.090004 , 59.724 ,125.11 ],
[ 89.355 , 56.712498 ,126.7305 ],
[ 92.6985 , 54.758 ,126.52 ],
[ 94.4685 , 57.383 ,128.4515 ],
[ 91.9065 , 57.297997 ,131.2145 ],
[ 91.99349 , 53.578995 ,131.1695 ],
[ 95.752495 , 53.376 , 132.0325 ],
[ 95.24799 , 55.990997, 134.80699 ],
[ 92.29199 , 54.0455 , 136.277 ],
[ 94.5055 , 50.9205 , 136.68399 ],
[ 97.5085 , 52.947 , 137.85399 ],
[ 95.353 , 54.6695 , 140.651 ],
[ 94.194496 , 51.2645 , 141.4345 ],
[ 97.6015 , 49.722 , 141.7245 ],
[ 99.26149 , 52.813496 ,143.35449 ],
[ 96.79849 , 53.233498, 146.2645 ],
[ 96.237 , 49.554 , 146.97299 ]]
The value of vv[0] turns out to be [-0.18894662 , 0.24432637,-0.9511066 ], which is in opposite sign from my expected result. I am unsure why it would produce such a result. Could anyone help me sort out this issue?
1 Fitting a line in 3D
The first principal component is a direction. Think of it as a line of infinite length. It doesn't matter if you flip this line, the direction remains the same.
In other words: the algorithm does not consider the order of the points that the object has travelled to. So for the algorithm, the object flying backwards through the points is just as valid as flying forwards.

How do I write an accumulator to squash an array around a midpoint?

Say I've got an array of arrays as follows:
array([[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]])
This is a list of prices and quantities, with the positive numbers being buys and the negative numbers being sells.
If the current market price is 7050, I want to 'squash' my prices, as follows:
array([[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7050. , 300. ], # Price & quantity changed here
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]])
and and do the same in thing in the other direction, so if the market price were 7085:
array([[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7085. , -300. ], # Price & quantity change here
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]])
What's the cleanest way to write such a thing? Ideally, I'd like to write the squashing and the quantity grouping as two separate steps.
This is the kind of operations that are made easier by pandas.
>>> import numpy as np
>>> import pandas as pd
>>>
>>> t = np.array([[ 7108.4, -600. ],
... [ 7101.3, -500. ],
... [ 7094.3, -400. ],
... [ 7087.2, -300. ],
... [ 7080.1, -200. ],
... [ 7073.1, -100. ],
... [ 7058.3, 100. ],
... [ 7051.3, 200. ],
... [ 7044.2, 300. ],
... [ 7037.1, 400. ],
... [ 7030.1, 500. ],
... [ 7023. , 600. ]])
>>>
>>> df = pd.DataFrame(t, columns=['price', 'quantity'])
>>> (df.price > 7050) & (df.quantity > 0)
0 False
1 False
2 False
3 False
4 False
5 False
6 True
7 True
8 False
9 False
10 False
11 False
dtype: bool
>>> df[(df.price > 7050) & (df.quantity > 0)]
price quantity
6 7058.3 100.0
7 7051.3 200.0
>>> df[(df.price > 7050) & (df.quantity > 0)].quantity.sum()
300.0
>>> df[(df.price < 7085) & (df.quantity < 0)]
price quantity
4 7080.1 -200.0
5 7073.1 -100.0
>>> df[(df.price < 7085) & (df.quantity < 0)].quantity.sum()
-300.0
You could still stick with numpy if you want:
>>> import numpy as np
>>> import pandas as pd
>>>
>>> t = np.array([[ 7108.4, -600. ],
... [ 7101.3, -500. ],
... [ 7094.3, -400. ],
... [ 7087.2, -300. ],
... [ 7080.1, -200. ],
... [ 7073.1, -100. ],
... [ 7058.3, 100. ],
... [ 7051.3, 200. ],
... [ 7044.2, 300. ],
... [ 7037.1, 400. ],
... [ 7030.1, 500. ],
... [ 7023. , 600. ]])
>>>
>>>
>>> t[:,0]
array([ 7108.4, 7101.3, 7094.3, 7087.2, 7080.1, 7073.1, 7058.3,
7051.3, 7044.2, 7037.1, 7030.1, 7023. ])
>>> t[:,0] > 7050
array([ True, True, True, True, True, True, True, True, False,
False, False, False], dtype=bool)
>>> (t[:,0] > 7050) & (t[:,1] > 0)
array([False, False, False, False, False, False, True, True, False,
False, False, False], dtype=bool)
>>> t[(t[:,0] > 7050) & (t[:,1] > 0)]
array([[ 7058.3, 100. ],
[ 7051.3, 200. ]])
>>> t[(t[:,0] < 7085) & (t[:,1] < 0)]
array([[ 7080.1, -200. ],
[ 7073.1, -100. ]])
>>> t[(t[:,0] < 7085) & (t[:,1] < 0)][:,1]
array([-200., -100.])
>>> t[(t[:,0] < 7085) & (t[:,1] < 0)][:,1].sum()
-300.0
since Eric answer is great but as per your comment :
Is possible to modify the original array, so that the output is the
full list of orders with changes inside?
Here is my approach without numpy :
array=[[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]]
price=0
positive_value=7050
updated_list=[]
track=[]
for index,item in enumerate(array):
if item[0]>positive_value:
if item[1]>0:
price+=item[1]
track.append(index)
else:
updated_list.append(item)
else:
updated_list.append(item)
updated_list.insert(track[0],[positive_value,price])
print(updated_list)
output:
[[7108.4, -600.0], [7101.3, -500.0], [7094.3, -400.0], [7087.2, -300.0], [7080.1, -200.0], [7073.1, -100.0], [7050, 300.0], [7044.2, 300.0], [7037.1, 400.0], [7030.1, 500.0], [7023.0, 600.0]]
for second one:
array=[[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]]
price=0
positive_value=7085
updated_list=[]
track=[]
for index,item in enumerate(array):
if item[0] < positive_value:
if item[1] < 0:
price+=item[1]
track.append(index)
else:
updated_list.append(item)
else:
updated_list.append(item)
updated_list.insert(track[0],[positive_value,price])
print(updated_list)
output:
[[7108.4, -600.0], [7101.3, -500.0], [7094.3, -400.0], [7087.2, -300.0], [7085, -300.0], [7058.3, 100.0], [7051.3, 200.0], [7044.2, 300.0], [7037.1, 400.0], [7030.1, 500.0], [7023.0, 600.0]]

How to get random forest regression performance output in Python like that produced in R?

In R, I can easily get the performance of a random forest like the following.
How can I get the similar stuff in Python easily? Thanks a lot.
Summary of the Random Forest Model
==================================
Number of observations used to build the model: 35
Missing value imputation is active.
Call:
randomForest(formula = rank ~ .,
data = crs$dataset[crs$sample, c(crs$input, crs$target)],
ntree = 500, mtry = 3, importance = TRUE, replace = FALSE, na.action = na.roughfix)
Type of random forest: regression
Number of trees: 500
No. of variables tried at each split: 3
Mean of squared residuals: 5.578147
% Var explained: 97.22
Variable Importance
Here there is a simple example using sklearn random forest . http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier
You can easily get the values that you are looking for.
In this example we are using an input matrix X with 2 variables and a binary output y.
from sklearn.ensemble import RandomForestClassifier
fr = RandomForestClassifier(n_estimators=100, oob_score=True).fit(X, y)
fr.n_estimators
Out[10]: 100
fr.oob_decision_function_
Out[11]:
array([[ 0.14285714, 0.85714286],
[ 0.86666667, 0.13333333],
[ 0.02631579, 0.97368421],
[ 1. , 0. ],
[ 0.97826087, 0.02173913],
[ 0.97826087, 0.02173913],
[ 0.20512821, 0.79487179],
[ 0.97368421, 0.02631579],
[ 0.77777778, 0.22222222],
[ 0. , 1. ],
[ 0. , 1. ],
[ 1. , 0. ],
[ 0.52380952, 0.47619048],
[ 0.43243243, 0.56756757],
[ 1. , 0. ],
[ 0. , 1. ],
[ 0.05714286, 0.94285714],
[ 0. , 1. ],
[ 1. , 0. ],
[ 0.76470588, 0.23529412],
[ 1. , 0. ],
[ 0. , 1. ],
[ 0.95454545, 0.04545455],
[ 0.9 , 0.1 ],
[ 0.02222222, 0.97777778],
[ 0.875 , 0.125 ],
[ 0.02857143, 0.97142857],
[ 1. , 0. ],
[ 0.58823529, 0.41176471],
[ 0. , 1. ],
[ 0.20512821, 0.79487179],
[ 0.97435897, 0.02564103],
[ 0.91176471, 0.08823529],
[ 0. , 1. ],
[ 0.30232558, 0.69767442],
[ 1. , 0. ],
[ 0.94444444, 0.05555556],
[ 0. , 1. ],
[ 0.075 , 0.925 ],
[ 0.05263158, 0.94736842],
[ 1. , 0. ],
[ 0. , 1. ],
[ 0.02702703, 0.97297297],
[ 0.91176471, 0.08823529],
[ 0.43243243, 0.56756757],
[ 0.08333333, 0.91666667],
[ 0.10526316, 0.89473684],
[ 0.93548387, 0.06451613],
[ 0.02857143, 0.97142857],
[ 0.53658537, 0.46341463],
[ 0.5 , 0.5 ],
[ 0.66666667, 0.33333333],
[ 1. , 0. ],
[ 0.55555556, 0.44444444],
[ 0.96666667, 0.03333333],
[ 0.97142857, 0.02857143],
[ 0. , 1. ],
[ 0. , 1. ],
[ 1. , 0. ],
[ 0.05882353, 0.94117647],
[ 0.94594595, 0.05405405],
[ 0.11904762, 0.88095238],
[ 0.92307692, 0.07692308],
[ 0.69767442, 0.30232558],
[ 1. , 0. ],
[ 0.12121212, 0.87878788],
[ 1. , 0. ],
[ 0.97727273, 0.02272727],
[ 1. , 0. ],
[ 0.87878788, 0.12121212],
[ 0.02380952, 0.97619048],
[ 0. , 1. ],
[ 0. , 1. ],
[ 0. , 1. ],
[ 0.10810811, 0.89189189],
[ 1. , 0. ],
[ 1. , 0. ],
[ 0.97619048, 0.02380952],
[ 0.54545455, 0.45454545],
[ 0.02380952, 0.97619048],
[ 0.07317073, 0.92682927],
[ 0.94285714, 0.05714286],
[ 0.25714286, 0.74285714],
[ 0. , 1. ],
[ 0. , 1. ],
[ 0.97560976, 0.02439024],
[ 0.11111111, 0.88888889],
[ 1. , 0. ],
[ 1. , 0. ],
[ 0.02857143, 0.97142857],
[ 0.97916667, 0.02083333],
[ 0. , 1. ],
[ 0.02564103, 0.97435897],
[ 0. , 1. ],
[ 0.32258065, 0.67741935],
[ 0.56410256, 0.43589744],
[ 1. , 0. ],
[ 0.92682927, 0.07317073],
[ 1. , 0. ],
[ 0.08823529, 0.91176471]])
fr.oob_score_
Out[12]: 0.87
fr.feature_importances_
Out[13]: array([ 0.82407373, 0.17592627])

List of all arrays not contained in other lists of arrays [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have a list of two dimensional points, represented as two element long lists/arrays. E.g.:
points =
[[ 10. , 10. ],
[ 11. , 10. ],
[ 10.5 , 9.1339746],
[ 10.5 , 10. ],
[ 10.75 , 9.5669873],
[ 10.25 , 9.5669873],
[ 2. , 2. ],
[ 3. , 2. ],
[ 2.5 , 1.1339746],
[ 2.5 , 2. ],
[ 2.75 , 1.5669873],
[ 2.25 , 1.5669873]]
I now want to have a list which does not contain certain elements of this first list.
exclude = [[2., 2.], [3., 2.], [2.5, 2.]]
Unfortunately
new_list = [p for p in points if p not in exclude]
will produce
[[ 10. , 10. ],
[ 11. , 10. ],
[ 10.5 , 9.1339746],
[ 10.5 , 10. ],
[ 10.75 , 9.5669873],
[ 10.25 , 9.5669873],
[ 2.75 , 1.5669873],
[ 2.25 , 1.5669873]]
instead of
[[ 10. , 10. ],
[ 11. , 10. ],
[ 10.5 , 9.1339746],
[ 10.5 , 10. ],
[ 10.75 , 9.5669873],
[ 10.25 , 9.5669873],
[ 2.5 , 1.1339746],
[ 2.75 , 1.5669873],
[ 2.25 , 1.5669873]]
It seems Python removes all elements here that have at least one element in common (and not all in common :/ ).
Is there any nice/short/elegant way to exclude elements if they are not completely contained in the first list?
Note: Since this question has been tagged numpy, I'm assuming points is a NumPy array. If that's true, you could generate a boolean mask (array) using np.logical_and and np.logical_or:
import numpy as np
points = np.array(
[[ 10. , 10. ],
[ 11. , 10. ],
[ 10.5 , 9.1339746],
[ 10.5 , 10. ],
[ 10.75 , 9.5669873],
[ 10.25 , 9.5669873],
[ 2. , 2. ],
[ 3. , 2. ],
[ 2.5 , 1.1339746],
[ 2.5 , 2. ],
[ 2.75 , 1.5669873],
[ 2.25 , 1.5669873]])
exclude = [[2., 2.], [3., 2.], [2.5, 2.]]
mask = np.logical_or.reduce(
[np.logical_and.reduce(
[points[:,idx] == ex[idx] for idx in range(len(ex))]) for ex in exclude])
new_points = points[~mask]
print(new_points)
prints
[[ 10. 10. ]
[ 11. 10. ]
[ 10.5 9.1339746]
[ 10.5 10. ]
[ 10.75 9.5669873]
[ 10.25 9.5669873]
[ 2.5 1.1339746]
[ 2.75 1.5669873]
[ 2.25 1.5669873]]
You can also view the 2D array as a 1D array and then use np.in1d.
#Using #unutbu array's.
def view_1d(arr):
return arr.view(np.dtype((np.void,arr.dtype.itemsize * arr.shape[1])))
points_1d=view_1d(points)
exclude_1d=view_1d(exclude)
print points[~np.in1d(points_1d,exclude_1d)]
[[ 10. 10. ]
[ 11. 10. ]
[ 10.5 9.1339746]
[ 10.5 10. ]
[ 10.75 9.5669873]
[ 10.25 9.5669873]
[ 2.5 1.1339746]
[ 2.75 1.5669873]
[ 2.25 1.5669873]]
Just to double check the trickery is working and some ballpark timings:
points=np.random.rand(1E6,2)
points=np.around(points,1)
exclude=np.random.rand(1E2,2)
exclude=np.around(exclude,1)
t = time.time()
mask1 = ~(np.in1d(view_1d(points),view_1d(exclude)))
print time.time()-t
#0.469238996506
t = time.time()
mask2 = ~np.logical_or.reduce(
[np.logical_and.reduce(
[points[:,idx] == ex[idx] for idx in range(len(ex))]) for ex in exclude])
print time.time()-t
#7.13628792763
#Just to check this is doing what I think its doing.
print np.all(mask1==mask2)
True
Timings are just for generating the mask. Both methods appear to scale similarly, I just showed large arrays to (hopefully) compensate for not using timeit.
Your code as posted is not syntactically valid.
It produces (once corrected) your desired result:
--
[[10.0, 10.0],
[11.0, 10.0],
[10.5, 9.1339746],
[10.5, 10.0],
[10.75, 9.5669873],
[10.25, 9.5669873],
[2.5, 1.1339746],
[2.75, 1.5669873],
[2.25, 1.5669873]]
See: http://ideone.com/7LOpa6
points = [[ 10. , 10. ],
[ 11. , 10. ],
[ 10.5 , 9.1339746],
[ 10.5 , 10. ],
[ 10.75 , 9.5669873],
[ 10.25 , 9.5669873],
[ 2. , 2. ],
[ 3. , 2. ],
[ 2.5 , 1.1339746],
[ 2.5 , 2. ],
[ 2.75 , 1.5669873],
[ 2.25 , 1.5669873]]
exclude = [[2., 2.], [3., 2.], [2.5, 2.]]
print [p for p in points if p not in exclude]

Categories

Resources