2020年6月5日 星期五

my_function.py

def df_to_mysql(df,table_name):
    import pyodbc
    cn = pyodbc.connect('DRIVER={MySQL ODBC 8.0 ANSI Driver};SERVER=localhost;DATABASE=my_db;USER=;PASSWORD=;OPTION=3;')
    cn.execute("truncate table " + table_name)
    for row in df.values:
        SQL = ""
        for i in row:
            if SQL == "":
                SQL = "insert into " + table_name + " values ('" + str(i) + "'"
            else:
                SQL += ",'" + str(i) + "'"
        SQL += ")"
        #print(SQL)
        cn.execute(SQL)
    cn.commit()
    cn.close()
    return "df_to_mysql1 execute completed"



#執行前確認DaraFrame與DB Schema一致
from my_function import df_to_mysql
print(df_to_mysql(df,"iris"))

python training 資料準備 (以DataFrame匯入) KNN

# 一開始先被好資料(df格式)
# 決定切割欄位
cols = ["x1","x2","x3","x4"]
x = np.array(df[cols])
y = np.array(df["y"])

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(x,y,test_size=0.3, random_state=88)

#使用KNN演算法
from sklearn.neighbors import KNeighborsClassifier

#從k=1開始測試
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train,y_train)
pred = knn.predict(X_test)

#使用決策樹
from sklearn import tree
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X_train,y_train)
tree.plot_tree(clf)
#pred = clf.predict(X_test)

#測試資料與預測資料比較
from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,pred))
print(classification_report(y_test,pred))

python dict 字典

countries = ['China', 'Japan', 'North Korea', 'South Korea', 'Taiwan', 'Thailand']
capitals = ['Beijing', 'Tokyo', 'Pyongyang', 'Seoul', 'Taipei', 'Bangkok']
#將list合併後轉成字典
d = dict(zip(countries,capitals))

#增加
d["United States"] = "Washington D.C."
#刪除
del d["Thailand"]

建立新字典,將舊字典的 key,value互換
new_dict = {}
for key,value in d.items():
    new_dict[value]=key
修改
new_dict["Washington D.C."] = "US"

python list

list to dict 字典
countries = ['China', 'Japan', 'North Korea', 'South Korea', 'Taiwan', 'Thailand']
capitals = ['Beijing', 'Tokyo', 'Pyongyang', 'Seoul', 'Taipei', 'Bangkok']
d = dict(zip(countries,capitals))

2020年6月4日 星期四

MySQL DATE_FORMAT() 函數 日期 時間

DATE_FORMAT(NOW(),'%Y-%m-%T')

格式描述
%a缩写星期名
%b缩写月名
%c月,数值
%D带有英文前缀的月中的天
%d月的天,数值(00-31)
%e月的天,数值(0-31)
%f微秒
%H小时 (00-23)
%h小时 (01-12)
%I小时 (01-12)
%i分钟,数值(00-59)
%j年的天 (001-366)
%k小时 (0-23)
%l小时 (1-12)
%M月名
%m月,数值(00-12)
%pAM 或 PM
%r时间,12-小时(hh:mm:ss AM 或 PM)
%S秒(00-59)
%s秒(00-59)
%T时间, 24-小时 (hh:mm:ss)
%U周 (00-53) 星期日是一周的第一天
%u周 (00-53) 星期一是一周的第一天
%V周 (01-53) 星期日是一周的第一天,与 %X 使用
%v周 (01-53) 星期一是一周的第一天,与 %x 使用
%W星期名
%w周的天 (0=星期日, 6=星期六)
%X年,其中的星期日是周的第一天,4 位,与 %V 使用
%x年,其中的星期一是周的第一天,4 位,与 %v 使用
%Y年,4 位
%y年,2 位

python 引用 py

a.py
name="weiwei"
age=18
def introduction():
    print "hi everyone, I'm",name,",",age,"years ago!"

b.py
from mei import introduction
introduction()

2020年6月2日 星期二

python 迴歸

import numpy as np
import matplotlib.pyplot as plt

#自訂x,y
x = np.array([1,2,3,4,5])
y = np.array([2,5,7,8,10])

#==========
# 散佈圖 + 迴歸
fig, ax = plt.subplots(figsize=(8, 4))

ax.scatter(x, y)

m, c = np.polyfit(x, y, 1) # 使用 Numpy 的 polyfit,參數 1 代表一維,算出 fit 直線斜率
ax.plot(x, m * x + c) # 使用 y = m * x + c 斜率和常數匯出直線

fig.suptitle('Scatter with regression')
plt.show()
#==========

predict = lambda x: m * x + c

print(predict(5))