網頁

2017年1月15日 星期日

物件

物件導向1
一個物件(Object)可以包含屬性(Attribute)與方法(Method)

import pandas as pd
name = ["蒙其·D·魯夫", "羅羅亞·索隆", "娜美", "騙人布", "賓什莫克·香吉士", "多尼多尼·喬巴", "妮可·羅賓", "佛朗基", "布魯克"] 
age = [19, 21, 20, 19, 21, 17, 30, 36, 90] 

# 建立 dict straw_hat_dict = {"name": name, "age": age } 

# 建立 data frame 
straw_hat_df = pd.DataFrame(straw_hat_dict) 
print(straw_hat_df.dtypes) # straw_hat_df 有 dtypes 屬性 
print(straw_hat.df.head()) # straw_hat_df 有 head 方法


物件導向2
一個物件(Object)屬於一種類別(Class)
定義類別

class Strawhat:
  ''' 
  這是一個叫做 Strawhat 的類別 
  ''' 
  def __init__(self, name, age): 
    self.name = name 
    self.age = age 

# 根據 Strawhat 類別建立兩個物件 
luffy = Strawhat("蒙其·D·魯夫", 19) 
zoro = Strawhat("羅羅亞·索隆", 21) 

# 使用屬性 
print(luffy.name) 
print(luffy.age) 
print(luffy.__doc__) 
print(zoro.name) 
print(zoro.age) 
print(zoro.__doc__)


物件導向3
定義類別的方法

class Strawhat: 
  ''' 這是一個叫做 Strawhat 的類別 ''' def __init__(self, name, age): self.name = name self.age = age def print_age_2_yr_ago(self): age_2_yr_age = self.age - 2 print(self.name, "兩年前是", age_2_yr_age, "歲。") # 根據 Strawhat 類別建立物件 luffy = Strawhat("蒙其·D·魯夫", 19) # 使用 luffy 的 print_age_2_yr_ago() 方法 luffy.print_age_2_yr_ago()

文字(str)
my_str = "python for Data science!!!"

print(my_str.startswith("Python"))
print(my_str.endswith("!"))
print(my_str.find("for"))
print(my_str.count("!"))
print(my_str.capitalize())
print(my_str.title())
print(my_str.upper())
print(my_str.lower())
print(my_str.swapcase())
print(my_str.replace("science", "analysis"))

out:
False
True 
Python for data science!!! 
Python For Data Science!!! 
PYTHON FOR DATA SCIENCE!!! 
python for data science!!! 
PYTHON FOR dATA SCIENCE!!! 
python for Data analysis!!!

list
age = [19, 21, 20, 19, 21, 17, 30, 36]

age.append(90)
print(age)
brook_age = age.pop()
print(brook_age)
print(age)
age.insert(8, brook_age)
age.remove(brook_age)
print(age)
print(age.index(21))
age.append(90)
age.append(90)
print(age.count(90))
age.sort()
print(age)
age.reverse()
print(age)

out:
[19, 21, 20, 19, 21, 17, 30, 36, 90]
90 
[19, 21, 20, 19, 21, 17, 30, 36] 
[19, 21, 20, 19, 21, 17, 30, 36] 
[17, 19, 19, 20, 21, 21, 30, 36, 90, 90] 
[90, 90, 36, 30, 21, 21, 20, 19, 19, 17]

dict
# as of 2016-12-13
team_name = "金州勇士隊"
wins = 21
losses = 4
win_percentage = 0.84
is_on_winning_streak = True

golden_state_warriors = {
    "team_name": team_name,
    "wins": wins,
    "losses": losses,
    "win_percentage": win_percentage,
    "is_on_winning_streak": is_on_winning_streak
}

print(golden_state_warriors.get("wins"))
print(golden_state_warriors.keys()) # 用 list 把結果轉換
print(golden_state_warriors.values()) # 用 list 把結果轉換

out:
21
dict_keys(['is_on_winning_streak', 'wins', 'team_name', 'win_percentage', 'losses']) dict_values([True, 21, '金州勇士隊', 0.84, 4])

ndarray
import numpy as np

print(np.zeros(6)) # 六個元素均為零的 1d array
print(np.arange(11)) # 十一個元素為 0 到 10 的 1d array
print(np.arange(1, 11)) # 十個元素為 1 到 10 的 1d array
print(np.arange(10).astype(np.str)) # 利用 astype() 方法轉換為文字 
print(np.arange(10).reshape(2, 5)) # 利用 reshape() 轉換為矩陣
print(np.arange(10).reshape(2, 5).T) # 利用 T 屬性轉置
print(np.random.normal(size = 10)) # 生成 10 組標準常態分配(平均值為 0,標準差為 1 的常態分配)隨機變數
print(np.random.uniform(size = 10)) # 生成 10 組介於 0 與 1 之間均勻分配隨機變數

out:
[ 0. 0. 0. 0. 0. 0.]
[ 0 1 2 3 4 5 6 7 8 9 10] 
[ 1 2 3 4 5 6 7 8 9 10] 
['0' '1' '2' '3' '4' '5' '6' '7' '8' '9'] 
[[0 1 2 3 4] [5 6 7 8 9]] 
[[0 5] [1 6] [2 7] [3 8] [4 9]] 
[-1.54011452 0.01713279 -0.03278685 1.4184904 1.75206569 -1.14644167 1.912005 -0.41178504 0.11401443 1.28097786] 
[ 0.12950068 0.55311978 0.18856714 0.59106765 0.21379646 0.010011 0.88442601 0.11328004 0.32626053 0.46299239]

data frame
import pandas as pd

name = ["蒙其·D·魯夫", "羅羅亞·索隆", "娜美", "騙人布", "賓什莫克·香吉士", "多尼多尼·喬巴", "妮可·羅賓", "佛朗基", "布魯克"]
age = [19, 21, 20, 19, 21, 17, 30, 36, 90]

# 建立 dict
straw_hat_dict = {"name": name,
                  "age": age
}

# 建立 data frame
straw_hat_df = pd.DataFrame(straw_hat_dict)

# 刪除觀測值或欄位
straw_hat_df.drop(0, axis = 0)
straw_hat_df.drop(1, axis = 1)

# 選擇觀測值或欄位
straw_hat_df.ix[0, :]
straw_hat_df.ix[:, 0]
straw_hat_df.ix[0, 0]

# 透過布林值篩選
filter = straw_hat_df.age >= 30
straw_hat_df[filter]
filter = straw_hat_df.age < 30
filter_2 = straw_hat_df.name == "蒙其·D·魯夫"
straw_hat_df[filter & filter_2]

查詢
在 Jupyter Notebook 中以 tab 鍵查詢
以 dir() 函數查詢=> $ dit()
官方文件與 Google








沒有留言:

張貼留言