본문 바로가기
반응형

python338

파이썬 병렬 처리 joblib.Parallel 파이썬 병렬 처리 joblib.Parallel¶ In [1]: from joblib import Parallel, delayed from math import sqrt In [2]: def multiply(num): return num * num In [3]: %%time a= Parallel(n_jobs=1)(delayed(multiply)(i) for i in range(0,100000)) CPU times: user 16.5 s, sys: 97.8 ms, total: 16.6 s Wall time: 17.9 s In [4]: %%time a = Parallel(n_jobs=2)(delayed(multiply)(i) for i in range(0,100000)) CPU times: user 1.09 .. 2023. 8. 10.
파이썬에서 엘라스틱서치 인덱스 컬럼이름 확인하기 파이썬에서 엘라스틱서치 인덱스 컬럼이름 확인하기 es.indices.get_mapping 을 활용하여 컬럼이름을 확인 합니다. #!pip install elasticsearch==5.5.3 from elasticsearch import Elasticsearch es = Elasticsearch( ["localhost:9200"], ) # query index_name = "naver_news" res = es.indices.get_mapping(index=index_name) # json return from pprint import pprint pprint(res[index_name]["mappings"]["properties"]) # out : {'category': {'fields': {'keywo.. 2023. 8. 9.
load iris dataframe for sample data load iris dataframe for sample data click for data detaill import pandas as pd from sklearn.datasets import load_iris iris = load_iris() df = pd.DataFrame(iris.data, columns=iris.feature_names) df['class'] = iris.target df.tail() 2023. 8. 9.
check pytorch tensor size check pytorch tensor size import torch ## allocate sample tensor to x x = torch.FloatTensor([ [1, 2], [3, 4], [5, 6], [7, 8] ]) ## size with "size" method x.size() # out : torch.Size([4, 2]) ## size with "shape" method x.shape # out : torch.Size([4, 2]) ## dimension with "dim" method x.dim() # out : 2 ## size on 0 dimension with "size(0)" method x.size(0) # out : 4 x.shape[0] # out: 4 2023. 8. 9.
넘파이 어레이를 이용해서 데이터프레임 만들기 넘파이 어레이를 이용해서 데이터프레임 만들기¶ In [1]: # 샘플 어레이 생성 import numpy as np sample_array = np.random.randint(10, 15, (5,3)) sample_array Out[1]: array([[10, 10, 12], [14, 10, 13], [14, 10, 14], [10, 14, 10], [12, 12, 12]]) In [2]: # 컬럼 및 인덱스 이름 생성 index_names = ["product_1", "product_2", "product_3", "product_4", "product_5"] column_names = ["season_1", "season_2", "season_3"] # 데이터 프레임 생성 import pandas as.. 2023. 8. 9.
파이토치 어레이를 텐서 타입으로 array to tensor 파이토치 어레이를 텐서 타입으로 array to tensor¶ In [1]: # 패키지 불러오기 import torch In [2]: # 샘플 어레이 생성 import numpy as np x_array = np.array([[1, 2], [3, 4]]) print(x_array, type(x_array)) [[1 2] [3 4]] In [3]: # 어레이를 텐서로 변경 x_tensor = torch.from_numpy(x_array) print(x_tensor, type(x_tensor)) tensor([[1, 2], [3, 4]]) In [4]: # 텐서를 다시 넘파이 어레이로 변환 x_array = x_tensor.numpy() print(x_array, type(x_array)) [[1 2] [3.. 2023. 8. 9.
반응형