Processing different data through same equation - python

I have an array that looks like this:
A=[ id. r d ]
[[ 47. 223.25190261 58.0551391 ]
[ 49. 223.25102751 58.05662719]
[ 57. 223.25013049 58.05139459]]
The first column isnt important. The following two are though, they are coordinates.
I have to compare EACH set of coordinates (column 2 and 3 together) against these coordinates:
(223.251, 58.05) by using this equation: B=sin(D)sin(d) + cos(D)cos(d)cos(R-r).
Where (R,D) are the original coordinates (223.251, 58.05) and (r,d) are the coordinates in the array.
How do I do this for each set of coordinates in the array without having to input the numbers myself or having to define each number and replace them with the next set of coordinates? I want the program to obviously keep (R,D) consistent and change (r,d) for each row and make the calculations. After it's done making the calculation for each row I want to have them output. I really have no idea how to do this, I'm thinking maybe something with a loop. I'm seriously lost.
The end of the code is this:
B=(((sin(dec0))*(sin(dec1)) + (cos(dec0)*cos(dec1))*(cos(ra0-ra1))))
print B
0.540302302454
But this only does the first row of coordinates, I want it to be done manually

I'm not sure if the formula is correct and data representative, because your values are really close to each other. Anyway, to print the B value for each item in your data, you can use:
from math import radians, sin, cos
orig = (223.251, 58.05)
R = radians(orig[0])
D = radians(orig[1])
A = [[ 47., 223.25190261, 58.0551391 ],
[ 49., 223.25102751, 58.05662719],
[ 57., 223.25013049, 58.05139459]]
for item in A:
r = radians(item[1])
d = radians(item[2])
B = sin(D)*sin(d) + cos(D)*cos(d)*cos(R-r)
print(B)
if you've got numpy array as input, use numpy module instead of math of course.

If you are willing to use NumPy you the operations can be vectorized avoiding the for loops like:
from numpy import array, deg2rad, sin, cos
orig = (223.251, 58.05)
R = deg2rad(orig[0])
D = deg2rad(orig[1])
A = array([[47., 223.25190261, 58.0551391 ],
[49., 223.25102751, 58.05662719],
[57., 223.25013049, 58.05139459]])
r = deg2rad(A[:,1])
d = deg2rad(A[:,2])
B = sin(D)*sin(d) + cos(D)*cos(d)*cos(R-r)
where B is a numpy.ndarray containing the result for each line of A.

Related

trying to seperate real and imaginary but it gets add up

I have given a set of array where it contains real and imaginary values . I need to separate them and print it out . but its not working out . the output gives
[3. +0.j 4.5+0.j 0. +0.j]
import numpy as np
array = np.array([3,4.5,3+5j,0])
real = np.isreal(array)
print(array[real])
img = np.iscomplex(array)
print(array[img])
Referring to numpy documentation you should do the following:
print(array.real)
print(array.imag)
I guess what you are looking is to print the real numbers if it does not have any imaginary number. If it has imaginary part, then just print the imaginary part.
import numpy as np
array = np.array([3,4.5,3+5j,0])
real = np.isreal(array)
print(array[real].real)
img = np.iscomplex(array)
print(array[img].imag)
# output
[ 3. 4.5 0. ]
[5.]
is this right?
import numpy as np
array = np.array([3,4.5,3+5j,0, 12.5])
real = np.isreal(array)
#here I check to prevent round number and just cast number like 12.0 1.0 to int
print([int(i) if str(i).rsplit(".", 1)[-1] == '0' else i for i in array[real].real ])
img = np.iscomplex(array)
print([complex(int(i.real),i.imag) for i in array[img]])
output:
[3, 4.5, 0, 12.5]
[(3+5j)]
I just append 12.5 for test to see how it's working!

Conversion of list into array , and extracting specific co-ordinates from the converted array

