2
1

Python学んでみた 第5回(numpy,Matplotlib)

Last updated at Posted at 2024-03-03

はじめに

初めまして!
エンジニアになって数年、今まで本を読むだけでしたが、もっとプライベートで楽しみながら成長したい!自分が学んだ足跡を残していきたい!と思い記事を書きました!
最終的には自在に開発できるようになりたいと思っています。:triumph:
いろいろな記事を参考にさせてもらっています。:bow_tone2:
その中でもこれってどういう意味?とかつまづいたところを念入りに書いていこうかと思います。:fist:

今回の目的

前回の続きです。

使用したものや事前準備

・Macbook Pro
・Python
・Anaconda
・jupyter notebook

Python

numpy

行列操作をおこうなことができるサードパーティのライブラリ。ソースコード内にnpといたらnumpyを指していると思っていい。600を超える関数を揃えている。
特徴は行列操作が速い。行列の入れ替えやスライス、フィルタリングによる抽出もできたりやれることは豊富。
ただ、一回で理解し切れる量ではなかったので省略しています。:sob:

import numpy as np
digits = np.array(range(10))
digits # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

計算

ループ文を使用しなくても全ての値を計算できる

digits # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
digits + 10 # array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
digits + digits # array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
np.sin(digits) # array([ 1. ,  0.54030231, -0.41614684, -0.9899925 , -0.65364362, 0.28366219,  0.96017029,  0.75390225, -0.14550003, -0.91113026])

配列生成

二次元配列が作成可能。
np.eyeは主対角線(indexが同じ)を1とした二次元配列を生成します。

np.arange(3) # array([0, 1, 2])
np.ones(3) # array([1., 1., 1.])
np.eye(3 ,5)
#array([[1., 0., 0., 0., 0.],
#       [0., 1., 0., 0., 0.],
#       [0., 0., 1., 0., 0.]])

乱数生成

np.random.random(3) # array([0.30741411, 0.20918913, 0.26900705])

Matplotlib

Pythonでグラフ(折れ線グラフ、散布図、ヒストグラム、棒グラフなど)を描画するためのライブラリ
こちらも省略しています。

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1)
y = np.cos(x)
fig = plt.figure() # 描画
ax = fig.add_subplot(221) # 左上
ax2 = fig.add_subplot(222) # 右上
ax.plot(x, y) # 折れ線グラフ生成
ax2.plot(x, y) # 折れ線グラフ生成

plt.savefig('test.png', dpi=300)

スクリーンショット 2024-03-03 22.04.27.png

スクリーンショット 2024-03-03 22.05.04.png

スクリーンショット 2024-03-03 22.06.08.png

最後に

最低限の内容は抑えられたと思いますが、勉強のしがいがあるプログラミング言語だと思いました。ついてけるかは別として、これで仕事できたら楽しそう。:thinking:
ここに書いてあることは感想の部分が多いので興味がある方はぜひ下記のリンクで学んでみてください!

2
1
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
2
1