2017年5月10日 星期三

python dictionary items 未依序排列問題

import collections

s = {}
for i in range(0,36,2):
    s.update({i:str(i)*10})

sorted_s = collections.OrderedDict(sorted(s.items()))
print(s)
print('-------')
print(sorted_s)
print('=======')
for i, j in sorted_s.items():
    print('%s:%s' % (i,j))
將 dictionary s 重新排序後, 輸出次序就能正常囉~
{0: '0000000000', 32: '32323232323232323232', 2: '2222222222', 4: '4444444444', 6: '6666666666', 8: '8888888888', 10: '10101010101010101010', 12: '12121212121212121212', 34: '34343434343434343434', 14: '14141414141414141414', 16: '16161616161616161616', 18: '18181818181818181818', 20: '20202020202020202020', 22: '22222222222222222222', 24: '24242424242424242424', 26: '26262626262626262626', 28: '28282828282828282828', 30: '30303030303030303030'}
-------
OrderedDict([(0, '0000000000'), (2, '2222222222'), (4, '4444444444'), (6, '6666666666'), (8, '8888888888'), (10, '10101010101010101010'), (12, '12121212121212121212'), (14, '14141414141414141414'), (16, '16161616161616161616'), (18, '18181818181818181818'), (20, '20202020202020202020'), (22, '22222222222222222222'), (24, '24242424242424242424'), (26, '26262626262626262626'), (28, '28282828282828282828'), (30, '30303030303030303030'), (32, '32323232323232323232'), (34, '34343434343434343434')])
=======
0:0000000000
2:2222222222
4:4444444444
6:6666666666
8:8888888888
10:10101010101010101010
12:12121212121212121212
14:14141414141414141414
16:16161616161616161616
18:18181818181818181818
20:20202020202020202020
22:22222222222222222222
24:24242424242424242424
26:26262626262626262626
28:28282828282828282828
30:30303030303030303030
32:32323232323232323232
34:34343434343434343434

2017年4月27日 星期四

2017年4月26日 星期三

Windows 10 + NVIDIA GPU + TensorFlow


  1. 參考 Install TensorFlow with GPU for Windows 10
  2. 強烈建議:
    • 確實檢查自己的NVIDIA顯示卡是否支援?官網
    • Windows設定 %CUDA_HOME% 和添加CUDA相關目錄至%PATH%環境變數
    • Anaconda 同時安裝 Python2 和 Python3 兩版本:下載連結
    • 建立 Conda tensorflow-gpu 虛擬環境時,記得 Python3版本為 3.5
      • conda create -n tensorflow-gpu python=3.5 (暫時別選3.6)
    • activate tensorflow-gpu 後,安裝 numbapro 套件
    • 開啟 jupyter notebook 後,執行下列 python3 code:參考網站
    • from numbapro import cuda, vectorize, guvectorize, check_cuda
      from numbapro import void, uint8 , uint32, uint64, int32, int64, float32, float64, f8
      import numpy as np
      check_cuda()
      
    • 確認執行結果是否類似下列結果,最後出現的是 True !:
    • ------------------------------libraries detection-------------------------------
      Finding cublas
       located at d:\Anaconda3\envs\tensorflow-gpu\DLLs\cublas64_70.dll
       trying to open library... ok
      Finding cusparse
       located at d:\Anaconda3\envs\tensorflow-gpu\DLLs\cusparse64_70.dll
       trying to open library... ok
      Finding cufft
       located at d:\Anaconda3\envs\tensorflow-gpu\DLLs\cufft64_70.dll
       trying to open library... ok
      Finding curand
       located at d:\Anaconda3\envs\tensorflow-gpu\DLLs\curand64_70.dll
       trying to open library... ok
      Finding nvvm
       located at d:\Anaconda3\envs\tensorflow-gpu\DLLs\nvvm64_30_0.dll
       trying to open library... ok
       finding libdevice for compute_20... ok
       finding libdevice for compute_30... ok
       finding libdevice for compute_35... ok
      -------------------------------hardware detection-------------------------------
      Found 2 CUDA devices
      id 0    b'GeForce GTX 1060 6GB'                              [SUPPORTED]
                            compute capability: 6.1
                                 pci device id: 0
                                    pci bus id: 2
      id 1      b'GeForce GTX 650'                              [SUPPORTED]
                            compute capability: 3.0
                                 pci device id: 0
                                    pci bus id: 1
      Summary:
       2/2 devices are supported
      PASSED
      Out[1]:
      True
      
  3. 兩張顯示卡的GPU數量不同,另外在Windows環境設定TF_MIN_GPU_MULTIPROCESSOR_COUNT=2 錯誤訊息才消失
希望大家能少走點冤枉路...:P #

2017年3月13日 星期一

pythone upgrade all installed packages 更新所有已安裝的packages

pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

pip3 freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip3 install -U
另外一種方式也是可以的...
pip install -U $(pip3 freeze |awk '{split($0,a,"=="); print a[1]}')

pip3 install -U $(pip3 freeze |awk '{split($0,a,"=="); print a[1]}')
#

2017年3月9日 星期四

Windows 7 - Cygwin / Cygwin64 設定 pip proxy


測試 --proxy 後有無"="皆可
pip install --proxy proxyuser:proxypassword@proxy.server.domain <python_package_name>

pip install --proxy=proxyuser:proxypassword@proxy.server.domain <python_package_name>

據說 pip3 "=" 無用
pip3 install --proxy proxyuser:proxypassword@proxy.server.domain <python_package_name>


#

2017年2月21日 星期二

R 以地址取得經緯座標值

假如data.frame data中的lat值為0時,則利用google maps api geocode 查詢地址的經緯座標,並存回 data$lat 與 data$lng 中:

library(ggmap)

for (i in 1:nrow(data)) {
     if (data$lat[i] == 0) {
         addr <- data$service_addr[i]
         url = paste('http://maps.google.com/maps/api/geocode/xml?address=', addr,'&sensor=false',sep='')
         doc = xmlTreeParse(url) 
         root = xmlRoot(doc) 
         data$lat[i] = xmlValue(root[['result']][['geometry']][['location']][['lat']]) 
         data$lng[i] = xmlValue(root[['result']][['geometry']][['location']][['lng']])
     }
}

# 將 data 內容寫入檔案 mymap.csv
write.table(data, file = "mymap.csv", sep = "," )

#

2017年2月20日 星期一

取得政府資料開放平台資料資源,將資料分類統計以R語言繪製成圓餅圖

library(data.table)

# 取得政府資料開放平台資料資源 (file format = csv)
data <- fread("http://search.data.gov.tw/wise/query?q=%2A%3A%2A&export=true&format=csv&rows=2147483647&d=1", header="auto", encoding = "UTF-8")

# 統計資料分類及分類數量, 統計結果以 data.frame 儲存至 categories
categories <- as.data.frame(table(data$服務分類))

# 計算每個分類百分比,取小數點第2位,將結果存於 pct
pct <- round(categories$Freq/sum(categories$Freq)*100,2)

# 把原來的分類"名稱 百分比 %" 存回原來的名類名稱中 
categories$Var1 <- paste(categories$Var1, pct, "%", sep=" ")

#繪製圓餅圖 Piechart
pie(categories$Freq, labels=categories$Var1, main="政府資料開放平台資料資源", col=rainbow(length(categories$Var1)))


執行結果: