I am stacking point clouds into one array using np.stack but when i see the result the values are shown in e notation. if i only stack x and y then the values are not shown in e notation only if i add the z column its shown in e notation
for stacking two values
import laspy
import numpy as np
las = laspy.read("f:\\lidar\\pointcloud.laz")
arr = np.stack([las.x, las.y], axis=0).transpose((1, 0))
arr
array([[ 368230.825, 5807507.866],
[ 368231.821, 5807508.151],
[ 368232.935, 5807508.114],
...,
[ 368496.193, 5807742.345],
[ 368495.747, 5807741.916],
[ 368495.412, 5807742.06 ]])
values when stacking with z values.
import laspy
import numpy as np
las = laspy.read("f:\\lidar\\pointcloud.laz")
arr = np.stack([las.x, las.y,las.z], axis=0).transpose((1, 0))
arr
array([[3.68230825e+05, 5.80750787e+06, 3.10790000e+01],
[3.68231821e+05, 5.80750815e+06, 3.11140000e+01],
[3.68232935e+05, 5.80750811e+06, 3.11320000e+01],
...,
[3.68496193e+05, 5.80774234e+06, 3.33270000e+01],
[3.68495747e+05, 5.80774192e+06, 3.29020000e+01],
[3.68495412e+05, 5.80774206e+06, 3.13750000e+01]])
Related
Consider the following vector:
import numpy as np
u = np.random.randn(5)
print(u)
[-0.30153275 -1.48236907 -1.09808763 -0.10543421 -1.49627068]
When we print its shape:
print(u.shape)
(5,)
I was told this is neither a column vector nor a row vector. So what is essentially this shape is in numpy (m,) ?
# one-dimensional array (rank 1 array)
# array([ 0.202421 , 1.04496629, -0.28473552, 0.22865349, 0.49918827])
a = np.random.randn(5,) # or b = np.random.randn(5)
# column vector (5 x 1)
# array([[-0.52259951],
# [-0.2200037 ],
# [-1.07033914],
# [ 0.9890279 ],
# [ 0.38434068]])
c = np.random.randn(5,1)
# row vector (1 x 5)
# array([[ 0.42688689, -0.80472245, -0.86294221, 0.28738552, -0.86776229]])
d = np.random.randn(1,5)
For example (see docs):
numpy.dot(a, b)
If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).
If both a and b are 2-D arrays, it is matrix multiplication
I want to sum the values in vals into elements of a smaller array a specified in an index list idx.
import numpy as np
a = np.zeros((1,3))
vals = np.array([1,2,3,4])
idx = np.array([0,1,2,2])
a[0,idx] += vals
This produces the result [[ 1. 2. 4.]] but I want the result [[ 1. 2. 7.]], because it should add the 3 from vals and 4 from vals into the 2nd element of a.
I can achieve what I want with:
import numpy as np
a = np.zeros((1,3))
vals = np.array([1,2,3,4])
idx = np.array([0,1,2,2])
for i in np.unique(idx):
fidx = (idx==i).astype(int)
psum = (vals * fidx).sum()
a[0,i] = psum
print(a)
Is there a way to do this with numpy without using a for loop?
Possible with np.add.at as long as the shapes align, i.e., a will need to be 1D here.
a = a.squeeze()
np.add.at(a, idx, vals)
a
array([1., 2., 7.])
my code is:
import numpy as np
import scipy.io as spio
x=np.zeros((22113,1),float)
x= spio.loadmat('C:\\Users\\dell\\Desktop\\Rabia Ahmad spring 2016\\'
'FYP\\1. Matlab Work\\record work\\kk.mat')
print(x)
x = np.reshape(len(x),1);
h = np.array([0.9,0.3,0.1],float)
print(h)
h = h.reshape(len(h),1);
dd = np.convolve(h,x)
and the error I encounter is "ValueError: object too deep for desired array"
kindly help me in this reguard.
{'__globals__': [], '__version__': '1.0', 'ans': array([[ 0.13580322,
0.13580322], [ 0.13638306, 0.13638306], [ 0.13345337, 0.13345337],
..., [ 0.13638306, 0.13638306], [ 0.13345337, 0.13345337], ..., [
0.13638306, 0.13638306], [ 0.13345337, 0.13345337], ..., [-0.09136963,
-0.09136963], [-0.12442017, -0.12442017], [-0.15542603, -0.15542603]])}
See {}? That means x from the loadmat is a dictionary.
x['ans'] will be an array
array([[ 0.13580322,
0.13580322], [ 0.13638306, 0.13638306], [ 0.13345337, 0.13345337],...]])
which, if I count the [] right is a (n,2) array of floats.
The following line does not make sense:
x = np.reshape(len(x),1);
I suspect you mean x = x.reshape(...) as you do with h. But that would give an error with the dictionary x.
When you say the shape of x is (9,) and its dtype is uint16 - where in your code you verifying that?
x = np.reshape(len(x),1); doesn't do anything useful. That completely discards the data in x, and creates an array of shape (1,), with the only element being len(x).
In your code, you reshape h to (3, 1), which is a 2D array, not a 1D array, which is why convolve complains.
Remove both of your reshapes, and instead just pass squeeze=True to scipy.io.loadmat - this is needed because matlab does not have the concept as 1d arrays, and squeeze tells scipy to try and flatten (N, 1) and (1, N) arrays to (N,) arrays
I am trying to do something very simple, but confused by the abundance of information about sparse matrices and vectors in Python.
I want to create two vectors, x and y, one of length 5 and one of length 6, being sparse. Then I want to set one coordinate in each one of them. Then I want to create a matrix A, sparse, which is 5 x 6 and add to it the outer product between x and y. I then want to do SVD on that A.
Here is what I tried, and it goes wrong in many ways.
from scipy import sparse;
import numpy as np;
import scipy.sparse.linalg as ssl;
x = sparse.bsr_matrix(np.zeros(5));
x[1] = 1;
y = sparse.bsr_matrix(np.zeros(6));
y[1] = 2;
A = sparse.coo_matrix(5, 6);
A = A + np.outer(x,y.transpose())
svdresult = ssl.svds(A,1);
At first, you should determine data you want to store in sparse matrix before constructing it. Otherwise you should use sparse.csc_matrix or sparse.csr_matrix instead. Then you can assign or change data like this:
x[0, 1] = 1
At second, outer product of vectors x and y is equivalent to x.transpose() * y.
Here is working code:
from scipy import sparse
import numpy as np
import scipy.sparse.linalg as ssl
x = np.zeros(5)
x[1] = 1
x_bsr = sparse.bsr_matrix(x)
y = np.zeros(6)
y[1] = 2
y_bsr = sparse.bsr_matrix(y)
A = sparse.coo_matrix((5, 6)) # Sparse matrix 5 x 6
B = x_bsr.transpose().dot(y_bsr) # Outer product of x and y
svdresult = ssl.svds((A + B), 1)
Output:
(array([[ 5.55111512e-17],
[ -1.00000000e+00],
[ 0.00000000e+00],
[ -2.77555756e-17],
[ 1.11022302e-16]]), array([ 2.]), array([[ 0., -1., 0., 0., 0., 0.]]))
I have this numpy array
X = [[ -9.03525007 7.45325017 33.34074879][ -6.63700008 5.13299996 31.66075039][ -5.12724996 8.25149989 30.92599964][ -5.12724996 8.25149989 30.92599964]]
I want to get the norm of this array using numpy. How can I do that?
for every array inside, I need sqrt(x2+y2+z2), so my output wull be array of 4 values (since there are 4 inside arrays)
To get what you ask for (the 2-norm of each row in your array), you can use the axis argument to numpy.linalg.norm:
import numpy
x = numpy.array([[ -9.03525007, 7.45325017, 33.34074879],
[ -6.63700008, 5.13299996, 31.66075039],
[ -5.12724996, 8.25149989, 30.92599964],
[ -5.12724996, 8.25149989, 30.92599964]])
print numpy.linalg.norm(x, axis=1)
=>
array([ 35.33825423, 32.75363451, 32.41594355, 32.41594355])
Why don't use numpy.linalg.norm
import numpy
x = [[ -9.03525007, 7.45325017 , 33.34074879], [ -6.63700008 , 5.13299996 , 31.66075039], [ -5.12724996 , 8.25149989 , 30.92599964], [ -5.12724996 , 8.25149989 , 30.92599964]]
print numpy.linalg.norm(x)
Output:
66.5069889437
Did you mean matrix norm(s)? If so:
import numpy as np
>>> xs = [[ -9.03525007, 7.45325017, 33.34074879], [-6.63700008, 5.13299996, 31.66075039], [-5.12724996, 8.25149989, 30.92599964], [-5.12724996, 8.25149989, 30.92599964]]
>>> np.linalg.norm(xs)
66.506988943656381
See: http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html
Other people have already given you the norm() function. You are probably looking to map() the norm() function within the array.
Just do:
from numpy.linalg import norm
norms = map(norm, x)