I have an output as a list on which I need to perform some operations. For that, I converted into an array using np.asarrayfunction .
My list
[[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
-5.41511240e+01, -2.30953821e+01, -1.74871288e+01,
-4.91000564e+01, 2.44382720e+02, 1.71213983e+02,
-1.54755310e+02, 5.97881714e+02, 3.52366218e+02,
-1.15560633e+01, 7.42149725e+02, 1.66477287e+02,
-1.18447102e+01, 7.36763064e+02, 1.65182437e+02,
5.41502771e+01, 2.30950820e+01, 1.74870470e+01,
-1.71258190e+01, 3.26592894e+02, 2.03220778e+02,
-1.41130065e+02, 7.04033786e+02, 3.84201614e+02,
-1.12642400e+01, 7.48636864e+02, 1.66665977e+02,
-1.14090840e+01, 7.36435064e+02, 1.63713810e+02,
1.21660845e-03, -8.60110629e-02, -1.93000576e-02,
1.57141460e+01, -2.34399996e+02, -2.86722926e+01,
5.28252697e+01, -4.40469167e+02, -1.11653705e+02,
1.03085631e+02, -5.01280352e+02, -1.93111585e+02,
7.16011844e+01, -5.88214725e+02, -2.18615940e+02,
5.67537804e+00, -4.35088906e+02, -9.76974016e+01,
7.71909591e+01, -3.88738749e+02, -6.29099586e+01,
-2.99970496e+01, -2.25985794e+02, 6.41590789e+01,
1.03847001e+02, -7.32419021e+01, 1.04802558e+02,
1.26585822e+00, -1.20170579e+02, -2.82526049e+01,
1.57900698e+00, -1.51780249e+02, -3.52080548e+01,
8.84543993e-01, -1.07795356e+02, -2.56307189e+01,
8.84543993e-01, -1.07795356e+02, -2.56307189e+01,
5.67537804e+00, -4.35088906e+02, -9.76974016e+01,
8.01141013e+00, -4.16078607e+02, -1.25355227e+02,
1.17740492e+00, -2.55151916e+02, -7.20503620e+01,
-1.73992688e+01, -2.44854505e+02, -9.25408725e+01,
8.70569014e-01, -1.68664569e+02, -3.73902498e+01,
1.39982512e+00, -2.00884252e+02, -4.47207875e+01,
5.24591115e-01, -1.65867774e+02, -3.68342864e+01,
5.24591115e-01, -1.65867774e+02, -3.68342864e+01]])]
I had planned to extract the first column and create a vector x extract second column vector y and third column vector z. Howver when I converted it into an array , it resulted into 1x1x96array type with the following structure
[[[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 -5.41511240e+01
-2.30953821e+01 -1.74871288e+01 -4.91000564e+01 2.44382720e+02
1.71213983e+02 -1.54755310e+02 5.97881714e+02 3.52366218e+02
-1.15560633e+01 7.42149725e+02 1.66477287e+02 -1.18447102e+01
7.36763064e+02 1.65182437e+02 5.41502771e+01 2.30950820e+01
1.74870470e+01 -1.71258190e+01 3.26592894e+02 2.03220778e+02
-1.41130065e+02 7.04033786e+02 3.84201614e+02 -1.12642400e+01
7.48636864e+02 1.66665977e+02 -1.14090840e+01 7.36435064e+02
1.63713810e+02 1.21660845e-03 -8.60110629e-02 -1.93000576e-02
1.57141460e+01 -2.34399996e+02 -2.86722926e+01 5.28252697e+01
-4.40469167e+02 -1.11653705e+02 1.03085631e+02 -5.01280352e+02
-1.93111585e+02 7.16011844e+01 -5.88214725e+02 -2.18615940e+02
5.67537804e+00 -4.35088906e+02 -9.76974016e+01 7.71909591e+01
-3.88738749e+02 -6.29099586e+01 -2.99970496e+01 -2.25985794e+02
6.41590789e+01 1.03847001e+02 -7.32419021e+01 1.04802558e+02
1.26585822e+00 -1.20170579e+02 -2.82526049e+01 1.57900698e+00
-1.51780249e+02 -3.52080548e+01 8.84543993e-01 -1.07795356e+02
-2.56307189e+01 8.84543993e-01 -1.07795356e+02 -2.56307189e+01
5.67537804e+00 -4.35088906e+02 -9.76974016e+01 8.01141013e+00
-4.16078607e+02 -1.25355227e+02 1.17740492e+00 -2.55151916e+02
-7.20503620e+01 -1.73992688e+01 -2.44854505e+02 -9.25408725e+01
8.70569014e-01 -1.68664569e+02 -3.73902498e+01 1.39982512e+00
-2.00884252e+02 -4.47207875e+01 5.24591115e-01 -1.65867774e+02
-3.68342864e+01 5.24591115e-01 -1.65867774e+02 -3.68342864e+01]]]
It leads to , an array [x0,y0,z0,x1,y1,z1,x3,y3,z3...... x31,y31,z31] and i want to extract the following ,
x = [x0, x2, .......... x31]
y= [y0, y1, ...........y31]
z= [z0,z1,..............z31]
I was wondering if thats possible to do it in the list or modifying array
IICU, given an array
[x0,y0,z0,x1,y1,z1,x2,y2,z2, .... , x31,y31,z31]
You can just use numpy indexing
x = arr[np.arange(0,len(arr),3)]
y = arr[np.arange(1,len(arr),3)]
z = arr[np.arange(2,len(arr),3)]
Edit:
As suggested in subsampling every nth entry in a numpy array
, Numpy.ndarray offers a simple and elegant option:
import numpy as np
arr = np.array(my_list)
x = arr[::3]
y = arr[1::3]
z = arr[2::3]
Original answer:
I'm not sure about the structure of your input list, as what you provided here is an invalid data.
Despite the extra brackets in your source data, this can be done with a fairly simple trick, and you don't have to be bothered with Numpy:
Assuming that you are trying to somehow split the list into three vectors in your specified way, assign your list to a, then you could have your desired lists by:
x = [a[3*i] for i in range(int(len(a)/3))]
y = [a[3*i+1] for i in range(int(len(a)/3))]
z = [a[3*i+2] for i in range(int(len(a)/3))]

