<jupyter_start><jupyter_text>SoftwareDesign 2020年10月号 第1特集 Pythonではじめる統計学雑誌からの写経 第1章 統計分析に必須のライブラリ  リスト1<jupyter_code>import seaborn as sns

tips = sns.load_dataset("tips")
sns.catplot(
    data=tips, x="time", y="tip", kind="violin"
)<jupyter_output><empty_output><jupyter_text>リスト2標本空間が $\{a_1, \dotsc, a_n\}$ であるとき、その算術平均 $(\displaystyle A)$ は次のとおりに定義される。$$A = \frac{1}{n}\sum_{k=1}^{n} a_k = \frac{a_1 + a_2 + \dotsb + a_n}{n}$$  NumPy入門  ndarray型<jupyter_code>import numpy as np

int_arr = np.array([1, 2])
int_arr.dtype
float_arr = np.array([1, 2], dtype=np.float32)
float_arr.dtype
float_arr.astype(np.int)<jupyter_output><empty_output><jupyter_text>ndarrayオブジェクト生成<jupyter_code>np.arange(3)
np.arange(0, 2.5, 0.5)
np.zeros(3)
np.ones([2, 3])
np.random.seed(1)
np.random.rand(3,3)
arr1 = np.arange(1, 4)
arr1
arr2 = np.arange(1, 13).reshape(4, 3)
arr2<jupyter_output><empty_output><jupyter_text>要素へのアクセス<jupyter_code>arr1[0]
arr1[1:3]
arr2[0]
arr2[1, 2]
arr2[0, 1:3]
arr1[1] = 10
arr1
arr2[1, :2] = 100
arr2<jupyter_output><empty_output><jupyter_text>ブロードキャスト<jupyter_code>li = [1, 2, 3]
[x + 1 for x in li]
arr1 = np.array([1, 2, 3])
arr1 + 1
# 前述のコードから続けてそのまま動かすと、テキストと異なる結果になる
# 恐らく、arr2を途中で変えたことを忘れたか、無視したものと考えられる
# このため、テキストにはないが改めてarr2の初期化を行う
arr2 = np.arange(1, 13).reshape(4, 3)
arr1 + arr2<jupyter_output><empty_output><jupyter_text>関数の適用<jupyter_code>np.power(arr1, arr2)
abs(np.array([1, -2, 3]))
sum(np.array([1, 2, 3]))
def my_func(x):
  return x ** 2 + 1

my_func(arr1)<jupyter_output><empty_output><jupyter_text>pandas入門  Series型<jupyter_code>import pandas as pd

ser = pd.Series([1, 2, 3], index=["a", "b", "c"])
ser
ser.values<jupyter_output><empty_output><jupyter_text>DataFrame型<jupyter_code>pd.DataFrame(
    [[1, 10], [2, 20]],
    index=["r1", "r2"],
    columns=["c1", "c2"]
)
diff_types_df = pd.DataFrame(
    [[0.1, 1], [0.2, 2]]
)
diff_types_df.dtypes
diff_types_df.values.dtype<jupyter_output><empty_output><jupyter_text>要素へのアクセス<jupyter_code>rc_df = pd.DataFrame(
    [
     ["r1c1", "r1c2", "r1c3"],
     ["r2c1", "r2c2", "r2c3"],
     ["r3c1", "r3c2", "r3c3"]
    ],
    index=["r1", "r2", "r3"],
    columns=["c1", "c2", "c3"]
)
rc_df<jupyter_output><empty_output><jupyter_text>ラベルからアクセス(locインデクサ)<jupyter_code>ser.loc["b"]
rc_df.loc["r2", "c2"]
rc_df.loc["r2":"r3", :]
rc_df.loc[["r1", "r3"], ["c1", "c3"]]<jupyter_output><empty_output><jupyter_text>位置からアクセス(ilocインデクサ)<jupyter_code>ser.iloc[1]
rc_df.iloc[1, 2]
rc_df.iloc[1:, [0, 2]]<jupyter_output><empty_output><jupyter_text>要素の変更<jupyter_code>ser.loc["b"] = 22
ser
rc_df.loc["r1", "c1"] = "R1C1"
rc_df
rc_df.iloc[1, 1] = "R2C2"
rc_df<jupyter_output><empty_output><jupyter_text>ブロードキャスト<jupyter_code>float_ser = pd.Series([1.1, 2.2, 3.3])
int_df = pd.DataFrame(
    [
     [1, 10, 100],
     [2, 20, 200],
     [3, 30, 300]
    ],
)
int_df
float_ser + 1
int_df + 1
int_df + float_ser<jupyter_output><empty_output><jupyter_text>関数の適用<jupyter_code>round(float_ser)
import numpy as np

np.median(float_ser)
def my_func(x):
  return x ** 2 + 1

my_func(float_ser)
float_ser.apply(np.floor)
int_df.apply(sum)
int_df.apply(sum, axis=1)<jupyter_output><empty_output><jupyter_text>基本統計量<jupyter_code>np.random.seed(1)
random_ser = pd.Series(np.random.rand(100))
random_df = pd.DataFrame(
    np.random.rand(100, 4),
    columns=["A", "B", "C", "D"],
)
random_ser.describe()
random_df.describe()<jupyter_output><empty_output><jupyter_text>Python による可視化入門  Matplotlib 入門<jupyter_code>import matplotlib.pyplot as plt

fig, ax = plt.subplots()
fit, ax = plt.subplots(ncols=2, figsize=(6,3))
fig.suptitle("figure")
ax[0].set_title("axes0")
ax[1].set_title("axes1")
import numpy as np

np.random.seed(1)

norm_arr = np.random.randn(10000)
fig, ax = plt.subplots()
ax.hist(norm_arr)
from scipy.stats import norm

# 恐らく、下記の宣言がないためエラーがあったので追加
fig, ax = plt.subplots()
x = np.linspace(-4, 4, 100)
y = norm.pdf(
    x, norm_arr.mean(), norm_arr.std()
)
ax.hist(norm_arr, bins=100, density=True)
ax.plot(x, y)
fig, ax = plt.subplots(ncols=2)
ax[0].hist(norm_arr, bins=100, density=True)
ax[1].plot(x, y)<jupyter_output><empty_output><jupyter_text>seaborn入門<jupyter_code>import seaborn as sns

sns.distplot(norm_arr)
fig, ax = plt.subplots(ncols=2)

sns.distplot(norm_arr, ax=ax[0], kde=False)
sns.distplot(
    norm_arr, ax=ax[1], hist=False, rug=True
)<jupyter_output>/usr/local/lib/python3.6/dist-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)
/usr/local/lib/python3.6/dist-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `kdeplot` (an axes-level function for kernel density plots).
  warnings.warn(msg, FutureWarning)
/usr/local/lib/python3.6/dist-packages/seaborn/distributions.py:2055: FutureWarning: The `axis` variable is no longer used and will be removed. Instead, assign variables directly to `x` or `y`.
  warnings.warn(msg, FutureWarning)
{'fraction_non_alphanumeric': 0.17042226850975195, 'fraction_numerical': 0.04790759325885249, 'mean_word_length': 6.2554945054945055, 'pattern_counts': {'":': 1, '<': 68, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '649516', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2529, 'n_words': 404, 'path': 'SoftwareDesign202010_01/SoftwareDesign202010_0101.ipynb', 'repo_name': 'zfukuoka/Copying_a_sutra', 'size': '5281'}