Last active
August 15, 2021 06:57
-
-
Save tomron/fa56ae15723b862d2d93a49b74c831a9 to your computer and use it in GitHub Desktop.
Revisions
-
tomron revised this gist
Aug 15, 2021 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,10 +2,7 @@ import numpy as np import time url = "http://archive.ics.uci.edu/ml/machine-learning-databases/mammographic-masses/mammographic_masses.data" names = ['BI-RADS', 'Age', 'Shape', 'Margin', 'Density', 'Severity'] -
tomron created this gist
Aug 15, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ import pandas as pd import numpy as np import time url = """ http://archive.ics.uci.edu/ml/machine-learning-databases/ mammographic-masses/mammographic_masses.data """ names = ['BI-RADS', 'Age', 'Shape', 'Margin', 'Density', 'Severity'] def manual_convert(): df = pd.read_csv(url, names=names) df = df.replace('?', np.NAN) df.loc[:, names[:-1]] = df.loc[:, names[:-1]].apply(pd.to_numeric) def use_na_values(): df = pd.read_csv(url, names=names, na_values=["?"]) def use_converters(): df = pd.read_csv( url, names=names, converters={"BI-RADS": lambda x: x if x != "?" else np.NAN} ) def repeat(func, n=10): times = [] for _ in range(n): start = time.time() func() end = time.time() times.append(end-start) return sum(times)/len(times) n = 100 print("manual_convert", repeat(manual_convert, n)) print("use_na_values", repeat(use_na_values, n)) print("use_converters", repeat(use_converters, n))