How to efficiently mutate certain num of values in an array?

Given an initial 2-D array:
initial = [
[0.6711999773979187, 0.1949000060558319],
[-0.09300000220537186, 0.310699999332428],
[-0.03889999911189079, 0.2736999988555908],
[-0.6984000205993652, 0.6407999992370605],
[-0.43619999289512634, 0.5810999870300293],
[0.2825999855995178, 0.21310000121593475],
[0.5551999807357788, -0.18289999663829803],
[0.3447999954223633, 0.2071000039577484],
[-0.1995999962091446, -0.5139999985694885],
[-0.24400000274181366, 0.3154999911785126]]
The goal is to multiply some random values inside the array by a random percentage. Lets say only 3 random numbers get replaced by a random multipler, we should get something like this:
output = [
[0.6711999773979187, 0.52],
[-0.09300000220537186, 0.310699999332428],
[-0.03889999911189079, 0.2736999988555908],
[-0.6984000205993652, 0.6407999992370605],
[-0.43619999289512634, 0.5810999870300293],
[0.84, 0.21310000121593475],
[0.5551999807357788, -0.18289999663829803],
[0.3447999954223633, 0.2071000039577484],
[-0.1995999962091446, 0.21],
[-0.24400000274181366, 0.3154999911785126]]
I've tried doing this:
def mutate(array2d, num_changes):
for _ in range(num_changes):
row, col = initial.shape
rand_row = np.random.randint(row)
rand_col = np.random.randint(col)
cell_value = array2d[rand_row][rand_col]
array2d[rand_row][rand_col] = random.uniform(0, 1) * cell_value
return array2d
And that works for 2D arrays but there's chance that the same value is mutated more than once =(
And I don't think that's efficient and it only works on 2D array.
Is there a way to do such "mutation" for array of any shape and more efficiently?
There's no restriction of which value the "mutation" can choose from but the number of "mutation" should be kept strict to the user specified number.
One fairly simple way would be to work with a raveled view of the array. You can generate all your numbers at once that way, and make it easier to guarantee that you won't process the same index twice in one call:
def mutate(array_anyd, num_changes):
raveled = array_anyd.reshape(-1)
indices = np.random.choice(raveled.size, size=num_changes, replace=False)
values = np.random.uniform(0, 1, size=num_changes)
raveled[indices] *= values
I use array_anyd.reshape(-1) in favor of array_anyd.ravel() because according to the docs, the former is less likely to make an inadvertent copy.
The is of course still such a possibility. You can add an extra check to write back if you need to. A more efficient way would be to use np.unravel_index to avoid creating a view to begin with:
def mutate(array_anyd, num_changes):
indices = np.random.choice(array_anyd.size, size=num_changes, replace=False)
indices = np.unravel_indices(indices, array_anyd.shape)
values = np.random.uniform(0, 1, size=num_changes)
raveled[indices] *= values
There is no need to return anything because the modification is done in-place. Conventionally, such functions do not return anything. See for example list.sort vs sorted.
Using shuffle instead of random_choice, this would be a different solution. It works on an array of any shape.
def mutate(arrayIn, num_changes):
mult = np.zeros(arrayIn.ravel().shape[0])
mult[:num_changes] = np.random.uniform(0,1,num_changes)
np.random.shuffle(mult)
mult = mult.reshape(arrayIn.shape)
arrayIn = arrayIn + mult*arrayIn
return arrayIn

