LoginSignup
4
14

More than 5 years have passed since last update.

1章 Python入門 ゼロから作るDeeplearningのいいとこだけ切り取る

Last updated at Posted at 2017-02-25

Python

pythonは、大学や専門学校、コンピューターサイエンスの授業では使われている。
Google, MS, FBなでのIT企業でも使われている。
機械学習つかうならPythonって言う流れ。

Pythonのバージョン

2系と3系がある。今回は3系ですべて書いていきます。

外部ライブラリ

  • Numpy
  • Matplotlib

Pythonのインタプリタ

算術計算

>>> 1 - 2
-1
>>> 4 * 5
20
>>> 7 / 5
1.4
>>> 3 ** 2
9

データ型

>>> type(10)
int
>>> type(2.718)
float
>>> type('hello')
str

変数

>>> x = 10
>>> print(x)
10
>>> x = 100
>>> y = 3.14
>>> x * y
314.0

リスト

>>> a = [1,2,3,4,5]
>>> len(a)
5
>>> a[0]
1
>>> a[0:2]
[1,2]
>>> a[1:]
[2,3,4,5]
>>> a[:-1]
[1,2,3,4]

ディクショナリ

>>> me = {'height': 100 }
>>> me['height']
100

ブーリアン

>>> hungry = True
>>> sleepy = False

if文

>>> hungry = True
>>> if hungry:
...  print('I'm hungry')
...

for文

>>> for i in [1,2,3]:

関数

>>> def hello():

クラス

class name
    def __init__(self, xxx, xxx) # constructor
        ...

    def method1 # method1
        ...
    def method2 # method2
        ...

for expample
```
class Man
def init(self, name)
self.name = name
print('init')

def hello(self):
   print('hello' + self.name)

def goodbye(self)
   print('goodby' + self.name)

# Numpy

## import

import numpy as np
```

numpyをnpとして読み込むことができます。

配列の生成

>>> x = np.array([1.0,2.0,3.0])
>>> print(x)
[1.2.3.]
>>> type(x)
numpy.ndarray

計算

>>> x = np.array([1.0, 2.0, 3.0])
>>> y = np.array([2.0, 4.0, 6.0])
>>> x + y
array([3., 6., 9.])
>>> x- y
array([-1., -2., -3.])
>>> x * y
array([2., 8., 18.])
>>> x / y
array([0.5, 0.5, 0.5])
>>> x = np.array([1.0, 2.0, 3.0])
>>> x/2.0
array([0.5, 1., 1.5])

x,yの要素数が同じじゃないといけない

N次元配列

>>> A = np.array([[1,2], [2,3]])
>>> print(A)
[
 [1,2]
 [3,4]
]
>>> A.shape
(2,2)
>>> A.dtype
dtype('int64')

>>> B = np.array([3,0], [0,6])
>>> A+B
array([4,2], [3,10])
>>> A*B
array([3,0], [0,24])
>>> print(A)
[[1 2] [3 4]]
>>> A * 10
[[10 20][30 40]]

ブロードキャスト

A = np.array([1,2][3,4])
B = np.array([10, 20])
A*B
array([10, 40][30, 80])

要素へのアクセス

>>> X = np.array([51,55], [14,19], [0,4])
>>> print(X)
>>> X[0][1]
55
for row in X:
    print(row)

[51 55]
[14 19]
[0 4]

1次元配列への変換
```
X = X.flattern()
[51 55 14 19 0 4]

X[np.array([0, 2, 4])]
array([51, 14, 0])
X > 15
array([T, T, F, T, F, F])
X[X>15]
array([51,55,19])
```

Matplotlib

単純なグラフの描画

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 6, 0.1)
y = np.sin(x)

plt.plot(x, y)
plt.show() ## グラフ描画

pyplot機能

  • グラフを複数表示可能
  • 画像も表示可能
4
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
14