python

Python 入門 機械学習のためにnumpyを学習

numpyとは

pythonのモジュール
数値計算を高速にできる
ビックデータの計算処理に適している

モジュールのインストール

pip install numpy

list型のサンプルデータを準備

サンプルリストを用意

sample_list = [ [ 1, 2, 3], [ 4, 5, 6]]
print(type(sample_list))
print(sample_list)
実行結果
<class 'list'>
[ [ 1, 2, 3], [ 4, 5, 6]]

list型サンプルデータをnumpy.ndarray型に変換(array)

nd_sample_list = np.array(sample_list)
print(type(nd_sample_list))
print(nd_sample_list)
実行結果
<class 'numpy.ndarray'>
[ [ 1, 2, 3]
  [ 4, 5, 6]]

配列の構造を確認(shape)

print("配列の構造: ", nd_sample_list.shape)
実行結果

縦2列, 横3行のデータ構造

配列の構造:  (2, 3)

データ要素数(size)

print(nd_sample_list.size,"要素数")
実行結果

データの個数6個

6 要素数

配列の生成(arange)

20個の1次元配列を生成

twenty_lists = np.arange(20)
実行結果
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
<class 'numpy.ndarray'>
配列の構造:  (20,)
要素数:  20

配列の構造の変更(reshape)

配列の構造を4*5の構造に変更

re_twenty_lists = twenty_lists.reshape(4,5)
実行結果
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
<class 'numpy.ndarray'>
配列の構造:  (4, 5)
要素数:  20
ABOUT ME
Umatani
株式会社Playground代表取締役。 18歳から国内大手企業にてエンジニアのキャリアを開始。その後、外資企業に約10年間勤めたのち、フリーランスとして国内複数企業のアプリ開発を経験。2018年に株式会社Playgroundを設立。Swiftスクール事業、iOS受託開発、サーバー受託開発、アプリケーション開発コンサルティング等、幅広く活動中。
株式会社Playgroundのサービス
  • 無料・簡単・片手でホームページを作成できる自社サービス Rakwi
  • Web制作とアプリ開発を学べるオンラインプログラミング講座 Upstairs
  • 開発,DX推進支援サービス スタートアッププラン

COMMENT

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA