list、array、ndarray

Pythonの標準ライブラリーには配列がなく、listかtupleになる。
listは文字通りリストな上、なんでもいっしょくたに放り込める。
見た目はn次元配列に見えるようなこともできるが、実質は(1次元の)リストで、そのリストのオブジェクトとしてリストを入れ子にできる。

    thisIsList = [0, 1, 2, 'a', 'b', [0, 1]] # it can contain any, including the other list
    print(thisIsList)
    thisIsList.append('aho')
    print(thisIsList)

Pythonの特徴的な配列アクセス方法にsliceがある。
start:stop:stepを指定してリストの要素にアクセスできる、stepは省略可能。

    print(thisIsList[0:3])
    print(thisIsList[::3])
    print(thisIsList[5][1])


arrayライブラリーが配列を提供するが、こいつは一次元配列のみ。

    thisIsArray = array.array('f', [0.0, 1.0, 2.0, 3.0, 4.0]) # it can contain the same data type, and is one dimensional
    print(thisIsArray)


numpyに含まれるndarrayが強烈。
"nd"の名の通りn次元配列が作れる。

    thisIsNdarray = np.array([[0.0, 1.0, 2.0, 3.0], [4.0, 5.0, 6.0, 7.0], [8.0, 9.0, 10.0, 11.0]], dtype = float) #it can contain same data type, and it can be N dimensional
    print(thisIsNdarray)

素数さえ合えば変形も自由自在

    thisIsNdarray = thisIsNdarray.reshape(4,3) # change to 3 x 4 matrix
    print(thisIsNdarray)
    thisIsNdarray = thisIsNdarray.reshape(2,2,3) # change to 3 x 2 x 2 matrix
    print(thisIsNdarray)
    thisIsNdarray = thisIsNdarray.reshape(3,4) # change back to 4 x 3 matrix
    print(thisIsNdarray)

sliceも使える、そして該当する要素に一括して代入可能。

    print(thisIsNdarray[1, :]) # get index = 1 row of any column
    print(thisIsNdarray[:, 2]) # get index = 2 column of any row
    print(thisIsNdarray[:,0:3:2]) # get index 0 and 2 columns of any row
    thisIsNdarray[:,:] = 0.0 # set all to 0
    print(thisIsNdarray)
    thisIsNdarray[0:2,:] = 1.0 #set index 1 and 2 row of any column to 1.0 
    print(thisIsNdarray)
    thisIsNdarray[:,2] = 2.0 # set index 2 column of any row to 2.0 
    print(thisIsNdarray)

初期化はこんな感じで。

    thisIsNdarray = np.arange(0.0, 12.0, 1.0, dtype = float).reshape(3,4) # does same as the first initialization
    print(thisIsNdarray)
    thisIsNdarray = np.zeros((3, 4), dtype = float) # if no default value is necessary, 'empty' method can be used
    print(thisIsNdarray)

github.com

PythonとTuple

tupleとは順序組 - Wikipedia

Pythonでは関数の戻り値をtupleで返すことができる。

def doSomething(num1, num2):
   return (num1 + num2, num1 - num2, num1 * num2, num1 / num2)

としておいて

returnValue = doSomething(1, 2)

とすると

returnValue = (3, -1, 2, 0.5)

となる。
こいつを

(addValue, subValue, multValue, divValue) = doSomething(1, 2)

として各変数として一個一個受け取ることも可能。
さらにTupleの()は省略してもよいので、

def doSomething(num1, num2):
   return num1 + num2, num1 - num2, num1 * num2, num1 / num2
addValue, subValue, multValue, divValue = doSomething(1, 2)

と書いてよい。
C言語系ならポインタや配列で受け取らなくてはならないものが、がばんとぶちまけてもらえてしまう。
なお、最近はC言語系(含むC#)でもTupleは使えるので、なんかすごい違和感があるコードが見られる。

ノートPC歴その12 MacBook Pro (Mid 2020)

f:id:nobu_macsuzuki:20201220203643j:plain

  • Core i5 2.0GHz
  • RAM 16GB
  • SSD 512GB
  • 13.3" 2560x1500 DCI-P3(D65)
  • W304xD214xH15mm3
  • 1.4kg

なんと今年3台目のMacのお迎えだ。
1台目はかなり旧式となった22nmプロセスのHaswell Macの更新、つまり定期更新だった。
もともと所有していたこのMid 2013のMacBook Air最新のOSが問題なく動作し、なおかつWindows 10のBootcampが可能なモデルの中では一番古いのだ。
いろいろとサポートが切れてしまう前に買い替えが必要だった。
軽量のAirでなくProにしたのは老眼が進んだ生産性を重視してだ。
ノートPCは可搬性が重要だ。
11.6"のAir理想のノートPCなのだが、2015年以降は11.6"モデルの更新が無くなってしまった。
UMPCのような超軽量小型モデルが安価に手に入るようになったこともあり、メインのノートPCに理想を求めなくてもよくなってきた。
以前は2kg以上でウェイトトレーニング上等だったMacBook Proの軽量化が進んだこともあった。
結果は正解で、お座敷PCとしてはベストなモデルだった。
唯一の誤算はSSDのサイズ。
筆者はどうしてもMacWIndowsマシンとして使いたいので、BootcampをしてSSDを分割するのは必須だ。
昔は80GBもあればWindowsの作業領域は十分だったのだが、いまや256GBの1/2を当てるのでは全く不十分。
ということで、1台目と同型でSSDが512GBのモデルに入れ替えたのが2台目、今年の4月だった。


その後、来るべきして来たAppleの自社SoCのMacへの導入もあり、最新でより長く使えるモデルに替えた方がよいように思えてきた。
筆者の場合、Macとして、そしてメインのノートPCとして使うために、Windowsのbootcampは必須だ。
ARMベースのAppleのSoCでWindowsのbootcampができる日もいつか来るだろうが、しばらくはいろいろと不具合もあろう。
それとM1ベースのMacBook (AirとPro)が発表になってから、今年のインテルCPUモデルが投げ売りに近い価格でebayに出回りだしたのも大きかった。
おそらく「Appleの新しいSoCなんて不具合が多いだろう」と思ってインテルCPUモデルを購入した人が、M1の高評価を見て慌てて乗り換えたのだろう。
ということで、この年の押し迫ったところで、最後のインテルCPU搭載モデルなるかもしれない、10nmプロセスのIce Lake CPUモデルに買い替えたのが3台目だ。


この3台目、第六世代と呼ばれるものなのだが、2017年モデルなどの第五世代とシャーシは一緒だがキーボードが更新されている。
2017年モデルはキーボードを叩くと底着間が強く、テーブルを叩いているような感じだった。
この2020年モデルはキーボードを叩いている、という感じに戻った。


ちなみに今年購入した3台、全て中古品でebayで入手したものだ。
状態はどれも極めてよく(もちろんとても注意深く選定した)、新品同様だった。
1台買っては前のを売ってを繰り返すわらしべ長者方式で、更新費用もそれほど掛からず、2台目が予定している値段で売れてくれれば、9万円余りでこのMacが購入できたことになる。

Phython matplotlib 3Dグラフ

ついでにCSVファイルの読み込み、散布図で書いた、後悔はしていない。

csvファイル (exampledata.csv)

x,y,z
0,0,0
1,0,1
.866,.5,1
.5,.866,1
0,1,1
-.5,.866,1
-.866,.5,1
-1,0,1
-.866,-.5,1
-.5,-.866,1
0,-1,1
0.5,-.866,1
.866,-.5,1
2,0,4
1.73,1,4
1,1.73,4
0,2,4
-1,1.73,4
-1.73,1,4
-2,0,4
-1.73,-1,4
-1,-1.73,4
0,-2,4
1,-1.73,4
1.73,-1,4

に対して

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
# for surface plot
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

def _3dplotLine():
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    t = np.linspace(0, 10, 101)
    x = t
    y = t
    z = -t
    ax.plot(x, y, z)
    plt.show()
def _3dplotScatter():
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    csvFile = np.loadtxt('exampledata.csv', delimiter=',', skiprows=1)
    NumberOfRow = csvFile.shape[0]
    rowRange = range (NumberOfRow)
    for indexRow in rowRange:
        [xs,ys,zs] = csvFile[indexRow]
        print([xs,ys,zs])
        ax.scatter(xs, ys, zs, c = 'y')
    plt.show()

def _3dplotSurface():
    fig = plt.figure()
    ax = fig.gca(projection='3d')

    X = np.arange(0, 2, 1)
    Y = np.arange(0, 2, 1)
    X, Y = np.meshgrid(X, Y)
    Z = -X -Y
    print(X)
    print(Y)
    print(Z)

    ax.plot_surface(X, Y, Z)
    plt.show()

def main():
    _3dplotLine()
    _3dplotScatter()
    _3dplotSurface()

if __name__ == "__main__":
    main()

f:id:nobu_macsuzuki:20201214095619p:plain
f:id:nobu_macsuzuki:20201214095631p:plain
f:id:nobu_macsuzuki:20201214095653p:plain
github.com

ガジェットリスト更新

  • Nokia 5.3(現地回線)
  • iPhoneSE (2nd Gen, 日本回線)
  • EOS Rp
  • Cube PC(Core i7 7700)
  • MacBook Pro 13" 6-2(Mid 2017 A1708, Core i5 7360U)
  • Chuwi Hi10 XR (Geminilake Reflesh) - FX選定評価中
  • Magic Ben MAG1 (Core m 8100Y)
  • Samsung Galaxy Tab S5e 64GB
  • Garmin Forerunner 235
  • Fossil Gen 5 Carlyle
  • Bose Quiet Comfort 35

耐え難きを耐え 忍び難きを忍び・・・メインの電話を普通のAndroidに買い替え、To Be Continued...
iPhone 6sは6600曲のno music no lifeライブラリーが64GBのストレージを逼迫したため、いま買うことができる容量128GBの一番安いiPhoneに買い替え。
iPhone 6sはもちろん下取りに、怪物君Unihertz Titanはebayで売れられていきましたとさ。