-
Python 기초 : Python Library(2)(Matplotlib, Seaborn, BeautifulSoup, Scikit-Learn)BootCamp/FastCampus x Upstage AI Lab 6기 2024. 11. 20. 16:18
데이터 시각화 라이브러리(Matplotlib)
- Matplotlib는 파이썬에서 데이터를 차트나 플롯으로 시각화하는 라이브러리이다.
- matplotlib.pyplot 모듈의 함수를 이용하여 간편하게 그래프를 만들 수 있다.
- 설치 방법: pip install matplotlib
- 사용 방법: import matplotlib.pyplot as plt
- Matplotlib에 대한 다양한 설명은 Matplotlib 공식 웹사이트에서 확인할 수 있다.
Matplotlib — Visualization with Python
seaborn Cartopy DNA Features Viewer plotnine WCS Axes seaborn seaborn is a high level interface for drawing statistical graphics with Matplotlib. It aims to make visualization a central part of exploring and understanding complex datasets. statistical data
matplotlib.org
# 3. Matplotlib를 활용한 시각화 import matplotlib.pyplot as plt # 데이터 준비 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # 선 그래프 그리기 plt.plot(x, y, label='Line') plt.title("Simple Line Plot") plt.xlabel("X-axis") plt.ylabel("Y-axis") plt.legend() plt.show() # 결과: 선 그래프가 X축과 Y축 값으로 플롯됨
고급 데이터 시각화(Seaborn)
- Seaborn은 Matplotlib 기반의 시각화 라이브러리이다.
- 유익한 통계 기반 그래픽을 그리기 위한 고급 인터페이스를 제공한다.
- 설치 방법: pip install seaborn
- 사용 방법: import seaborn as sns
- Seaborn에 대한 다양한 설명은 Seaborn 공식 웹사이트에서 확인할 수 있다.
seaborn: statistical data visualization — seaborn 0.13.2 documentation
seaborn: statistical data visualization
seaborn.pydata.org
# 4. Seaborn으로 고급 시각화 import seaborn as sns # 샘플 데이터 준비 tips = sns.load_dataset('tips') # 히스토그램 그리기 sns.histplot(data=tips, x='total_bill', bins=20, kde=True) plt.title("Total Bill Distribution") plt.show() # 결과: Total Bill의 분포를 보여주는 히스토그램 출력
웹 데이터 수집 라이브러리(BeautifulSoup)
- BeautifulSoup는 HTML 및 XML에서 데이터를 쉽게 처리하는 파이썬 라이브러리이다.
- HTML은 태그로 이루어져 있으며, 공백과 변화하는 소스들 때문에 오류가 발생할 가능성이 높다.
- BeautifulSoup을 이용하면 이러한 오류를 잡아서 고친 후 데이터를 전달할 수 있다.
- 설치 방법: pip install beautifulsoup4
- 사용 방법: from bs4 import BeautifulSoup
- BeautifulSoup에 대한 다양한 설명은 BeautifulSoup 공식 웹사이트에서 확인할 수 있다.
Beautiful Soup Documentation — Beautiful Soup 4.12.0 documentation
Beautiful Soup Documentation Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers h
www.crummy.com
# 5. BeautifulSoup를 활용한 웹 데이터 크롤링 from bs4 import BeautifulSoup # HTML 코드 예시 html_code = """ <html> <body> <h1>웹 제목</h1> <p>첫 번째 문단</p> <p>두 번째 문단</p> </body> </html> """ # BeautifulSoup 객체 생성 soup = BeautifulSoup(html_code, 'html.parser') # 태그 선택 및 출력 print("제목:", soup.h1.text) # 제목: 웹 제목 print("문단 목록:") for p in soup.find_all('p'): print("-", p.text) # 문단 목록: # - 첫 번째 문단 # - 두 번째 문단
머신러닝 라이브러리(Scikit-Learn)
- Scikit-Learn은 파이썬 기반의 머신러닝을 위한 가장 쉽고 효율적인 개발 라이브러리이다.
- 머신러닝은 곧 Scikit-Learn을 의미할 정도로 오랜 기간 파이썬 영역에서 인정받아왔다.
- 다양한 알고리즘과 개발을 위한 프레임워크 및 API를 제공한다.
- 설치 방법: pip install scikit-learn
- 사용 방법: import scikit-learn
- Scikit-Learn에 대한 다양한 설명은 Scikit-Learn 공식 웹사이트에서 확인할 수 있다.
scikit-learn: machine learning in Python — scikit-learn 1.5.2 documentation
Comparing, validating and choosing parameters and models. Applications: Improved accuracy via parameter tuning. Algorithms: Grid search, cross validation, metrics, and more...
scikit-learn.org
# 6. Scikit-Learn을 이용한 머신러닝 from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error # 데이터 준비 X = np.array([[1], [2], [3], [4], [5]]) y = np.array([2, 4, 6, 8, 10]) # 데이터 분리 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 모델 학습 model = LinearRegression() model.fit(X_train, y_train) # 예측 및 평가 y_pred = model.predict(X_test) print("테스트 데이터의 예측값:", y_pred) # 테스트 데이터의 예측값: [10.] print("평균 제곱 오차 (MSE):", mean_squared_error(y_test, y_pred)) # 평균 제곱 오차 (MSE): 0.0
'BootCamp > FastCampus x Upstage AI Lab 6기' 카테고리의 다른 글
Python 기초 : 데이터 시각화 (Data Visualization) (0) 2024.11.22 Python 기초 : 크롤링(Crawling), 인코딩(Encoding), 디코딩(Decoding) (0) 2024.11.21 Python 기초 : Python Library(1)(Pandas, Numpy) (1) 2024.11.20 국민내일배움카드 훈련과정 출석 관리용 엑셀 (부트캠프 etc..) (0) 2024.11.19 Python 기초 : 클래스와 모듈 (2) 2024.11.19