I am trying to print matrix nicely using pandas.
But I have a problem
I have this matrix:
[[ 0 -2 -4 -6 -8 -10]
[ -2 1 -1 -3 -5 -7]
[ -4 -1 2 0 -2 -4]
[ -6 -3 0 1 1 -1]
[ -8 -5 -2 -1 0 2]]
That I filled with this code:
def NW(s1,s2,match = 1,mismatch = -1, gap = -2):
penalty = {'MATCH': match, 'MISMATCH': mismatch, 'GAP': gap} #A dictionary for all the penalty valuse.
n = len(s1) + 1 #The dimension of the matrix columns.
m = len(s2) + 1 #The dimension of the matrix rows.
al_mat = np.zeros((m,n),dtype = int) #Initializes the alighment matrix with zeros.
#Scans all the first rows element in the matrix and fill it with "gap penalty"
for i in range(m):
al_mat[i][0] = penalty['GAP'] * i
p_mat[i][0] = 'V'
#Scans all the first columns element in the matrix and fill it with "gap penalty"
for j in range (n):
al_mat[0][j] = penalty['GAP'] * j
#Fill the matrix with the correct values.
for i in range(1,m):
for j in range(1,n):
di = al_mat[i-1][j-1] + Diagonal(s1[j-1],s2[i-1],penalty) #The value for match/mismatch - diagonal.
ho = al_mat[i][j-1] + penalty['GAP'] #The value for gap - horizontal.(from the left cell)
ve = al_mat[i-1][j] + penalty['GAP'] #The value for gap - vertical.(from the upper cell)
al_mat[i][j] = max(di,ho,ve) #Fill the matrix with the maximal value.(based on the python default maximum)
return al_mat
I want the matrix to look like this:
T C G C A
0 -2 -4 -6 -8 -10
T -2 1 -1 -3 -5 -7
C -4 -1 2 0 -2 -4
C -6 -3 0 1 1 -1
A -8 -5 -2 -1 0 2
or like this:
T C G C A
[0 -2 -4 -6 -8 -10]
T [-2 1 -1 -3 -5 -7]
C [-4 -1 2 0 -2 -4]
C [-6 -3 0 1 1 -1]
A [-8 -5 -2 -1 0 2]
I was trying to write this code:
import pandas as pd
col1 = [' ', 'T', 'C', 'G', 'C', 'A']
col2 = [' ', 'T', 'C', 'C', 'A']
df = pd.DataFrame(mat,index = col2, columns = col1)
print df
But I"v got this error:
df = pd.DataFrame(mat,index = col2, columns = col1)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 163, in __init__
copy=copy)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 224, in _init_ndarray
return BlockManager([block], [columns, index])
File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 237, in __init__
self._verify_integrity()
File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 313, in _verify_integrity
union_items = _union_block_items(self.blocks)
File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 906, in _union_block_items
raise Exception('item names overlap')
Exception: item names overlap
So it's worked only when I changed 1 letter, so I got:
T B G C A
0 -2 -4 -6 -8 -10
T -2 1 -1 -3 -5 -7
C -4 -1 2 0 -2 -4
C -6 -3 0 1 1 -1
A -8 -5 -2 -1 0 2
But still the layout of the matrix is not quite well.
How can I fix those problems?
Judging by the matrix that you've provided, I'm going to guess that your problem is that the matrix you are trying to use doesn't include commas and this is why you're getting errors. Try setting mat to equal:
[[0,-2,-4,-6,-8,-10],
[-2,1,-1,-3,-5,-7],
[-4,-1,2,0,-2,-4],
[-6,-3,0,1,1,-1],
[-8,-5,-2,-1,0,2]]
Related
I need to display negative numbers as positive (i.e. just hide the negative sign)
For that I'm currently using this format
proc format;
* 9 forces leading zeros, 0 doesn't;
* numbers are inversed so high = low;
picture posval
low - high='000009.00'
;
run;
But the problem is that in a gtl plot I will have 2 decimal values on the axis.
I can't change the format of the axis tickvalues back to no decimals.
Since my numbers range from -6 to 0 and none of them are exactly integers I figured something like this but it's totally inelegant and is more a hack than a solution.
proc format;
* 9 forces leading zeros, 0 doesn't;
* numbers are inversed so high = low;
picture posval
low -< -7 = '000009.00'
-7 -< -7 = '0009'
-7 -< -6 = '0009.00'
-6 -< -6 = '0009'
-6 -< -5 = '0009.00'
-5 -< -5 = '0009'
-5 -< -4 = '0009.00'
-4 -< -4 = '0009'
-4 -< -3 = '0009.00'
-3 -< -3 = '0009'
-3 -< -2 = '0009.00'
-2 -< -2 = '0009'
-2 -< -1 = '0009.00'
-1 -< -1 = '0009'
-1 -< -0 = '0009.00'
0 -< 0 = '000009'
0-high = '000009'
;
run;
Is there a better way?
EDIT:
A bit more information: I purposely invert my values to negatives to draw them as inverted bars on a barchartparm (sas GTL).
Why not just create a user defined format to call the ABS() function?
proc format ;
value abs low-high = [abs()] ;
run;
Why don't you use abs (absolute) function:
data abc;
input a;
cards;
-1
-2
-2.5
-3
2
3
3.05
4
;
run;
data abc1;
set abc;
a1 = abs(a);
run;
Output:
a | a1
-1 | 1
-2 | 2
-2.5 | 2.5
-3 | 3
2 | 2
3 | 3
3.05 | 3.05
4 | 4
Also, share the input output data sets if you have any different requirement.
I am working on a problem where I have to computer five eighth (5/8) of a given number using bit operations?
For positive number, I can do pretty easily. Basically, it is ( (x << 2) + x )>> 3.
However, for negative number it does not seem to work. I looked around the web, and apparently, I have to add a factor of 7, however, I can't quiet see why that would be required?
Division using shifting rounds towards negative infinity, while normal C division rounds towards zero.
That is, -9 / 8 == 1 (i.e., -1.25 rounded towards zero is -1), but -9 >> 2 == -2 (i.e., ``-1.25` rounded towards negative infinity is -2).
To fix that, for the specific case of division by 8 you can add 7 in the case of negative numbers to "adjust" the dividend such that the rounding happens as you expect.
The entirety of this question assumes your C compiler implements "arithmetic right shifts" for signed right shifts. Pretty much all architecture/compiler combinations do, but it's not guaranteed by the standard.
For positive x, x >> 3 and x / 8 both round toward zero.
For negative x, x >> 3 rounds toward negative infinity, while x / 8 rounds toward zero. Examples:
-1 >> 3 = -1 -1 / 8 = 0 different
-2 >> 3 = -1 -2 / 8 = 0 different
-3 >> 3 = -1 -3 / 8 = 0 different
-4 >> 3 = -1 -4 / 8 = 0 different
-5 >> 3 = -1 -5 / 8 = 0 different
-6 >> 3 = -1 -6 / 8 = 0 different
-7 >> 3 = -1 -7 / 8 = 0 different
-8 >> 3 = -1 -8 / 8 = -1 same
-9 >> 3 = -2 -9 / 8 = -1 different
-10 >> 3 = -2 -10 / 8 = -1 different
-11 >> 3 = -2 -11 / 8 = -1 different
-12 >> 3 = -2 -12 / 8 = -1 different
-13 >> 3 = -2 -13 / 8 = -1 different
-14 >> 3 = -2 -14 / 8 = -1 different
-15 >> 3 = -2 -15 / 8 = -1 different
-16 >> 3 = -2 -16 / 8 = -2 same
-17 >> 3 = -3 -17 / 8 = -2 different
-18 >> 3 = -3 -18 / 8 = -2 different
-19 >> 3 = -3 -19 / 8 = -2 different
When the numerator (x) is a multiple of the denominator (8), the results are the same. For the other 7/8 of the results, the results are different by 1. This means if we want >> 3 to behave the same as / 8, we need to change the numerator.
Generally speaking, if you have an integer division operator that rounds down, you can make it round up by adding (denominator - 1) to the numerator. But let's get there in baby steps. Suppose we change the numerator by adding 1:
( -1 + 1) >> 3 = 0 -1 / 8 = 0 same
( -2 + 1) >> 3 = -1 -2 / 8 = 0 different
( -3 + 1) >> 3 = -1 -3 / 8 = 0 different
( -4 + 1) >> 3 = -1 -4 / 8 = 0 different
( -5 + 1) >> 3 = -1 -5 / 8 = 0 different
( -6 + 1) >> 3 = -1 -6 / 8 = 0 different
( -7 + 1) >> 3 = -1 -7 / 8 = 0 different
( -8 + 1) >> 3 = -1 -8 / 8 = -1 same
( -9 + 1) >> 3 = -1 -9 / 8 = -1 same
(-10 + 1) >> 3 = -2 -10 / 8 = -1 different
(-11 + 1) >> 3 = -2 -11 / 8 = -1 different
(-12 + 1) >> 3 = -2 -12 / 8 = -1 different
(-13 + 1) >> 3 = -2 -13 / 8 = -1 different
(-14 + 1) >> 3 = -2 -14 / 8 = -1 different
(-15 + 1) >> 3 = -2 -15 / 8 = -1 different
(-16 + 1) >> 3 = -2 -16 / 8 = -2 same
(-17 + 1) >> 3 = -2 -17 / 8 = -2 same
(-18 + 1) >> 3 = -3 -18 / 8 = -2 different
(-19 + 1) >> 3 = -3 -19 / 8 = -2 different
Now we have 2/8 of the results matching. Try adding 2:
( -1 + 2) >> 3 = 0 -1 / 8 = 0 same
( -2 + 2) >> 3 = 0 -2 / 8 = 0 same
( -3 + 2) >> 3 = -1 -3 / 8 = 0 different
( -4 + 2) >> 3 = -1 -4 / 8 = 0 different
( -5 + 2) >> 3 = -1 -5 / 8 = 0 different
( -6 + 2) >> 3 = -1 -6 / 8 = 0 different
( -7 + 2) >> 3 = -1 -7 / 8 = 0 different
( -8 + 2) >> 3 = -1 -8 / 8 = -1 same
( -9 + 2) >> 3 = -1 -9 / 8 = -1 same
(-10 + 2) >> 3 = -1 -10 / 8 = -1 same
(-11 + 2) >> 3 = -2 -11 / 8 = -1 different
(-12 + 2) >> 3 = -2 -12 / 8 = -1 different
(-13 + 2) >> 3 = -2 -13 / 8 = -1 different
(-14 + 2) >> 3 = -2 -14 / 8 = -1 different
(-15 + 2) >> 3 = -2 -15 / 8 = -1 different
(-16 + 2) >> 3 = -2 -16 / 8 = -2 same
(-17 + 2) >> 3 = -2 -17 / 8 = -2 same
(-18 + 2) >> 3 = -2 -18 / 8 = -2 same
(-19 + 2) >> 3 = -3 -19 / 8 = -2 different
Apparently, if we compute (x + i) >> 3, (i+1)/8 of the results match. So to make all of the results match, we solve (i+1)/8 = 1 for i, getting i = 7. And here's what we get if we add 7 to the numerator:
( -1 + 7) >> 3 = 0 -1 / 8 = 0 same
( -2 + 7) >> 3 = 0 -2 / 8 = 0 same
( -3 + 7) >> 3 = 0 -3 / 8 = 0 same
( -4 + 7) >> 3 = 0 -4 / 8 = 0 same
( -5 + 7) >> 3 = 0 -5 / 8 = 0 same
( -6 + 7) >> 3 = 0 -6 / 8 = 0 same
( -7 + 7) >> 3 = 0 -7 / 8 = 0 same
( -8 + 7) >> 3 = -1 -8 / 8 = -1 same
( -9 + 7) >> 3 = -1 -9 / 8 = -1 same
(-10 + 7) >> 3 = -1 -10 / 8 = -1 same
(-11 + 7) >> 3 = -1 -11 / 8 = -1 same
(-12 + 7) >> 3 = -1 -12 / 8 = -1 same
(-13 + 7) >> 3 = -1 -13 / 8 = -1 same
(-14 + 7) >> 3 = -1 -14 / 8 = -1 same
(-15 + 7) >> 3 = -1 -15 / 8 = -1 same
(-16 + 7) >> 3 = -2 -16 / 8 = -2 same
(-17 + 7) >> 3 = -2 -17 / 8 = -2 same
(-18 + 7) >> 3 = -2 -18 / 8 = -2 same
(-19 + 7) >> 3 = -2 -19 / 8 = -2 same
To make it more visual,
Take the number, say, -42 (which is not a multiple of 5, but whatever)
11010110
Put a radix point 3 places from the right (this divides by 8)
11010.110 = -5.25
How to round up: add ones to all fraction bits (meaning that iff those are not all zero, the addition will carry into the integer part), so 0.111, then chop:
11010.110
0.111 = 7/8
--------- +
11011.101 = -4.375
chop:
11011.000 = -5
To convert to a normal integer, shift until the radix point is just before the least significant bit:
11011.000 >>s 3 =
11111011. = still -5, but in normal integer format
In the code you just put the radix point in virtually (so do nothing, but then proceed as if it is there) and fractions are implicit (so 7/8 is written as 7). And the chop is unnecessary since the right shift throws those bits out anyway. All that's left is add 7, then shift.
I have a problem with the output of the soft demapper function (demodulate_soft_bits) when using 16-QAM and 64-QAM modulation.
here is a portion of the code (64-QAM):
cvec comple;
Modulator_2D qam;
ivec cont_d = "0:1:63";
vec qam_r = "-7 -7 -7 -7 -7 -7 -7 -7 -5 -5 -5 -5 -5 -5 -5 -5 -3 -3 -3 -3 -3 -3 -3 -3 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7";
vec qam_i = "-7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7";
comple.set_length(qam_i.length(),false);
for(int i=0; i<qam_i.length(); i++)
comple(i) = std::complex<double>(qam_r(i)/sqrt((double)42),qam_i(i)/sqrt((double)42));
qam.set(comple, cont_d);
temp = qam.demodulate_soft_bits( symb_recus,channel,1);
symb_recus is the output of a realistic 4x4 MIMO channel
channel is the channel gain
At the output I got a random values which doesn't correspond to the hard output and I got also a succession of 0
exp :
temp : [6.80 3.33 1.64 -14.27 -7.06 -3.51 0 0 0 0 0 0 -3.47 .....]
Hard output : [ 0 1 0 1 1 1 1 1 0 0 1 1 0 ... ]
can you please help me
I need to build a bar gragh that illustrate a distribution of pseudorandom numbers that determined by linear congruential method
Xn+1 = (a * Xn + c) mod m
U = X/m
on the interval [0,1]
For example:
Interval Frequency
[0;0,1] 0,05
[0,1;0,2] 0,15
[0,2;0,3] 0,1
[0,3;0,4] 0,12
[0,4;0,5] 0,1
[0,5;0,6] 0,15
[0,6;0,7] 0,05
[0,7;0,8] 0,08
[0,8;0,9] 0,16
[0,9;1,0] 0,4
I used such a method:
float mas[10] = {0,0,0,0,0,0,0,0,0,0};
void metod1()
{
int x=-2, m=437, a=33, c=61;
float u;
for(int i=0;i<m;i++){
x=(a*x + c) % m;
u=(float)x/(float)m;
int r;
r = ceil(u*10);
mas[r] = mas[r] + 1;
}
for(i=0;i<10;i++) cout<<"["<<(float)i/10<<";"<<(float)(i+1)/10<<"]"<<" | "<<mas[i]<<"\n-----------------"<<endl;
return;
}
If you know another officient methods for this problem, that are not straitforward, i would appreciate it.
Your code currently has a much larger problem the efficiency. Assuming you've defined mas as something like int mas[10];, it has undefined behavior.
To see the problem, let's modify your code to print out the values of r that it generates:
void metod1() {
int mas[11] = { };
int x = -2, m = 437, a = 33, c = 61;
float u;
for (int i = 0; i < m; i++) {
x = (a*x + c) % m;
u = (float)x / (float)m;
int r;
r = ceil(u * 10);
//mas[r] = mas[r] + 1;
std::cout << r << '\t';
}
// for (i = 0; i < 10; i++) cout << "[" << (float)i / 10 << ";" << (float)(i + 1) / 10 << "]" << " | " << mas[i] << "\n-----------------" << endl;
return;
}
Then let's look at the results:
0 -2 -7 -4 -7 -7 -6 0 -6 -1
-5 -6 -1 -9 -7 -2 -7 -3 0 -6
0 -8 -5 -6 -8 -6 -7 0 -2 -6
-7 -6 -2 -4 -9 0 -4 -5 -1 -2
-5 0 -2 -1 -4 -8 -5 -2 -8 -5
-9 -4 -5 -7 -9 -8 -3 -9 -9 -9
-3 -4 -5 -3 -9 -6 -5 -3 -1 0
-5 -5 -6 -7 -9 -5 -4 -1 -5 -1
-9 -2 0 -9 -6 -7 -5 -5 -3 -3
-9 -3 0 -4 -1 -1 0 -8 -4 -4
-2 -7 0 -6 -6 -8 -4 -8 -2 -8
-8 -2 -4 -7 -1 -6 -1 -3 -7 -3
-5 -9 -8 -5 -8 -7 -4 -1 -8 -7
-7 -2 -9 -5 -3 0 2 8 8 2
6 8 7 1 5 2 8 4 1 5
10 1 3 6 4 10 5 6 6 10
[more elided]
It doesn't look like you've planned for the fact that you'll be producing negative numbers, and if you fail to do so, the result is undefined behavior when you index outside the bounds of mas.
I have some questions on manipulating on a file ;
a.) I am a bit confused about get and put pointer in c++. Do I show correct position of get pointer and put pointer.
MyFile . seekg ( 0 , ios :: beg ) ;
MyFile . seekp ( -10 , ios :: end ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
__________________________________________________________________
^ ^
^ ^
^ ^
get Pointer put pointer
Myfile . get ( character ) ;
MyFile . write ( SomeString, 4 ) ;
MyFile . flush ( ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
__________________________________________________________________
^ ^
^ ^
^ ^
get Pointer put pointer
i.) Are Seekg and seekp always guarentee that get an put pointer always shows correct position ?
ii.) If you know more about this topic, can you show/give me some point(s) I should be careful when I use them, ( if there is )
b.) Is
FileIN . seekg ( 1, ifstream :: cur ) ;
equal to
FileIN . seekg ( 1, ios :: cur ) ;
Platform : linux
File format : binary
a) It's wrong. File streams maintain one file pointer, for both input and output. Both seekg and seekp do the same thing. The reason there are two different functions is that the interface of iostreams is generic, it can be used for devices which do have separate put and get pointers.
Quote from the standard [filebuf]:
In particular:
— If the file is not open for reading the input sequence cannot be read.
— If the file is not open for writing the output sequence cannot be written.
— A joint file position is maintained for both the input sequence and the output sequence.
b) Yes, they are the same.
EDIT:
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
MyFile . seekg ( 0 , ios :: beg ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
MyFile . seekp ( -10 , ios :: end ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
Myfile . get ( character ) ;
// you must sync/flush if your last operation was input and you switch to output,
// or your last operation was output and you switch to input.
MyFile . sync ( ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
MyFile . write ( SomeString, 4 ) ;
MyFile . flush ( ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer