• Skip to main content
  • Skip to primary sidebar

学習記録

プログラミング

ファンシーインデックス

2017年11月24日 by 河副 太智 Leave a Comment

1
2
3
4
5
6
7
8
9
10
11
12
13
<span class="cm-variable">arr</span> = <span class="cm-variable">np</span>.<span class="cm-property">array</span>([[<span class="cm-number">1</span>, <span class="cm-number">2</span>], [<span class="cm-number">3</span>, <span class="cm-number">4</span>], [<span class="cm-number">5</span>, <span class="cm-number">6</span>], [<span class="cm-number">7</span>, <span class="cm-number">8</span>]])
 
の二次元配列は以下のようになる
[[<span class="cm-number">1</span> <span class="cm-number">2</span>]
[<span class="cm-number">3</span> <span class="cm-number">4</span>]
[<span class="cm-number">5</span> <span class="cm-number">6</span>]
[<span class="cm-number">7</span> <span class="cm-number">8</span>]]</code><code class="cm-s-ipython language-python">
</code><code class="cm-s-ipython language-python">
 
以下のように指定すると
<span class="cm-builtin">print</span>(<span class="cm-variable">arr</span>[[<span class="cm-number">3</span>, <span class="cm-number">2</span>, <span class="cm-number">0</span>]])
 
このように出力

1
2
3
4
 
[[<span class="cm-number">7</span> <span class="cm-number">8</span>]
[<span class="cm-number">5</span> <span class="cm-number">6</span>]
[<span class="cm-number">1</span> <span class="cm-number">2</span>]]

Filed Under: Numpy

axis=1 は行で横方向処理 axis=0は列で縦方向処理 ベクトルは計算後の数値を出力していく順番

2017年11月24日 by 河副 太智 Leave a Comment

「numpy axis slice」の画像検索結果

 

 

 

 

axis=1 は横で行 axis=0は縦で列
ベクトルは計算後の数値を出力していく順番

## 2×4の行列を作成
A = np.arange(8).reshape(2,4)
# >>> A
# array([[0, 1, 2, 3],
# [4, 5, 6, 7]])
## 列方向の和をとり,行ベクトルを出力
row_sum = np.sum(A, axis=0)
# >>> row_sum
# array([ 4, 6, 8, 10])
## 行方向の和をとり,列ベクトルを出力
col_sum = np.sum(A, axis=1)
# >>> col_sum
# array([ 6, 22])

Filed Under: Numpy

二次元配列の縦横参照、スライス、

2017年11月24日 by 河副 太智 Leave a Comment

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr)

[[1 2 3]
[4 5 6]
[7 8 9]]

# 変数arrの要素のうち3を出力してください
print (arr[0,2])

3

 

———————————————————–

#二次元配列が以下のようになっている場合

[[1 2 3]
[4 5 6]
[7 8 9]]

#以下のように指定すると
print (arr[1:,:2])

1
2
3
4
5
:に向かって上から数値の分だけ下に進んで踏んだ数を含めた
[[4 5]
[7 8]]
 
#上記のようになる

これは[2:,:2]を指定したという事は
2:が上から下に向かい2,3列目だけを指定する事と
:2が左から右に向かい2,1行目を指定しているから:1であれば左から右に向かい1行目
(ゼロはない)

 

print (arr[1:,1:])

の場合は

1
2
3
4
[[5 6]
[8 9]]
 
となる

 

Filed Under: Numpy

スカラー値

2017年11月24日 by 河副 太智 Leave a Comment

「スカラーとは、配列とかと異なり、

より小さな部分に’分割する’ことが できない値のこと

Filed Under: Numpy

二次元配列

2017年11月24日 by 河副 太智 Leave a Comment

np.array([リスト, リスト])と表記することで二次元配列を作成

import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

1
2
[[1 2 3 4]
[5 6 7 8]]

# 変数arrの行列の各次元ごとの要素数を出力

print(arr.shape)

1
2
(2, 4)
 

# 変数arrを4行2列の行列に変換してください

print(arr.reshape(4,2))

1
2
3
4
[[1 2]
[3 4]
[5 6]
[7 8]]

Filed Under: Numpy

一次元配列

2017年11月24日 by 河副 太智 Leave a Comment

import numpy as np
a = np.arange(15)
print(a)

numbers = [24, 3, 4 ,23 ,10, 12]

# ndarray変数を生成し、変数np_numbersに代入してください
np_numbers = np.array(numbers)
print(np_numbers)

カンマのないリスト配列になる

1
2
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
[24  3  4 23 10 12]

Filed Under: Numpy

  • « Go to Previous Page
  • Page 1
  • Interim pages omitted …
  • Page 41
  • Page 42
  • Page 43
  • Page 44
  • Page 45
  • Interim pages omitted …
  • Page 55
  • Go to Next Page »

Primary Sidebar

カテゴリー

  • AWS
  • Bootstrap
  • Dash
  • Django
  • flask
  • GIT(sourcetree)
  • Plotly/Dash
  • VPS
  • その他tool
  • ブログ
  • プログラミング
    • Bokeh
    • css
    • HoloViews
    • Jupyter
    • Numpy
    • Pandas
    • PosgreSQL
    • Python 基本
    • python3
      • webアプリ
    • python3解説
    • scikit-learn
    • scipy
    • vps
    • Wordpress
    • グラフ
    • コマンド
    • スクレイピング
    • チートシート
    • データクレンジング
    • ブロックチェーン
    • 作成実績
    • 時系列分析
    • 機械学習
      • 分析手法
      • 教師有り
    • 異常値検知
    • 自然言語処理
  • 一太郎
  • 数学
    • sympy
      • 対数関数(log)
      • 累乗根(n乗根)
    • 暗号学

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in