Python : Library

m_ngyeongΒ·2024λ…„ 10μ›” 10일
0

Python

λͺ©λ‘ 보기
12/17
post-thumbnail

🐍 Python


Library

πŸ“š NumPy: 수치 연산을 μœ„ν•œ 라이브러리

import numpy as np

# λ°°μ—΄ 생성
arr = np.array([1, 2, 3, 4])
print(arr * 2)  # [2 4 6 8]

# λ°°μ—΄μ˜ κΈ°λ³Έ 톡계
print(np.mean(arr))  # 2.5

πŸ“š Pandas: 데이터 뢄석 및 μ‘°μž‘μ„ μœ„ν•œ 라이브러리

import pandas as pd

# λ°μ΄ν„°ν”„λ ˆμž„ 생성
data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35]
}
df = pd.DataFrame(data)
print(df)

# λ°μ΄ν„°ν”„λ ˆμž„ μ‘°μž‘
print(df['Age'].mean())  # 30.0

πŸ“š Matplotlib: 데이터 μ‹œκ°ν™”λ₯Ό μœ„ν•œ 라이브러리

import matplotlib.pyplot as plt

# 데이터
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# μ„  κ·Έλž˜ν”„ 생성
plt.plot(x, y, marker='o')
plt.title('Line Graph')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

πŸ“š Requests: HTTP μš”μ²­μ„ 보내기 μœ„ν•œ 라이브러리

import requests

# GET μš”μ²­ 보내기
response = requests.get('https://api.github.com')
print(response.status_code)  # 200
print(response.json())  # 응닡 λ³Έλ¬Έ 좜λ ₯

πŸ“š BeautifulSoup: μ›Ή μŠ€ν¬λž˜ν•‘μ„ μœ„ν•œ 라이브러리

from bs4 import BeautifulSoup
import requests

# μ›Ή νŽ˜μ΄μ§€ μš”μ²­
response = requests.get('https://www.example.com')
soup = BeautifulSoup(response.text, 'html.parser')

# HTML μš”μ†Œ μΆ”μΆœ
print(soup.title.string)  # μ›Ή νŽ˜μ΄μ§€μ˜ 제λͺ© 좜λ ₯

πŸ“š Scikit-Learn: 기계 ν•™μŠ΅μ„ μœ„ν•œ 라이브러리

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# 데이터 λ‘œλ“œ
iris = load_iris()
X, y = iris.data, iris.target

# 데이터 λΆ„ν• 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# λͺ¨λΈ ν›ˆλ ¨
model = RandomForestClassifier()
model.fit(X_train, y_train)

# 예츑 및 평가
predictions = model.predict(X_test)
print(accuracy_score(y_test, predictions))  # 정확도 좜λ ₯


profile
ΚšΘ‰Ιž

0개의 λŒ“κΈ€