SlideShare a Scribd company logo
1 of 46
Download to read offline
PythonとPyDataの基礎-
TFUG Utsunomiya #12
お前誰?-
l  櫻岡響
l  電気電子システム工学専攻 修
士2年
l  音声研に所属
l  言語処理とかやってます
2
AGENDA-
p  Python基礎-
p  基本のデータ構造
p  基本の制御構造
p  PyData-
p  Numpyでベクトル計算
p  正弦波の作図
p  W杯のデータで遊ぶ
AGENDA-
p  Python基礎
p  基本のデータ構造-
p  基本の制御構造
p  PyData
p  Numpyでベクトル計算
p  正弦波の作図
p  W杯のデータで遊ぶ
変数代入とprint-
p  jupyter notebookの場合セルの最後がprint
される
a = 'hello World'!
print(a)!
a = 'hello World'!
a!
5
Pythonのデータ構造-
p  数値
p  文字列
p  真偽値
p  リスト
p  タプル
p  辞書
6
数値-
p  int 1, 2, 3!
p  float 0.2, 3.4, 5.6!
p  主な演算子
p  + 加算
p  - 減算
p  * 乗算
p  / 除算
p  % 剰余
p  ** 累乗
p  13と6で全部やってみましょう
7
文字列-
8
p  'Hello' or "Hello"!
p  主な演算子
p  + 結合
'Hello' + ' ' + 'World'!
論理値-
9
p  True, False!
p  主な演算子
p  and / or / not!
True and False!
リスト 1/3-
10
p  配列のようなもの
p  型を混在させて格納できる
a = [ 2, 'foo', 6.4, 'bar', True]!
a
リスト 2/3-
11
p  値の取り出し方
p  a[1:3]みたいな書き方→スライス
a[0]!
a[1]!
a[-1]!
a[1:3]
リスト 3/3-
12
p  代入
p  下を実行後のaを見てみましょう
a[1] = 'Hello'!
a!
タプル-
13
p  リストとほぼ一緒
p  再代入出来ない
a = (2, ‘foo’, 6.4, ‘bar’, True)!
print(a)!
a[0] = 'Hello'
辞書-
14
p  連想配列
foo = {'a':100, 'b':200, 'c':300}!
foo['b']!
型の変換-
15
p  str、 float、boolなどの関数をつかう
float(5)
AGENDA-
p  Python基礎
p  基本のデータ構造
p  基本の制御構造-
p  PyData
p  Numpyでベクトル計算
p  正弦波の作図
p  W杯のデータで遊ぶ
Pythonの制御構造-
p  if
p  for
p  インデントで階層を表す
17
if-
18
p  条件分岐
score = 50!
if score < 60:!
print('成績は不可です')!
elif score >=90:!
print('成績は秀です')!
else:!
print('合格です')
for-
19
p  繰り返し
p  range(5)に書き換えてみましょう
for i in [0,1,2,3,4]:!
print(i)
Let’s FizzBuzz-
p  Step1.-
p  0から100までprintするプログラムを書いて
ください-
p  Step2.-
p  以下の仕様に変更してください-
p 3の倍数のときはFizz
p 5の倍数のときはBuzz
p 15の倍数のときはFizzBuzz
20
AGENDA-
p  Python基礎
p  基本のデータ構造
p  基本の制御構造
p  PyData
p  Numpyでベクトル計算-
p  正弦波の作図
p  W杯のデータで遊ぶ
そのまえに-
関数の実行と属性呼び出し-
23
p  関数の実行
p  some_func(arg)!
p  属性の呼び出し
p  some_object.attr!
import-
p  import hoge!
p  グローバルにhogeオブジェクトが定義され
る
p  from hoge import fuga !
p  hogeモジュールにあるfugaオブジェクトを
定義
24
では行きます-
Numpy-
26
p  数値計算用ライブラリ
p  ベクトル・行列計算ができる
import numpy as np
http://www.numpy.org/
Numpy配列の作り方 1/2-
27
p  numpy.array関数を使う
a = np.array([2,4,6])!
b = np.array([!
[1,2,3],!
[4,5,6],!
[7,8,9]!
])!
Numpy配列の作り方 2/2-
p  numpy.arange
p  range関数のNumpy版
p  numpy.identity
p  単位行列
p  numpy.zeros
p  ゼロ行列
p  numpy.linspace !
p  startからstopまでnum個に分割
28
Numpy配列の演算子など-
29
p  +, - 加算、減算!
p  * 要素毎の積
p  b.dot(a) ドット積
p  a.T 転置
b.dot(a).T
Universal functions-
p  Numpy配列に対して適用出来る関数
p  numpy.sin!
p  numpy.cos!
p  numpy.tan!
p  numpy.exp!
p  numpy.log!
30
練習-
31
p  以下のx, yを作ってみてください
p  x : -πからπを100個に分割
p  y : xの要素それぞれにsinを適用
AGENDA-
p  Python基礎
p  基本のデータ構造
p  基本の制御構造
p  PyData
p  Numpyでベクトル計算
p  正弦波の作図-
p  W杯のデータで遊ぶ
matplotlib-
33
p  作図をするためのライブラリ
from matplotlib import pyplot as plt!
https://matplotlib.org/
正弦波の作図-
34
p  さきほどのx、yを使いましょう
fig, ax = plt.subplots(figsize=(12,9))!
ax.plot(x,y)!
ax.set_xlabel('$x$')!
ax.set_ylabel('$sin(x)$')!
ax.set_title('$y = sin(x)$')
AGENDA-
p  Python基礎
p  基本のデータ構造
p  基本の制御構造
p  PyData
p  Numpyでベクトル計算
p  正弦波の作図
p  W杯のデータで遊ぶ-
Pandas-
36
p  統計処理のためのライブラリ
p  pandas.Series!
p  1次元の名前付きデータ列
p  pandas.DataFrame!
p  2次元の分割表
import pandas as pd!
pd.options.display.max_rows = 10
https://pandas.pydata.org/
DLお願いします-
https://goo.gl/VPjb8K-
CSVの読み込み方-
38
p  出典
p  2018 FIFA World Cup Squads
p  https://www.kaggle.com/cclayford/2018-fifa-
world-cup-squads
data = pd.read_csv(!
'2018 FIFA World Cup Squads.csv'!
)!
ワールドカップ!-
データ抽出 1/2-
p  列
p  data['Team'] -> Series!
p  行
p  data.iloc[123] -> Series!
p  範囲
p  data.iloc[0:5, 3:6] -> DataFrame!
40
データ抽出 2/2-
41
p  boolのSeriesでの抽出
p  cはなんでしょうか?
p  DataFrame[c]が出来るのがポイント
c = (data['Group'] == 'A')!
data[c]
練習1 /4-
p  日本人選手の名前をprintしましょう
42
練習2 /4-
p  ゴール数が最多の選手の名前と国をprintし
ましょう
p  pandas.Series.idxmax()!
43
練習3 /4-
p  W杯登録選手を輩出している上位5クラブ
を挙げましょう
p  pandas.Series.value_counts()
44
練習4 /4-
p  出場試合数(Caps)とゴール数(Goals)を散
布図にしましょう
p  ax.scatter(x,y)!
p  x,yはpandas.SeriesでもOK
45
オマケ-
p  seaborn
p  matplotlibのラッパー
p  統計的な作図が簡単に出来る
p  Nobebookに入れときました
p  matplotlibよりカスタマイズはしにくいの
で注意
46

More Related Content

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

PythonとPyDataの基礎