シラバス レッスン 250 中 77 · データの基礎:numpy と pandas
データの基礎:numpy と pandas

配列の作成とインデックス

配列を作る方法はいくつかあります。いちばんよく使うもの:

import numpy as np
a = np.array([1, 2, 3])      # from a list
b = np.arange(0, 10, 2)      # like range: [0 2 4 6 8]
z = np.zeros(4)              # [0. 0. 0. 0.]
o = np.ones((2, 3))          # a 2x3 grid of ones

どの配列にも dtype(要素の型)と shape(各次元に沿った大きさ)があります:

a = np.array([1, 2, 3])
print(a.dtype)   # int32 or int64 depending on the platform (int32 in this browser)
print(a.shape)   # (3,)   one dimension, length 3

grid = np.array([[1, 2, 3], [4, 5, 6]])
print(grid.shape)  # (2, 3)   2 rows, 3 columns

インデックスとスライスはリストと同じように働き、0 から始まります:

a = np.array([10, 20, 30, 40, 50])
print(a[0])     # 10
print(a[-1])    # 50
print(a[1:4])   # [20 30 40]   start included, stop excluded

2 次元の配列では grid[row, col] でインデックスを指定します:

print(grid[0, 2])   # 3  (row 0, column 2)
print(grid[1])      # [4 5 6]  (the whole second row)
あなたの番

numpy を使って steps = np.arange(0, 10, 2)(0..8 の偶数)を作ってください。その shape を shp に保存します。それから真ん中の 3 つの値(インデックス 1、2、3)をスライスして mid に入れてください。

このレッスンに問題を見つけましたか?報告する · このレッスンについて質問する

この演習はまもなく開きます

その前のレッスンを終えると、このレッスンが解放されます。コース全体が無料でアカウントも不要、このレッスンは今すぐ読めます。