Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manuscript #58

Merged
merged 7 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/extras/5tex/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ lualatex sample.tex

- `sample.pdf` という名前のファイルができるので、それを開く。

:::tip
Mac を使っている場合は、バックスラッシュ( `\` )は、`Alt` を押しながら `¥` を押せば、打つことができます。
:::

:::tip
LuaLaTeX 以外も使うことができます。pLaTeX で jsarticle を使う例も載せておきます。

Expand Down
10 changes: 6 additions & 4 deletions docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ Python やアルゴリズムについて簡単にまとめていこうかなと

## 更新履歴

10/10 第一周の分を執筆 [ここから](/docs/python/01google-colaboratory/)
11/6 range 関数についてを補足 [この下](http://localhost:3000/docs/python/08array/#%E5%95%8F%E9%A1%8C-1)

10/10 練習問題を追加
10/30 第三週の分を執筆 [ここから](/docs/python/07for/)

10/23 第二週の分を執筆 [ここから](/docs/python/05function/)

10/16 練習問題をさらに追加

10/23 第二週の分を執筆 [ここから](/docs/python/05function/)
10/10 練習問題を追加

10/30 第三週の分を執筆 [ここから](/docs/python/07for/)
10/10 第一周の分を執筆 [ここから](/docs/python/01google-colaboratory/)
2 changes: 2 additions & 0 deletions docs/python/05function/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ $G=6.7\times 10^{-11}$、$r=2$、$M=60$、$m=20$ とします。
<Answer>
<ViewSource path="_samples/introduce_language.ipynb" />

:::note
`f-string` を使わないと次のようになります。

<ViewSource path="_samples/introduce_language2.ipynb" />
:::
</Answer>
27 changes: 27 additions & 0 deletions docs/python/07for/_samples/upto_sum.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"sum(range(1, 11))"
],
"cell_type": "code",
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"55"
]
},
"metadata": {},
"execution_count": 1
}
],
"execution_count": null
}
]
}
8 changes: 6 additions & 2 deletions docs/python/07for/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,14 @@ while(条件):

### 練習問題 1

1 から n までの和を求める関数を作って、実際に 1 から 10 までの和を求めてみましょう。
$1$ から $n$ までの和を求める関数を作って、実際に 1 から 10 までの和を求めてみましょう。

<Answer>
<ViewSource path="_samples/upto.ipynb" />
<ViewSource path="_samples/upto.ipynb" />

実は、このプログラムは次のようにしてもっと簡単に書くことができます。しかし、次項の配列の内容がわかっていないと理解できないため、次項で詳しく紹介します。

<ViewSource path="_samples/upto_sum.ipynb" />
</Answer>

### 練習問題 2
Expand Down
30 changes: 30 additions & 0 deletions docs/python/08array/_samples/average_numpy.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"import numpy\n",
"\n",
"scores = [26, 78, 83, 20, 10, 11, 22, 16, 41, 95]\n",
"numpy.mean(scores)"
],
"cell_type": "code",
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"40.2"
]
},
"metadata": {},
"execution_count": 1
}
],
"execution_count": null
}
]
}
30 changes: 30 additions & 0 deletions docs/python/08array/_samples/average_statistics.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"import statistics\n",
"\n",
"scores = [26, 78, 83, 20, 10, 11, 22, 16, 41, 95]\n",
"statistics.mean(scores)"
],
"cell_type": "code",
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"40.2"
]
},
"metadata": {},
"execution_count": 1
}
],
"execution_count": null
}
]
}
26 changes: 26 additions & 0 deletions docs/python/08array/_samples/range.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"print(range(1, 11))\n",
"print(list(range(1, 11)))"
],
"cell_type": "code",
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"range(1, 11)\n",
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n"
]
}
],
"execution_count": null
}
]
}
27 changes: 27 additions & 0 deletions docs/python/08array/_samples/sum.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"sum([1, 2, 3])"
],
"cell_type": "code",
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"6"
]
},
"metadata": {},
"execution_count": 2
}
],
"execution_count": null
}
]
}
27 changes: 27 additions & 0 deletions docs/python/08array/_samples/sum2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"sum([2, 5, 7])"
],
"cell_type": "code",
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"14"
]
},
"metadata": {},
"execution_count": 1
}
],
"execution_count": null
}
]
}
27 changes: 27 additions & 0 deletions docs/python/08array/_samples/variance_numpy.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"import numpy\n",
"\n",
"scores = [26, 78, 83, 20, 10, 11, 22, 16, 41, 95]\n",
"print(numpy.var(scores))"
],
"cell_type": "code",
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"955.5600000000001\n"
]
}
],
"execution_count": null
}
]
}
27 changes: 27 additions & 0 deletions docs/python/08array/_samples/variance_statistics.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"import statistics\n",
"\n",
"scores = [26, 78, 83, 20, 10, 11, 22, 16, 41, 95]\n",
"print(statistics.pvariance(scores))"
],
"cell_type": "code",
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"955.56\n"
]
}
],
"execution_count": null
}
]
}
30 changes: 28 additions & 2 deletions docs/python/08array/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,32 @@ len(配列名)
<ViewSource path="_samples/change3.ipynb" />
</Answer>

:::info
実は for 文を書くときに出てきた range は、配列のようなものを作る関数です。ここで、配列のようなものと言ったのは厳密には配列ではないからですが、ひとまず配列と考えて問題ないでしょう。そのため、例えば `range(1, 11)` は `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]` を生成します。下のプログラムでは、range 関数の返り値を配列にしています。

<ViewSource path="_samples/range.ipynb" />

前項の練習問題の解答で `sum(range(1, 11))` のような別解がありましたが、sum 関数は与えられた配列の和を求める関数なので、これは range 関数で 1 から 10 までの配列を作り、それの和を sum 関数で求めていたということです。

<ViewSource path="_samples/sum.ipynb" />

<ViewSource path="_samples/sum2.ipynb" />

:::

### 練習問題 1

生徒の英語の点数が書かれた配列を受け取って、その平均点を返す関数を作ってみましょう。
実際に、点数をそれぞれ 26 点、78 点、83 点、20 点、10 点、11 点、22 点、16 点、41 点、95 点として計算してみましょう。

<Answer>
<ViewSource path="_samples/average10.ipynb" />
<ViewSource path="_samples/average10.ipynb" />

実は、平均点を求めるライブラリもあります。[statistics ライブラリの mean 関数](https://docs.python.org/ja/3/library/statistics.html#statistics.mean)や[numpy ライブラリの mean 関数](https://numpy.org/doc/stable/reference/generated/numpy.mean.html)があります。

<ViewSource path="_samples/average_statistics.ipynb" />

<ViewSource path="_samples/average_numpy.ipynb" />
</Answer>

### 練習問題 2
Expand All @@ -88,7 +107,7 @@ len(配列名)
$n$ 個の観測データを $x_1,x_2,\cdots,x_n$ 、平均を $\bar{x}$ とすると、分散 $s^2$ は次のように与えられるとします。

$$
s^2=\frac{\sum_{i=1}^n(x_i-\bar{x})^2}{n}
s^2=\frac{\sum\limits_{i=1}^n(x_i-\bar{x})^2}{n}
$$

練習問題 1 と同じように具体的な値を入れて確かめてみましょう。
Expand All @@ -99,4 +118,11 @@ $$
次のようにしても計算できますが( `variance` 関数の中が少し変わっています。)、for 文の中が実行される度に `average` 関数が実行されて計算量が多くなってしまうので、上のようにした方が良いでしょう。

<ViewSource path="_samples/variance2.ipynb" />

実は、分散を計算するライブラリがあります。[statistics ライブラリの pvariance 関数](https://docs.python.org/ja/3/library/statistics.html#statistics.pvariance)を使う方法や[NumPy ライブラリの var 関数](https://numpy.org/doc/stable/reference/generated/numpy.var.html)があります。

<ViewSource path="_samples/variance_statistics.ipynb" />

<ViewSource path="_samples/variance_numpy.ipynb" />

</Answer>