Best Python code snippet using locust
6. UC - Practice - Python Percentiles.py
Source:6. UC - Practice - Python Percentiles.py
...21plt.show() # Display the histogram 2223# Calculate Percentile Values using numpy functions24# 50th Percentile25np.percentile(vals, 50) # Gives value at 50th percentile which is the Median2627# Compute the median28np.median(vals) # Display the median of random values2930# 90th Percentile31np.percentile(vals, 90) # Gives value at 90th percentile3233# 20th Percentile34np.percentile(vals, 20) # Gives value at 20th percentile3536# 75th Percentile37np.percentile(vals, 75) # Gives value at 75th percentile which is the Q33839# 25th Percentile40np.percentile(vals, 25) # Gives value at 25th percentile which is the Q14142# Generate random numbers with normal distribution - Set 243vals = np.random.normal(5, 10, 10000) # Generate random numbers with normal distribution; mu = 5, sigma = 104445# Segment the income data into 50 buckets, and plot it as a histogram46plt.hist(vals, 50) # Segments the data into 50 buckets47plt.show() # Display the histogram 4849# Calculate Percentile Values using numpy functions50# 50th Percentile51np.percentile(vals, 50) # Gives value at 50th percentile which is the Median5253# Compute the median54np.median(vals) # Display the median of random values5556# 90th Percentile57np.percentile(vals, 90) # Gives value at 90th percentile5859# 20th Percentile60np.percentile(vals, 20) # Gives value at 20th percentile6162# 75th Percentile63np.percentile(vals, 50) # Gives value at 75th percentile which is the Q36465# 25th Percentile66np.percentile(vals, 25) # Gives value at 25th percentile which is the Q16768# Generate random numbers with normal distribution - Set 369vals = np.random.normal(-5, 10, 10000) # Generate random numbers with normal distribution; mu = -5, sigma = 107071# Segment the income data into 50 buckets, and plot it as a histogram72plt.hist(vals, 50) # Segments the data into 50 buckets73plt.show() # Display the histogram 7475# Calculate Percentile Values using numpy functions76# 50th Percentile77np.percentile(vals, 50) # Gives value at 50th percentile which is the Median7879# Compute the median80np.median(vals) # Display the median of random values8182# 90th Percentile83np.percentile(vals, 90) # Gives value at 90th percentile8485# 20th Percentile86np.percentile(vals, 20) # Gives value at 20th percentile8788# 75th Percentile89np.percentile(vals, 50) # Gives value at 75th percentile which is the Q39091# 25th Percentile92np.percentile(vals, 25) # Gives value at 25th percentile which is the Q19394# Generate random numbers with normal distribution - Set 495vals = np.random.normal(-50, 10, 100) # Generate random numbers with normal distribution; mu = -50, sigma = 109697# Segment the income data into 50 buckets, and plot it as a histogram98plt.hist(vals, 50) # Segments the data into 50 buckets99plt.show() # Display the histogram 100101# Calculate Percentile Values using numpy functions102# 50th Percentile103np.percentile(vals, 50) # Gives value at 50th percentile which is the Median104105# Compute the median106np.median(vals) # Display the median of random values107108# 90th Percentile109np.percentile(vals, 90) # Gives value at 90th percentile110111# 20th Percentile112np.percentile(vals, 20) # Gives value at 20th percentile113114# 75th Percentile115np.percentile(vals, 50) # Gives value at 75th percentile which is the Q3116117# 25th Percentile118np.percentile(vals, 25) # Gives value at 25th percentile which is the Q1119
...
test_percentiles.py
Source:test_percentiles.py
...16 "dask",17 ],18)19@percentile_internal_methods20def test_percentile(internal_method):21 d = da.ones((16,), chunks=(4,))22 qs = [0, 50, 100]23 assert_eq(24 da.percentile(d, qs, internal_method=internal_method),25 np.array([1, 1, 1], dtype=d.dtype),26 )27 x = np.array([0, 0, 5, 5, 5, 5, 20, 20])28 d = da.from_array(x, chunks=(3,))29 result = da.percentile(d, qs, internal_method=internal_method)30 assert_eq(result, np.array([0, 5, 20], dtype=result.dtype))31 assert same_keys(32 da.percentile(d, qs, internal_method=internal_method),33 da.percentile(d, qs, internal_method=internal_method),34 )35 assert not same_keys(36 da.percentile(d, qs, internal_method=internal_method),37 da.percentile(d, [0, 50], internal_method=internal_method),38 )39 if internal_method != "tdigest":40 x = np.array(["a", "a", "d", "d", "d", "e"])41 d = da.from_array(x, chunks=(3,))42 assert_eq(43 da.percentile(d, [0, 50, 100]), np.array(["a", "d", "e"], dtype=x.dtype)44 )45@pytest.mark.skip46def test_percentile_with_categoricals():47 try:48 import pandas as pd49 except ImportError:50 return51 x0 = pd.Categorical(["Alice", "Bob", "Charlie", "Dennis", "Alice", "Alice"])52 x1 = pd.Categorical(["Alice", "Bob", "Charlie", "Dennis", "Alice", "Alice"])53 dsk = {("x", 0): x0, ("x", 1): x1}54 x = da.Array(dsk, "x", chunks=((6, 6),))55 p = da.percentile(x, [50])56 assert (p.compute().categories == x0.categories).all()57 assert (p.compute().codes == [0]).all()58 assert same_keys(da.percentile(x, [50]), da.percentile(x, [50]))59@percentile_internal_methods60def test_percentiles_with_empty_arrays(internal_method):61 x = da.ones(10, chunks=((5, 0, 5),))62 assert_eq(63 da.percentile(x, [10, 50, 90], internal_method=internal_method),64 np.array([1, 1, 1], dtype=x.dtype),65 )66@percentile_internal_methods67def test_percentiles_with_empty_q(internal_method):68 x = da.ones(10, chunks=((5, 0, 5),))69 assert_eq(70 da.percentile(x, [], internal_method=internal_method),71 np.array([], dtype=x.dtype),72 )73@percentile_internal_methods74@pytest.mark.parametrize("q", [5, 5.0, np.int64(5), np.float64(5)])75def test_percentiles_with_scaler_percentile(internal_method, q):76 # Regression test to ensure da.percentile works with scalar percentiles77 # See #302078 d = da.ones((16,), chunks=(4,))79 assert_eq(80 da.percentile(d, q, internal_method=internal_method),81 np.array([1], dtype=d.dtype),82 )83@percentile_internal_methods84def test_unknown_chunk_sizes(internal_method):85 x = da.random.random(1000, chunks=(100,))86 x._chunks = ((np.nan,) * 10,)87 result = da.percentile(x, 50, internal_method=internal_method).compute()88 assert 0.1 < result < 0.989 a, b = da.percentile(x, [40, 60], internal_method=internal_method).compute()90 assert 0.1 < a < 0.991 assert 0.1 < b < 0.9...
stats.py
Source:stats.py
1import numpy as np2from .extmath import stable_cumsum3from .fixes import _take_along_axis4def _weighted_percentile(array, sample_weight, percentile=50):5 """Compute weighted percentile6 Computes lower weighted percentile. If `array` is a 2D array, the7 `percentile` is computed along the axis 0.8 .. versionchanged:: 0.249 Accepts 2D `array`.10 Parameters11 ----------12 array : 1D or 2D array13 Values to take the weighted percentile of.14 sample_weight: 1D or 2D array15 Weights for each value in `array`. Must be same shape as `array` or16 of shape `(array.shape[0],)`.17 percentile: int or float, default=5018 Percentile to compute. Must be value between 0 and 100....
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!