tinybike / weightedstats Goto Github PK
View Code? Open in Web Editor NEWCalculate weighted mean, median, and weighted median.
License: MIT License
Calculate weighted mean, median, and weighted median.
License: MIT License
Hi,
I have a test sample as follow:
df = pd.DataFrame([['Alabama',4779736,5.7],
['Alaska',710231,5.6],
['Arizona',6392017,4.7],
['Arkansas',2915918,5.6],
['California',37253956,4.4],
['Colorado',5029196,2.8],
['Connecticut',3574097,2.4],
['Delware',897934,5.8]],columns=['State','Population','Murder rate'])
I tried to use the weighted_median as:
import weightedstats as ws
ws.weighted_median(df['Murder rate'],weights=df['Population'])
but the following error showed up on the rerun part of the function(return data[weights.index(max(weights))]) :
TypeError: 'RangeIndex' object is not callable
It works with the weighted_mean
Thanks
Sima
I installed the module via pip and was playing with the example as given
import weightedstats as ws
my_data = [1, 2, 3, 4, 5]
my_weights = [10, 1, 1, 1, 9]
ws.weighted_median(my_data, weights=my_weights)
The output is 2.5. Seems correct since the median falls in between 2 and 3 here.
Then I changed the weights so 3 is the middle value:
my_weights = [10, 1, 1, 1, 10]
ws.weighted_median(my_data, weights=my_weights)
But now the output is still 2.5. This also happens when the weights are [1,1,1,1,1], but not when I omit the weights fully.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.