생각/파이썬 , R

파이썬 라이브러리와 예시

kyunghoonk00k 2023. 6. 20. 13:21
반응형
  1. NumPy (수치 계산 라이브러리)
python
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a + b
print(c)
  1. Pandas (데이터 분석 라이브러리)
python
import pandas as pd

data = {'name': ['John', 'Alice', 'Bob'], 'age': [32, 24, 29]}
df = pd.DataFrame(data)
print(df)
  1. Matplotlib (시각화 라이브러리)
python
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
  1. Scikit-learn (머신러닝 라이브러리)
python
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

X = np.random.rand(100, 1)
y = 2 * X + 1

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
model = LinearRegression()
model.fit(X_train, y_train)
test_predictions = model.predict(X_test)
  1. TensorFlow (딥러닝 라이브러리)
python
import tensorflow as tf

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)
  1. Requests (HTTP 요청 라이브러리)
python
import requests

response = requests.get('https://www.example.com')
print(response.text)
  1. BeautifulSoup (웹 스크래핑 라이브러리)
python
import requests
from bs4 import BeautifulSoup

url = 'https://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
print(soup.title)
반응형

'생각 > 파이썬 , R' 카테고리의 다른 글

파이썬 예시 코드  (0) 2023.06.20
파이썬 기초 문법  (1) 2023.06.19
[TIL] 2022.12.04  (0) 2022.12.04
[파이썬 R 데이터 분석 1강]  (1) 2022.09.25