Best Python code snippet using pytest-benchmark
outliers.py
Source:outliers.py
...14 self.replace = replace15 # TODO: Tal vez implementar eliminación de outliers 2D para latitud y longitud16 # TODO: En los inputers tengo replacement por moda, mean, etc podrÃa disponibilizar métodos desde acá17 RIDICULOUS_PRICE_FOR_PROPERTY = 4000018 def iqr_outliers(19 self, data: pd.DataFrame, field: str, strategy="mode", margin: float = 1.520 ) -> pd.DataFrame:21 """Método de remoción de outliers usando IQR"""22 # Será la primera barrera de eliminación, solo tenemos campos positivos23 if not data[data[field].isnull()].empty:24 raise RuntimeError(25 f"Para aplicar IQR asegurate que el campo '{field}' no contenga NaN"26 )27 Q1 = data[field].quantile(0.25)28 Q3 = data[field].quantile(0.75)29 IQR = Q3 - Q130 mask = ~(31 (data[field] < (Q1 - margin * IQR)) | (data[field] > (Q3 + margin * IQR))32 )33 if self.replace:34 if strategy == "mode":35 replace = data[field].mode()[0]36 elif strategy == "mean":37 replace = data[field].mean()38 else:39 replace = data[field].median()40 data.loc[mask, field] = data.loc[mask, field].fillna(replace)41 return data42 return data[mask]43 def iqr_outliers_prices_per_comuna(self, data: pd.DataFrame) -> pd.DataFrame:44 """Filtra outliers por cada comuna 'l3'45 The idea behind removing localized outliers by 'l3' is the socioeconomic46 level, the prices of communes more exclusive areas,47 such as Puerto Madero or Recoleta, would be affected when48 trimming with global IQR. A possible better one is maybe49 do not cut by 'l3', but by larger areas, like south zone, central zone50 and north zone (It is a TODO!)51 """52 data = data[data.price > self.RIDICULOUS_PRICE_FOR_PROPERTY]53 groups = [54 self.iqr_outliers(group, "price")55 for _, group in data.copy().groupby("Comuna")56 ]57 return pd.concat(groups)58 def fit(self, X: pd.DataFrame, y=None) -> FieldOutliers:59 return self60 def transform(self, X: pd.DataFrame) -> pd.DataFrame:61 X = X.copy()62 X = self.iqr_outliers(X, "rooms", margin=3.2)63 X = self.iqr_outliers(X, "bedrooms", margin=3.2)64 X = self.iqr_outliers(X, "bathrooms", margin=3.2)65 X = self.iqr_outliers(X, "surface_total", strategy="mean")...
anomaly_detection_methodes.py
Source:anomaly_detection_methodes.py
1from scipy import stats2import numpy as np3import pandas as pd45def anomaly_detector(data, config):6 if config['time_series']:7 # TODO8 return None9 else:10 id_column=data['id']11 data=data.drop(columns=['id'])12 IQR_outliers=iqr_outlier_detector(data)13 zscore_outliers=zscore_outlier_detector(data)14 columns=['id']15 for i in range(len(zscore_outliers.columns)):16 columns.append(IQR_outliers.columns[i])17 columns.append(zscore_outliers.columns[i])18 result=pd.DataFrame(columns=columns)19 print(columns)20 result['id']=id_column.values21 result[IQR_outliers.columns]=IQR_outliers.values22 result[zscore_outliers.columns]=zscore_outliers.values2324 return result252627def iqr_outlier_detector(data):28 columns=data.columns29 columns=list(map(lambda x: 'IQR_method_'+x,columns))30 # calculate Q1 and Q331 Q1 = data.quantile(0.25)32 Q3 = data.quantile(0.75)3334 # calculate the IQR35 IQR = Q3 - Q13637 # filter the dataset with the IQR38 IQR_outliers = (data < (Q1 - 1.5 * IQR)) |(data > (Q3 + 1.5 * IQR))39 IQR_outliers.columns=columns40 return IQR_outliers4142def zscore_outlier_detector(data):43 columns=data.columns44 columns=list(map(lambda x: 'zscore_method_'+x,columns))45 data=(data-data.mean(axis=0))/data.std(axis=0)46 zscore_outliers= data >= 347 zscore_outliers.columns=columns
...
outlier_test_plot.py
Source:outlier_test_plot.py
...6data = get_image(3,9).get_data()7std = vol_std(data)8# RMS outliers plot 9rms = vol_rms_diff(data)10outliers_rms = iqr_outliers(rms)[0]11ranges = iqr_outliers(rms)[1]12outliers_values = []13for i in outliers_rms:14 outliers_values.append(rms[i])15plt.figure()16plt.plot(rms)17plt.plot(outliers_rms,outliers_values,"ro", marker = "o")18plt.axhline(y = ranges[0], linestyle="--", color = "g")19plt.axhline(y = ranges[1], linestyle="--", color = "g")20plt.savefig('outliers RMS sub9 run3')21### subject 1 run 1 22data = get_image(1,1).get_data()23std = vol_std(data)24rms = vol_rms_diff(data)25outliers_rms = iqr_outliers(rms)[0]26ranges = iqr_outliers(rms)[1]27outliers_values = []28for i in outliers_rms:29 outliers_values.append(rms[i])30plt.figure()31plt.plot(rms)32plt.plot(outliers_rms,outliers_values,"ro", marker = "o")33plt.axhline(y = ranges[0], linestyle="--", color = "g")34plt.axhline(y = ranges[1], linestyle="--", color = "g")...
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!!