2018年12月31日 星期一

深度學習開始-在Pycharm中安裝函式庫

深度學習開始-在Pycharm中安裝函式庫

在開始深度學習之前,先將會用到的函示庫裝好,
照以下步驟:




















輸入需要新增的函示庫名稱,即可自動搜尋並且直接安裝。






預計之後會用到函式庫清單,可以一併先安裝起來:
1. numpy
2. BLAS
3. openBLAS
4. matplotlib
5. SciPy
6. Pydot-ng
7. tensorflow
8. theano
9. Keras




2018年12月30日 星期日

深度學習開始-建置環境-使用Pycharm

深度學習 - 建置環境

我選擇使用一開始練習的python的IDE,Pycharm
作業環境 - MacBook Pro macOS Mojave 10.14.2

1. 安裝Pycharm
至 https://www.jetbrains.com/pycharm/download/#section=mac 下載免費版











2. 安裝Anaconda
至 https://www.anaconda.com/download/#macos 選apple











選擇使用python 3 以上版本












都安裝好後,開啟Pycharm







這邊選擇使用Conda的環境,記得要選 python 3.6版,thensorflow還未支援3.7版本,若使用3.7版會無法執行。

 最後按確認後即可開始寫程式了。

 終於安裝好了...接下來還要install 我們會用到的 import 函式庫。




2018年12月27日 星期四

#練習資料夾檔案讀取&輸出

#練習資料夾檔案讀取&輸出


#例外處理練習

#例外處理練習


#流程控制-成績單系統

#流程控制-成績單系統




#判斷質數練習


#判斷質數練習


#單字出現頻率統計程式

#單字出現頻率統計程式

從網路上抓了一篇文章,要統計這篇文章出現哪些單字,並建立一個dict() 做key與value的應用。最後統計單字出現的頻率,並將出現超過1次的字print出來。
以下是網路隨意抓的文章:

The Izumo and Kaga have been carrying helicopters designed for anti-submarine warfare since entering service over the past three years. They will need to have their decks reinforced to accommodate the heavier F-35Bs, as well as the heat and force from the jets' thrusters when they land vertically.
Japan will also increase its order for F-35A jets, which take off and land on conventional runways, to 105, the government said. Forty-two of those jets are in service or were part of earlier Japanese orders. Those planes will replace the Japan Air Self-Defense Force's aging F-15J fighters.
The purchases will be spread over 10 years, with 27 of the F-35As and 18 of the F-35Bs to be acquired, as well as the two warships to be refitted, in the first five years.
Total spending over the first five years is pegged at $282.4 billion and will include creating cyber defense and naval transportation units that operate across Japan's three military branches, the Ground, Air and Maritime Self-Defense Forces.


先將文字存成一個純文字檔案sample.txt 路徑:
"/Users/lido/Library/Preferences/PyCharmCE2018.3/scratches/sample.txt"
這邊會用到python 的正則表達式函數 (可參考RUNOOB正則表達式教程)
re.match(pattern, string, flags=0),正則表達式可以查找文檔内或输入域内特定的文本。
import re

將文章讀取出來,存到article 內
fp = open("/Users/lido/Library/Preferences/PyCharmCE2018.3/scratches/sample.txt","r")
article = fp.read()
>>>type(article)
<class 'str'>

建立一個新的字串符new_article,利用正則匹配替換文章後存到裡面。
new_article = re.sub("[^a-zA-Z\s]","",article)

re.sub 檢索和替換
Python 的re模組提供了re.sub用於替換字符串中的匹配項。
re.sub(pattern, repl, string, count=0)
pattern : 正則中的模式字串。
repl : 替換的文字符串,也可為一個函數。
string : 要被查找替換的原始字符串。
count : 模式匹配後替換的最大次數,默認 0 表示替換所有的匹配。

^匹配字符的開頭,a-zA-Z匹配所有的字母,\s匹配任何空白字符,包括空格、制表符、換頁符等等,等價於 [ \f\n\r\t\v]。

正則匹配後,印出來看看:
>>>new_article
'The Izumo and Kaga have been carrying helicopters designed for antisubmarine warfare since entering service over the past three years They will need to have their decks reinforced to accommodate the heavier FBs as well as the heat and force from the jets thrusters when they land vertically Japan will also increase its order for FA jets which take off and land on conventional runways to  the government said Fortytwo of those jets are in service or were part of earlier Japanese orders Those planes will replace the Japan Air SelfDefense Forces aging FJ fighters The purchases will be spread over  years with  of the FAs and  of the FBs to be acquired as well as the two warships to be refitted in the first five years Total spending over the first five years is pegged at  billion and will include creating cyber defense and naval transportation units that operate across Japans three military branches the Ground Air and Maritime SelfDefense Forces '

接著利用split() 函數,將文字都存進word_list()中
words = new_article.split()

