今天繼續(xù)來(lái)學(xué)習(xí)numpy。
學(xué)習(xí)有關(guān)復(fù)數(shù)矩陣在numpy中的創(chuàng)建和使用。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : SundayCoder-俊勇
# @File : numpy3.py
import numpy as np
# numpy基本學(xué)習(xí)第三課
# 今天學(xué)習(xí)創(chuàng)建一個(gè)復(fù)數(shù)矩陣。
array=np.array([1+2j,2+3j])
print array
# [ 1.+2.j 2.+3.j]
# 打印出復(fù)數(shù)矩陣的實(shí)部數(shù)據(jù)。
print array.real
# [ 1. 2.]
# 打印出復(fù)數(shù)矩陣的虛部數(shù)據(jù)。
print array.imag
# [ 2. 3.]
# 輸出復(fù)數(shù)矩陣的數(shù)據(jù)類(lèi)型
print array.dtype
# complex128【復(fù)數(shù)類(lèi)型】
# 無(wú)論是實(shí)數(shù)矩陣還是復(fù)數(shù)矩陣,轉(zhuǎn)化成為列表的方式均一樣。
# 例如把a(bǔ)rray目前這個(gè)復(fù)數(shù)舉證轉(zhuǎn)換成為列表。
list1=array.tolist()
print list1
# 輸出結(jié)果:[(1+2j), (2+3j)]
print list1[0]
# 輸出結(jié)果:(1+2j)
# 轉(zhuǎn)換成為列表的過(guò)程中可以指定數(shù)據(jù)類(lèi)型。
# 這里我把復(fù)數(shù)矩陣轉(zhuǎn)換成為實(shí)數(shù)整形列表
list2=array.astype(int)
print list2
# 輸出結(jié)果:[1 2]
# 這里會(huì)出現(xiàn)一個(gè)警告:ComplexWarning: Casting complex values to real discards the imaginary part
# list2=array.astype(int)
# 也就是這樣的轉(zhuǎn)換不安全,丟失掉了復(fù)數(shù)的虛部。【一般實(shí)際中不推薦這樣使用】
運(yùn)行結(jié)果:
[ 1.+2.j 2.+3.j]
[ 1. 2.]
[ 2. 3.]
complex128
[(1+2j), (2+3j)]
(1+2j)
ComplexWarning: Casting complex values to real discards the imaginary part
list2=array.astype(int)
[1 2]