목록전체 글 (462)
Note
How to find all the local maxima (or peaks) in a numeric series? # Input ser = pd.Series([2, 10, 3, 4, 9, 10, 2, 7, 3]) # Solution dd = np.diff(np.sign(np.diff(ser))) peak_locs = np.where(dd == -2)[0] + 1 peak_locs # output array([1, 5, 7])
How to convert a 1d array of tuples to a 2d numpy array? # Input: url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_1d = np.genfromtxt(url, delimiter=',', dtype=None) # Solution # 1: Convert each row to a list and get the first 4 items iris_2d = np.array([row.tolist()[:4] for row in iris_1d]) iris_2d[:4] # 2: Import only the first 4 columns from source url iri..
How to compute the euclidean distance between two series? # Input p = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) q = pd.Series([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) # 1 sum((p - q)**2)**.5 # 2 np.linalg.norm(p-q) # output 18.165902124584949
# 빈 데이터 프레임 생성 insert_df = pd.DataFrame() n_counts = 0 # df_channel - 채널id 데이터 프레임 for channel_id in df_channel['channel_id']: try: video_url = "https://www.youtube.com/channel/{}/community".format(channel_id) session = HTMLSession() response = session.get(video_url) n_counts += 1 if(response.status_code == 429): print(response) soup = bs(response.html.html, "html.parser") data = re.search(r"var..
How to extract a particular column from 1D array of tuples? # Input: url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_1d = np.genfromtxt(url, delimiter=',', dtype=None) print(iris_1d.shape) # output #> (150,) # Solution: species = np.array([row[4] for row in iris_1d]) species[:5] array([b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-se..