How to get indices of maximum values in numpy but with threshold value?

My code
import numpy as np
from numpy import loadtxt
s = loadtxt("sest.txt", delimiter=" ", unpack=False)
b = loadtxt("base.txt", delimiter=" ", unpack=False)
d=b-s
e = np.absolute(d)
me = e.argsort()[-100:][::-1]
print me
I got
[400600 401600 399600 400601 401601 399601 401599 400599 399599 399602
401602 400602 399598 401598 400598 400603 401603 399603 401597 399597
401604 400597 399604 400604 400605 399605 401605 401596 399596 400596
399606 401606 400606 399595 401595 400595 399607 401607 400607 400608
400594 401608 399608 401594 399594 400609 401609 399609 401593 400593
399593 401610 400610 399610 400592 401592 399592 399611 400611 401611
399591 401612 401591 400612 400591 399612 399613 401613 400613 399590
400590 401590 400614 399614 401614 399589 400589 401589 401615 399615
400615 401616 399616 400616 400588 399588 401588 400617 401617 399617
401587 400587 399587 400618 399618 401618 399586 400586 401586 400619]
Works fine.But I want to specify all elements in d that are larger then 2.5?So I do not care if there are 100 or 200 just everything above this threshold level.Is it possible to extend argsort or not?
if you are just seeking array elements above a certain threshold value, you can use x[x>a], where a is the threshold. For purposes of illustration, I will show now using ipython and edit later. Let us assume "x" is some numpy array:
In [9]: x=np.random.rand(1,10) # an array with random elements
In [10]: print x[x>0.6] # select elements above 0.6
[ 0.71733906 0.74028607 0.66293195 0.86649922 0.7423685 0.71807904
0.8215429 ]
In [11]: print x
[[ 0.36655557 0.71733906 0.74028607 0.66293195 0.86649922 0.21478604
0.7423685 0.71807904 0.30482062 0.8215429 ]]

Using numpy's function loadtxt() to read a two column file then using polyfit() to return an array

afile is a given file, and z the degree of the polynomial. Been breaking my head over this for a while, frustrating how I'm basically given zero instructions on how to proceed.
This is what I thought it should be like:
import numpy as np
def newfile(afile,z):
x,y = np.loadtxt(afile)
d= np.polyfit(x,y,z)
return d
I have attempted to do it as
data = np.loadtxt(afile)
x = data[0:]
by printing "data" I'm given this format:
[[ 2. 888.8425]
[ 6. 888.975 ]
[ 14. 888.1026]
[ 17. 888.2071]
[ 23. 886.0479]
[ 26. 883.3316]
[ 48. 877.04 ]
[ 99. 854.3665]]
By printing "x" in this case just gives me the whole list (I'm thinking the issue lies in the lack of comma). In this case I'd want x to be an array of the left column.
I suppose you are getting an error when unpacking in this statement:
x,y = np.loadtxt(afile)
you should replace it for this:
x, y = zip(*np.loadtxt(afile))
the rest should work

Categories

Resources