Skip to content

Instantly share code, notes, and snippets.

View lawrencechen0921's full-sized avatar
🏠
Working from home

Lawrence Chen lawrencechen0921

🏠
Working from home
View GitHub Profile
public class QuickFindUF
{
private int [ ] id;
public QuickFindUF( int N)
{
id = new int[N]; # set id of each object to itself { N array accesses}
for ( int i = 0 ; i < N ; i ++)
id[ i ] = i ;
}
@lawrencechen0921
lawrencechen0921 / tf.keras.model.py
Last active September 10, 2019 11:38
建立keras model
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
@lawrencechen0921
lawrencechen0921 / tensorflow dataflow.py
Created September 10, 2019 10:36
tensorflow的資料流t 新手教學
import tensorflow as tf
a = tf.constant(1, name='a')
b = tf.constant(1, name='b')
c = a+b
with tf.Session as sess: #注意以上的session將a,b 視為個體, 否則跑出來的結果會為不同
print(sess.run(c))
import tensorflow as tf
a = tf.constant(1, name='a')
@lawrencechen0921
lawrencechen0921 / BS4HTML.py
Created August 30, 2019 11:46
用BeautifulSoup 分析HTML 標籤、屬性和值
from bs4 import BeautifulSoup as soup
fin=open('web.htm', encoding='utf-8') #將html檔案放置 web.htm(再轉換到fin開啟)
s=fin.read()
htm=soup(s,'html.parser')
print(htm.title.prettify())
print(htm.title.contents)
print(htm.contents[0])
print(htm.title.name)
print(htm.title.string)
print(htm.meta)
@lawrencechen0921
lawrencechen0921 / twstock.py
Last active August 24, 2019 14:26
預測股票分析以及建圖---可自選股票型號並用前五天趨勢分析未來(FinMind技術)
from FinMind.Data import Load
import numpy as np
#import pandas as pd
from keras.layers.core import Dense, Dropout
from keras.layers import LSTM , GRU
from keras.models import Sequential
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import math
from keras.optimizers import Adam
@lawrencechen0921
lawrencechen0921 / crawl data form website.py
Created August 13, 2019 05:42
使用urllib.request 爬蟲
#這邊會介紹把網頁後端的程式碼抓下來的結果以及會使用到的指令
import urllib.request as ur #先把模組簡單匯入
url="https://看你需要甚麼網站的名稱" #輸入自己要的網站
resp=ur.urlopen(url) #暫時存在ur裏頭
data=resp.read() #data會在暫存器里先讀取
print(data)
#以下毒出來的資料皆為byte,你也可以用decode函數轉換成字串
#但是urllib.request的程式碼較為複雜 比較建議使用request的第三方模組,得出的結果也會較為簡潔
@lawrencechen0921
lawrencechen0921 / data.pyplot.py
Created August 11, 2019 07:24
教大家如何用matplotlib第三方模組將數值轉換成圖片
#首先你必須用 pip install matplotlib
#等他下載完成,你就可以使用模組去進行數據視覺化的動作
import matplotlib.pyplot as plt
data=[5,10,15,25,20] #把你需要的data數值用出來
plt.barh(range(len(data)),data) #圖片會顯示你data長度和範圍
plt.show()
#是不是很簡單呀
@lawrencechen0921
lawrencechen0921 / Python Counter.py
Created August 9, 2019 13:36
使用Counter模組找出最常出現的五個字以及次數
#再windos按鈕畫面點擊右鍵(並按下執行)
#先在cmd 命令提示字元 輸入python再輸入import this
#將那段話存入 xxx.txt
from collections import Counter #記住從from 甚麼 import 甚麼都要放在第一行 以免篩選器錯誤
fin=open('xxx.txt','rt')
s=fin.read().lower() #物件fin 使用函式read一次讀取所有內容, 使用函式lower將所有字母轉換小寫
words=re.findall(r'[\w\']+',s) #用re.findall找出大小寫字母、數字、底線、和單引號
c=Counter(words) #將物件counter以words為輸入, 將獲得的counter物件轉換成c
print(c.most_common(5)) #把c內容最常出現的五個屬性字找出
@lawrencechen0921
lawrencechen0921 / 李白將進酒(你也可以用其他的)正規式搜尋.py
Created August 9, 2019 12:41
Regular Expression 正規表示式- 常用於字串的分析與擷取歐, 不過要先匯入模組re 假設一篇文章很長, 你想知道他出現過幾次也可以用這個方法也可以使用下一個Couter模組計算
import re #匯入模組 #這個符號是python拿來解釋用的歐
s='君不見黃河之水天上來,奔流到海不復回。\
君不見高堂明鏡悲白髮,朝如青絲暮成雪。
人生得意須盡歡,莫使金樽空對月。\
天生我材必有用,千金散盡還復來。\
烹羊宰牛且爲樂,會須一飲三百杯。\
岑夫子,丹丘生。將進酒,杯莫停。\
與君歌一曲,請君爲我側耳聽。\
鐘鼓饌玉不足貴,但願長醉不願醒。\
古來聖賢皆寂寞,惟有飲者留其名。\
@lawrencechen0921
lawrencechen0921 / unicode encode&decode.py
Created August 9, 2019 12:10
使用unicode編碼與解碼
print('我是小帥哥'.encode('unicode-escape')) #將【我是一個小帥哥】以函式encode以 (unicode-escape)進行編碼
def utf8(data): #定義utf-8
data_byte=data.encode('utf-8') #回傳data的utf-8編碼結果指定給data_byte
data2=data_byte.decode('utf-8')
print('將',data,'經由utf-8編碼後為',data_byte)
print('將',data_byte,'經由utf-8解碼後為',data2)
print(data,'的長度為',len(data)) #將data長度顯示於螢幕上
print(data_byte,'的長度為',len(data_byte)) #將data_byte長度顯示於螢幕上
utf('我是一個小帥哥')
utf("b'\\u6211\\u662f\\u5c0f\\u5e25\\u54e5'")