Pythonはシンプルな文法と豊富なライブラリを備え、さまざまな分野で活用されています。
1. データ操作・解析
🔹 Pandas – データ操作(Data Manipulation)
Pandasは、表形式のデータを効率的に処理できるライブラリです。データの読み込み、クリーニング、フィルタリング、変換など、多くの機能を提供します。
📌 サンプルコード:
import pandas as pd
# CSVファイルの読み込み
df = pd.read_csv("data.csv")
# データの確認
print(df.head())
# 特定の条件でフィルタリング
filtered_df = df[df["age"] > 30]
2. 機械学習
🔹 Scikit-Learn – 機械学習(Machine Learning)
Scikit-Learnは、機械学習アルゴリズムを簡単に実装できるライブラリです。分類・回帰・クラスタリングなど、多様な機械学習タスクに対応しています。
📌 サンプルコード:
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
# データのロード
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)
# モデルの作成と学習
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
# 予測
predictions = model.predict(X_test)
3. ディープラーニング
🔹 TensorFlow – 深層学習(Deep Learning)
TensorFlowはGoogleが開発したディープラーニング向けのライブラリです。
📌 サンプルコード:
import tensorflow as tf
from tensorflow.keras import layers
# モデルの定義
model = tf.keras.Sequential([
layers.Dense(64, activation='relu', input_shape=(10,)),
layers.Dense(32, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
# コンパイル
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
4. データ可視化
🔹 Matplotlib & Seaborn – データ可視化(Data Visualization)
MatplotlibやSeabornを使うことで、折れ線グラフやヒートマップなどを簡単に作成できます。
📌 サンプルコード(ヒートマップ):
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
data = np.random.rand(10, 10)
sns.heatmap(data, annot=True)
plt.show()
5. Web開発
🔹 Flask – 軽量なWebアプリ開発
FlaskはシンプルなWebフレームワークで、APIの構築に最適です。
📌 サンプルコード:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask!"
if __name__ == "__main__":
app.run(debug=True)
🔹 FastAPI – 高速なAPI開発 🚀
FastAPIは、Flaskよりも高速で非同期処理に対応したAPIフレームワークです。データのバリデーションやOpenAPIドキュメントが自動生成されるのも魅力です。
📌 サンプルコード(簡単なAPIサーバー):
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "query": q}
✨ FastAPIのメリット
- 自動ドキュメント生成(Swagger UI対応)
- 非同期処理対応(async/await)
- データバリデーションが簡単
FastAPIを使うことで、スピーディーにAPI開発ができます!
6. ゲーム開発
🔹 Pygame – ゲーム開発(Game Development)
Pygameは2Dゲーム開発に適したライブラリです。
📌 サンプルコード(ウィンドウを開く):
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Pygame Window")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
7. モバイルアプリ開発
🔹 Kivy – モバイルアプリ開発(Mobile App Development)
Kivyを使うことで、クロスプラットフォームのアプリが開発できます。
📌 サンプルコード:
from kivy.app import App
from kivy.uix.button import Button
class MyApp(App):
def build(self):
return Button(text="Hello, Kivy!")
MyApp().run()
8. GUI開発
🔹 Tkinter – デスクトップアプリのGUI開発(GUI Development)
TkinterはPython標準ライブラリで、簡単なGUIアプリを作成できます。
📌 サンプルコード:
import tkinter as tk
root = tk.Tk()
root.title("Tkinter Window")
label = tk.Label(root, text="Hello, Tkinter!")
label.pack()
root.mainloop()