numpy數組(1)

引入numpy數組

import numpy as np

建立numpy數組函數

countries = np.array([
    'Afghanistan', 'Albania', 'Algeria', 'Angola', 'Argentina',
    'Armenia', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas',
    'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium',
    'Belize', 'Benin', 'Bhutan', 'Bolivia',
    'Bosnia and Herzegovina'
])

employment = np.array([
    55.70000076,  51.40000153,  50.5       ,  75.69999695,
    58.40000153,  40.09999847,  61.5       ,  57.09999847,
    60.90000153,  66.59999847,  60.40000153,  68.09999847,
    66.90000153,  53.40000153,  48.59999847,  56.79999924,
    71.59999847,  58.40000153,  70.40000153,  41.20000076
])

獲取數組中某項code

print countries[0]
print countries[3]

截取數組中的某一段orm

print countries[0:3]
print countries[:3]
print countries[17:]
print countries[:]

獲取numpy數組的數據類型索引

print countries.dtype
print employment.dtype
print np.array([0, 1, 2, 3]).dtype
print np.array([1.0, 1.5, 2.0, 2.5]).dtype
print np.array([True, False, True]).dtype
print np.array(['AL', 'AK', 'AZ', 'AR', 'CA']).dtype

循環numpy數組form

for country in countries:
    print 'Examining country {}'.format(country)

for i in range(len(countries)):
    country = countries[i]
    country_employment = employment[i]
    print 'Country {} has employment {}'.format(country,
            country_employment)

numpy的一些內置函數()import

print employment.mean() #取平均數
print employment.std()  #獲取標準差
print employment.max()  #取最大值
print employment.sum()  #求和

獲取最大項的索引值循環

i = employment.argmax();    #argmax()方法獲取employment數組中的最大一項的位置  
max_value = employment[i]  
max_country = countries[i]
相關文章
相關標籤/搜索