iT邦幫忙

1

Day11 職訓(機器學習與資料分析工程師培訓班): Python程式設計, 建立Model+載入

  • 分享至 

  • xImage
  •  

上午:Python程式設計

  • 早上學習function, *args, *kwargs, 全域變數 & 區域變數
def guess():
    while True:
        a = int(input('猜數字'))
        if a > 10:
            print('小一點')
            continue
        elif a <= 9:
            print('大一點')
            continue
        elif a == 10:
            print('恭喜答對')
            break
    else:
        print('再猜一次')
# f2c= 0 --> degree為華氏 
def convert_C_F(degree, f2c = 0):
    if f2c== 0:
        f_degree = degree*(9/5) + 32
        print(f'攝氏{degree}度 = 華氏{f_degree}度')
    else:
        c_degree = (degree-32)*9/5
        print(f'華氏{degree}度 = 攝氏{c_degree}度' )
# 從任一個數的數列取出最大值
def find_max(*args):
    print(max(args))
    
find_max(1,8,4,9)
# 任一數列求相鄰數字的差、和、乘積、及每個數的平方
def caluate(*num):
    diff = []
    summ = []
    mult = []
    square = []
    for i in range(len(num)):
        if i<len(num)-1:
            diff.append(num[i+1]-num[i])
            summ.append(num[i+1]+num[i])
            mult.append(num[i+1]*num[i])
        square.append(num[i]*num[i])
    print(diff)
    print(summ)
    print(mult)
    print(square)
    
caluate(1,2,3,4)
# 全域變數 & 區域變數
a=b=c=1
def test(b):
    a=2
    print(a,b,c)
    
test(2)
print(a,b,c)   

**下午:人工智慧與機器學系概論

練習使用Spyder,利用excel建立csv檔,再將model套用csv預測數值

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
# step1 load data
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LogisticRegression as LR

data = pd.read_csv('training.csv')
test = pd.read_csv('testing.csv')
print(data.head())

# step 2: prepare X,Y
X = data['x'].values.reshape(-1,1)
Y = data['y'].values.reshape(-1,1)
testX = test['x'].values.reshape(-1,1)

# step 3: Build model

model = LR()
model.fit(X, Y)

a = model.coef_
b = model.intercept_

#儲存模型
import pickle
import gzip
with gzip.GzipFile('myModle.pgz', 'w') as f:
    pickle.dump(model,f)
    
# step 4: Evaluate
from sklearn.metrics import classification_report 
preY = model.predict(X)
target_names = ['red', 'green']
print(classification_report(Y, preY, target_names=target_names)) 

# step 5: Deploy
testY = model.predict(testX)
test['y'] = testY
test.to_csv('result.csv', index = False, mode = 'w')
# step1 load data ******************
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LogisticRegression as LR

#data = pd.read_csv('training.csv')
test = pd.read_csv('testing.csv')


# step 2: prepare X,Y ****************

testX = test['x'].values.reshape(-1,1)

# step 3: Load model *****************

import pickle
import gzip
with gzip.open('myModle.pgz', 'r') as f:
    model=pickle.load(f)
    
# step 4: Evaluate *********************

# step 5: Deploy **********************
testY = model.predict(testX)
test['y'] = testY

test.to_csv('result.csv', index = False, mode = 'w')

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言