Category: Python

  • Python for文

    Pythonで繰り返し処理をする時に使う 「for文」はイテラブルオブジェクトを繰り返す。

    イテラブルオブジェクト

    • リスト
    • タプル
    • 文字列
    • 辞書
    • 集合
    • range オブジェクト
    • ジェネレータ

    リストを繰り返す

    fruits = [“apple”, “banana”, “cherry”]
    for fruit in fruits:
    print(fruit)

  • Python update

    When upgrading Python, it is easier to remove the old Python to make it easier to set up.

  • Python 3.9 -> 3.11.4 – Mac

    % nano ~/.bash_profile

    PATH=”/Library/Frameworks/Python.framework/Versions/3.11/bin:${PATH}”
    export PATH

    control + x

    Y

    enter

    % source ~/.bash_profile

    % python3 -V

  • I don’t know how to PATH on Mac

    I have updated python, but when I check in the terminal, I see the previous version

  • scikit-learn

    import sklearn.datasets
    import sklearn.linear_model
    import sklearn.model_selection
    import matplotlib.pyplot as plt
    import pandas as pd

    diabetes = sklearn.datasets.load_diabetes()
    df = pd.DataFrame(diabetes.data, columns=diabetes.feature_names)
    df

    diabetes.target

    x = diabetes.data
    y = diabetes.target

    x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y, test_size=0.2)

    lr = sklearn.linear_model.LinearRegression()
    lr.fit(x_train, y_train)

    lr.score(x_test, y_test)

    predicted = lr.predict(x)
    fig, ax = plt.subplots()
    ax.scatter(y, predicted, edgecolors=(0, 0, 0))
    ax.plot([y.min(), y.max()], [y.min(), y.max()], “k–“, lw=4)
    ax.set_xlabel(“Measured”)
    ax.set_ylabel(“Predicted”)
    plt.show()

  • numpy

    import matplotlib.pyplot as plt
    import numpy as np

    xy図
    t = np.arange(0, 5, 0.2)
    plt.plot(t, t, ‘r–‘, label=’y=x’)
    plt.plot(t, t**2, ‘bs’, label=’y=x**2′)
    plt.plot(t, t**3, ‘g^’, label=’y=x**3′)
    plt.legend()
    plt.show()

    散布図
    x = np.random.rand(50)
    y = np.random.rand(50)
    sizes = np.random.rand(50) * 100
    colors = np.random.rand(50)
    plt.scatter(x, y, s=sizes, c=colors)
    plt.show

    棒グラフ
    values = [1, 2, 3, 4, 5, 6]
    x_pos = np.arange(len(values))
    objects = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]

    plt.bar(x_pos, values)
    plt.xticks(x_pos, objects)
    plt.show()

    円グラフ
    rates = [10, 20, 30, 40]
    labels = [‘Python’, ‘C++’, ‘Ruby’, ‘Java’]
    colors = [‘red’, ‘green’, ‘yellow’, ‘blue’]

    plt.pie(rates, labels=labels, colors=colors, autopct=’%1.1f%%’)
    plt.axis(‘equal’)
    plt.show()

  • ループ処理 for 〜

    a = [‘a’, ‘b’, ‘c’]
    for s in a:
    print(s)

  • import matplotlib.pyplot as plt

    matplotlib を import する際は .pyplot も必要。
    これを付けないと下記のエラーになる。
    AttributeError: module ‘matplotlib’ has no attribute ‘show’

  • import pandas as pd .csvデータを読み込む

    df = pd.read_csv(‘ファイルパス.csv’, encoding=’shift_jis’)
    print(df)

    毎回検索。。。
    メモとして残す。

  • scatter サンプルコード

    plt.scatter(x[:,0], x[:,2], c=y, cmap=’rainbow’)

    plt.grid()

    plt.xlabel(columns[0])
    plt.ylabel(columns[2])

    plt.title(‘アイリスデータセットによる散布図’)

    plt.show()