Sponsored Content
Top Forums Shell Programming and Scripting Python: make dual vector dot-product more pythonic Post 303041217 by figaro on Monday 18th of November 2019 04:18:23 PM
Old 11-18-2019
Python: make dual vector dot-product more pythonic

I have this dot product, calculating weighted means, and is applied to two columns in a list:
Code:
# calculate weighted values in sequence
for i in range(len(temperatures)-len(weights)):
  temperatures[i].append(sum([weights[j]*temperatures[i+j][5] for j in range(len(weights))]))
  temperatures[i].append(sum([weights[j]*temperatures[i+j][6] for j in range(len(weights))]))

The calculation is a running dot-product, ie the list of temperature samples is far larger than the list of weights, hence the correction of subtracting len(weights) at the end of the main loop.
This traverses the list of weights twice, which is inefficient and degrades performance. How could this be done in a more pythonic way?

I also have concerns about the main loop. Would this be considered more pythonic?:
Code:
# calculate weighted values in sequence
for i in range(len(temperatures)):
   try:
      # weighted calculation here
   except:
      # do nothing, because array out of bounds


Last edited by figaro; 11-18-2019 at 06:01 PM.. Reason: Emphasise the fact that the lists are not of the same length, ie the dot product calculates a running weighted mean.
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

make: WARNING- Product is not licensed.

I do beg for my bad english ofr advance (french..) I've a problem with unix sco openserver 5 enterprise system I can't do any make at all! when i do it, i've this message : make: WARNING- Product is not licensed. however, i've entered the license number, code and data information... ... (3 Replies)
Discussion started by: joedelabush
3 Replies

2. UNIX for Dummies Questions & Answers

Backup of Product Database

HI, I know its scarey me asking this, but system is homegrown and I am just having fun, but at the same time dont want to have tooo much fun where the phrase "little knowledge is dangerous" perfectly fits my actions ;-). I have a couple of packages that are failing to be removed... #... (2 Replies)
Discussion started by: Student37
2 Replies

3. HP-UX

TrustedMigration product ?

I'm running HP-UX B.11.23 U ia64 I've got SOX auditors asking me if we have the TrustedMigration product. I don't know what that is and google isn't being helpful. Can you tell me what this product is (and what it is for) and how to know if my system is running it and/or prove that it is... (1 Reply)
Discussion started by: LisaS
1 Replies

4. Shell Programming and Scripting

Make python script ignore .htaccess

I just wrote a tiny script with the help of ghostdog74 to search all my files for special content phrases. After a few modifications I now made it work, but one problem is left. The files are located in public_html folder, so there might also be .htaccess files. So I ignored scanning of that... (4 Replies)
Discussion started by: medic
4 Replies

5. Programming

vector c++

hello guys. i'm new to c++. i've problem using two dimensional vector. i've a project of making conway's game of life. this is the code that i have made so far. my problem is how can i give a two dimensional vector through main. glider.vec1 = vec; is not correct way to give a two... (2 Replies)
Discussion started by: nishrestha
2 Replies

6. Hardware

Fedora 16 dual monitor - dual head - automatic monitor shutdown

Hi, I am experiencing troubles with dual monitors in fedora 16. During boot time both monitors are working, but when system starts one monitor automatically shut down. It happend out of the blue. Some time before when I updated system this happend but then I booted older kernel release and... (0 Replies)
Discussion started by: wakatana
0 Replies

7. UNIX for Beginners Questions & Answers

Which Product to Choose?

Okay, I have an Asus A8NSLI board with an Athlon 64 and I dunno, maybe 8gig Ram and Windows has crashed for the last time so I've finally had enough and I'll make it a Unix machine. I have a new 1Tera drive and I'm all set to go. Which brand of Unix/Linux can you advise me to go for? The... (3 Replies)
Discussion started by: abrogard
3 Replies

8. Shell Programming and Scripting

Pythonic Parsing