印出來看看:
>>>words
['The', 'Izumo', 'and', 'Kaga', 'have', 'been', 'carrying', 'helicopters', 'designed', 'for', 'antisubmarine', 'warfare', 'since', 'entering', 'service', 'over', 'the', 'past', 'three', 'years', 'They', 'will', 'need', 'to', 'have', 'their', 'decks', 'reinforced', 'to', 'accommodate', 'the', 'heavier', 'FBs', 'as', 'well', 'as', 'the', 'heat', 'and', 'force', 'from', 'the', 'jets', 'thrusters', 'when', 'they', 'land', 'vertically', 'Japan', 'will', 'also', 'increase', 'its', 'order', 'for', 'FA', 'jets', 'which', 'take', 'off', 'and', 'land', 'on', 'conventional', 'runways', 'to', 'the', 'government', 'said', 'Fortytwo', 'of', 'those', 'jets', 'are', 'in', 'service', 'or', 'were', 'part', 'of', 'earlier', 'Japanese', 'orders', 'Those', 'planes', 'will', 'replace', 'the', 'Japan', 'Air', 'SelfDefense', 'Forces', 'aging', 'FJ', 'fighters', 'The', 'purchases', 'will', 'be', 'spread', 'over', 'years', 'with', 'of', 'the', 'FAs', 'and', 'of', 'the', 'FBs', 'to', 'be', 'acquired', 'as', 'well', 'as', 'the', 'two', 'warships', 'to', 'be', 'refitted', 'in', 'the', 'first', 'five', 'years', 'Total', 'spending', 'over', 'the', 'first', 'five', 'years', 'is', 'pegged', 'at', 'billion', 'and', 'will', 'include', 'creating', 'cyber', 'defense', 'and', 'naval', 'transportation', 'units', 'that', 'operate', 'across', 'Japans', 'three', 'military', 'branches', 'the', 'Ground', 'Air', 'and', 'Maritime', 'SelfDefense', 'Forces']

很好,工作完成差不多,剩最後統計的for迴圈了。
利用.upper() 函數,將文字全部轉成大寫,好方便統計。
如果字符沒有在word_counts_dict()中,則index value=1,如果文符有在裡面(即重複了,則該字符的index value+1)。
最後print出index value>1的字符
for word in words:
    if word.upper() in word_counts:
        word_counts[word.upper()] = word_counts[word.upper()] +1    
    else:
        word_counts[word.upper()] = 1
key_list = list(word_counts.keys())
key_list.sort()

for key in key_list:
    if word_counts[key] > 1:
        print("{}:{}".format(key,word_counts[key]),end=" ")

完整程式碼:
import re
fp = open("/Users/lido/Library/Preferences/PyCharmCE2018.3/scratches/sample.txt","r")
article = fp.read()
new_article = re.sub("[^a-zA-Z\s]","",article)
words = new_article.split()
word_counts={}
for word in words:
    if word.upper() in word_counts:
        word_counts[word.upper()] = word_counts[word.upper()] +1    
    else:
        word_counts[word.upper()] = 1
key_list = list(word_counts.keys())
key_list.sort()

for key in key_list:
    if word_counts[key] > 1:
        print("{}:{}".format(key,word_counts[key]),end=" ")
AIR:2 AND:7 AS:4 BE:3 FBS:2 FIRST:2 FIVE:2 FOR:2 FORCES:2 HAVE:2 IN:2 JAPAN:2 JETS:3 LAND:2 OF:4 OVER:3 SELFDEFENSE:2 SERVICE:2 THE:14 THEY:2 THOSE:2 THREE:2 TO:5 WELL:2 WILL:5 YEARS:4 

#map & lambda 函數練習

#map & lambda 函數練習

#自訂函數

#自訂函數

#python中-變數的記憶體位置

#python中-變數的記憶體位置

#檢視兩個變數是否為同一個記憶體位置 (重要!!)截錄書“從初學到活用Python 開發技巧的16堂課”6-3-5
在設計python時,有時候會有一些變數資料以互為指定的的方式來設定某些變數的初值,如果設定的時候兩者所指到的是同一個記憶體位置,不留意一下此特性可能會在程式中不小心改到了不想要改的內容。
因此,暸解如何檢視兩個變數是否使用一個記憶體有其必要性。
>>>a ={1,2,3}
>>>b=a
>>>b.add(4)
>>>a
{1, 2, 3, 4}
>>>a is b
True

*為了避免此情形,就要使用.copy() 方法
>>> a= {1,2,3}
>>> b=a.copy()
>>> b.add(4)
>>> b
{1, 2, 3, 4}
>>> a

{1, 2, 3}

#自我練習 : 樂透中獎機率程式


#自我練習 : 樂透中獎機率程式

# 猜數字遊戲

# 猜數字遊戲

#numpy & matplotlib 練習


#numpy & matplotlib 練習

numpy & matplotlib 是很強大的數學工具,numpy在計算數學陣列和矩陣效率可以和編譯過的C語言比擬。
先簡單練習numpy的基礎應用吧。

利用numpy畫個sin,cos,sin+cos函數。














輸出:

#list 練習

#list 練習

將a list 內的數字平方後重新寫進a內。










輸出結果:










參考 runoob : list, dict, set

#if else 條件控制 練習


#if else 條件控制 練習










if a==1:
  print('你的手機適合打電動')
elif a==2:
  print('你的手機適合筆記')
elif a==3:
  print('你的手機適合多工')

if & else 後面都要加冒號
最後一行也可以改成
else :
    print('你的手機適合多工')

2018年12月26日 星期三

#99乘法表練習

#99乘法表練習


剛學習for迴圈時,第一個想到的就是99乘法表了,先來看陽春版的99乘法表。

輸出:
後來學到了format,試著用看看吧~

輸出:


# for [變數名稱] in range(n):
(縮排) print([變數名稱])

#以下是用 %s 來對齊的功能
for i in range (1,10):             
   for j in range (1,10):
      a = i*j
      print('%2s' % a ,end=' ')  
   print(end='\n')

'%s' 字符串格式
%10s——右對齊佔位符10位
%-10s——左對齊,佔位符10位
%.2s——截取2位字符串
%10.2s——10位佔位符,截取兩位字符串
ex. print ( '%s' % 'hello world' )其中,中間的%對後面’hello world‘ 定義要怎麼對齊。也可以對多個字串對齊ex. print('%2s+%2s=%2s' % (a,b,c))


參考 runoob : for , format , range