- 이 코드는 데이터 프레임을 libsvm 형식으로 변경합니다.
- 희소 행렬에서 0을 제거하여 데이터 프레임에서 libsvm을 생성합니다.
- 다음 URL로 테스트했습니다.
- nan을 사용하면 예외가 발생합니다. (nan 값이 아닌 0 값이 대부분 값으로 취급되기 때문에)
datascience.stackexchange.com/questions/73157/convert-pandas-dataframe-with-mixed-datatypes-to-libsvm-format
import pandas as pd
from sklearn.datasets import dump_svmlight_file
def df_to_libsvm(df: pd.DataFrame):
x = df.drop('label', axis=1)
y = df('label')
dump_svmlight_file(X=x, y=y, f="libsvm.dat", zero_based=True)
df = pd.DataFrame(
{'label' : (1, 2, 3, 4, 5),
'index0' : (6, 7, 0, 9, 10),
'index1' : (11, 12, 13, 14, 15)},
)
print(df)
df_to_libsvm(df)


![[JS] Primitive [JS] Primitive](https://cafe.pageof.kr/wp-content/plugins/contextual-related-posts/default.png)