Experts and All, Hello ! I am trying to fabricate a simple shell script in python that has taken me almost 5 hours to complete. I am using python 3.6. So, I am trying to read a file, parse the log file and trying to answer this basic question of how many GET's and how many POST's are there... (1 Reply)
Discussion started by: ManoharMa
1 Replies
NCFLINT(1)						      General Commands Manual							NCFLINT(1)

NAME
ncflint - netCDF File Interpolator SYNTAX
ncflint [-3] [-4] [-6] [-A] [-C] [-c] [-D dbg] [-d dim,[ min][,[ max]]] [-F] [-h] [-i var,val3][-L dfl_lvl][-l path] [-O] [-p path] [-R] [-r] [-t thr_nbr] [-v var[,...]] [-w wgt[, wgt2]] [-X box] [-x] file1 file2 file3 DESCRIPTION
ncflint creates an output file that is a linear combination of the input files. This linear combination can be a weighted average, a nor- malized weighted average, or an interpolation of the input files. Coordinate variables are not acted upon in any case, they are simply copied from file_1. There are two conceptually distinct methods of using ncflint. The first method is to specify the weight each input file is to have in the output file. In this method, the value val3 of a variable in the output file file_3 is determined from its values val1 and val2 in the two input files according to wgt1*val1+wgt2*val2 Here at least wgt1, and, optionally, wgt2, are specified on the command line with the -w (or --weight or --wgt_var ) switch. If only IR wgt1 is specified then wgt2 is automatically computed as wgt2=1-wgt1. Note that weights larger than 1 are allowed. Thus it is possible to specify wgt1=2 and wgt2=-3. One can use this functionality to multiply all the values in a given file by a constant. The second method of using ncflint is to specify the interpolation option with -i (or with the --ntp or --interpolate long options). This is really the inverse of the first method in the following sense. When the user specifies the weights directly, ncflint has no work to do besides multiplying the input values by their respective weights and adding the results together to produce the output values. This assumes it is the weights that are known a priori. In another class of cases it is the "arrival value" (i.e., val3 ) of a particular vari- able var that is known a priori. In this case, the implied weights can always be inferred by examining the values of var in the input files. This results in one equation in two unknowns, wgt1 and wgt2: val3=wgt1*val1+wgt2*val2. Unique determination of the weights requires imposing the additional constraint of normalization on the weights: wgt1+wgt2=1. Thus, to use the interpolation option, the user specifies var and val3 with the -i option. ncflint will compute wgt1 and wgt2, and use these weights on all variables to generate the output file. Although var may have any number of dimensions in the input files, it must represent a single, scalar value. Thus any dimensions associated with var must be "degenerate", i.e., of size one. If neither -i nor -w is specified on the command line, ncflint defaults to weighting each input file equally in the output file. This is equivalent to specifying -w0.5 or -w0.5,0.5. Attempting to specify both .BR -i and -w methods in the same command is an error. ncflint is programmed not to interpolate variables of type NC_CHAR and NC_BYTE. This behavior is hardcoded. AUTHOR
NCO manual pages written by Charlie Zender and Brian Mays. REPORTING BUGS
Report bugs to <http://sf.net/bugs/?group_id=3331>. COPYRIGHT
Copyright (C) 1995-2010 Charlie Zender This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICU- LAR PURPOSE. SEE ALSO
The full documentation for NCO is maintained as a Texinfo manual called the NCO User's Guide. Because NCO is mathematical in nature, the documentation includes TeX-intensive portions not viewable on character-based displays. Hence the only complete and authoritative versions of the NCO User's Guide are the PDF (recommended), DVI, and Postscript versions at <http://nco.sf.net/nco.pdf>, <http://nco.sf.net/nco.dvi>, and <http://nco.sf.net/nco.ps>, respectively. HTML and XML versions are available at <http://nco.sf.net/nco.html> and <http://nco.sf.net/nco.xml>, respectively. If the info and NCO programs are properly installed at your site, the command info nco should give you access to the complete manual, except for the TeX-intensive portions. HOMEPAGE
The NCO homepage at <http://nco.sf.net> contains more information. NCFLINT(1)
All times are GMT -4. The time now is 05:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy