讀 python 機器學習實踐指南

本書分8個章節

第1 章,Python 機器學習的生態系統,深入Python,它有一個深度活躍的開發者社區,而且許多開發者來自科學社區。這爲Python 提供了豐富的科學計算庫。在本章中,我們將討論這些關鍵庫的特性以及如何準備你的環境,以最好地利用它們。


第 2 章,構建應用程序,發現低價的公寓,指導我們構建第一個機器學習應用程序,我們從一個最小但實際的例子開始:建設應用程序來識別低價的公寓。到本章結束,我們將創建一個應用程序,使得尋找合適的公寓變得更容易點。


第 3 章,構建應用程序,發現低價的機票,演示瞭如何構建應用程序來不斷地監測票價。一旦出現異常價格,應用程序將提醒我們,可以快速採取行動。
第 4 章,使用邏輯迴歸預測IPO 市場,展示了我們如何使用機器學習決定哪些IPO 值得仔細研究,而哪些可以直接跳過。

第 5 章,創建自定義的新聞源,介紹如何構建一個系統,它會了解你對於新聞的品味,而且每天都可以爲你提供個性化的新聞資訊。
第 6 章,預測你的內容是否會廣爲流傳,檢查一些被大家廣泛分享的內容,並試圖找到這種內容相對於其他人們不願分享的內容有哪些特點。
第 7 章,使用機器學習預測股票市場,討論如何構建和測試交易策略。當你試圖設計屬於自己的系統時,有無數的陷阱要避免,這是一個幾乎不可能完成的任務。但是,這個過程有很多的樂趣,而且有的時候,它甚至可以幫你盈利。


第 8 章,建立圖像相似度的引擎,幫助你構建高級的、基於圖像的深度學習應用。我們還將涵蓋深度學習的算法來了解爲什麼它們是如此的重要,以及爲什麼它們成爲了最近研究的熱點。


第 9 章,打造聊天機器人,演示如何從頭構建一個聊天機器人。讀完之後,你將瞭解更多關於該領域的歷史及其未來前景。


第 10 章,構建推薦引擎,探討不同類型的推薦系統。我們將看到它們在商業中是如何實現和運作的。我們還將實現自己的推薦引擎來查找GitHub 資料庫。

第一章節主要是瞭解了:機器學習的幾個步驟 :  獲取  -----  檢查和探索方法  ----  準備 清理  ---  建模 ---- 評估-----部署 。

按照機器學習的6個步驟 在pycharm上實現data數據的處理。 花類型下的 花瓣長寬 花萼的長寬 的參數關係研究並使用可視化的工具做出關係圖。使用到的庫及代碼見下圖。


"""NO.1"""
# import requests
# r = requests.get(r"https://api.github.com/users/acombs/starred")
# r2 = r.json()
# # f = open('requests_txt.txt', mode='w+', encoding='utf-8')
# # f.write(str(r2))
# # f.close()
#
# print( r2 )


"""NO.2使用pandas 分析數據data """

# import os
# import pandas as pd
# import requests
# # PATH = r'E:/Users/iris/'
# PATH = r'E:\python_data\iris'
# r = requests.get('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data')
# with open(PATH + 'iris.data', 'w') as f:
#     f.write(r.text)
#
# os.chdir(PATH)
# df = pd.read_csv(PATH + 'iris.data', names=['sepal length', 'sepal width','petal length', 'petal width', 'class'])
# df.head()
# print(df.head())
# print()
# print(df['sepal length'])
# print()
# print(df[(df['class']=='Iris-virginica')&(df['petal width']>2.2)])
# print()
# print(df.corr())

"""NO.3 顯示直方圖 只顯示 petal width 數據列"""
# import os
# import pandas as pd
#
# import matplotlib.pyplot as plt
# plt.style.use('ggplot')
# # matplotlib inline
# import numpy as np
# PATH = r'E:\python_data\iris'
# df = pd.read_csv(PATH + 'iris.data', names=['sepal length', 'sepal width','petal length', 'petal width', 'class'])
#
# fig, ax = plt.subplots(figsize=(6,4))
# ax.hist(df['petal width'], color='black');
# ax.set_ylabel('Count', fontsize=12)
# ax.set_xlabel('Width', fontsize=12)
# plt.title('Iris Petal Width', fontsize=14, y=1.01)
# plt.show()


"""NO.4 顯示直方圖 只顯示4列數據列"""
# import os
# import pandas as pd
#
# import matplotlib.pyplot as plt
# plt.style.use('ggplot')
# # matplotlib inline
# import numpy as np
# PATH = r'E:\python_data\iris'
# df = pd.read_csv(PATH + 'iris.data', names=['sepal length', 'sepal width','petal length', 'petal width', 'class'])
#
# fig, ax = plt.subplots(2,2, figsize=(6, 4))
#
# ax[0][0].hist(df['petal width'], color='black')
# ax[0][0].set_ylabel('Count', fontsize=12)
# ax[0][0].set_xlabel('Width', fontsize=12)
# ax[0][0].set_title('Iris Petal Width', fontsize=14, y=1.01)
#
# ax[0][1].hist(df['petal length'], color='black')
# ax[0][1].set_ylabel('Count', fontsize=12)
# ax[0][1].set_xlabel('Lenth', fontsize=12)
# ax[0][1].set_title('Iris Petal Lenth', fontsize=14, y=1.01)
#
# ax[1][0].hist(df['sepal width'], color='black')
# ax[1][0].set_ylabel('Count', fontsize=12)
# ax[1][0].set_xlabel('Width', fontsize=12)
# ax[1][0].set_title('Iris Sepal Width', fontsize=14, y=1.01)
#
# ax[1][1].hist(df['sepal length'], color='black')
# ax[1][1].set_ylabel('Count', fontsize=12)
# ax[1][1].set_xlabel('Length', fontsize=12)
# ax[1][1].set_title('Iris Sepal Length', fontsize=14, y=1.01)
#
# plt.tight_layout()
# """自動調整佈局避免擁擠 """
# plt.show()


""""NO.5 散點圖  """
# import os
# import pandas as pd
#
# import matplotlib.pyplot as plt
# plt.style.use('ggplot')
# # matplotlib inline
# import numpy as np
# PATH = r'E:\python_data\iris'
# df = pd.read_csv(PATH + 'iris.data', names=['sepal length', 'sepal width','petal length', 'petal width', 'class'])
# fig, ax = plt.subplots(figsize=(6,6))
# ax.scatter(df['petal width'], df['petal length'], color='green')
# ax.set_xlabel('Petal Width')
# ax.set_ylabel('Petal Length')
# ax.set_title('Petal Scatterplot')
# plt.show()


# """"NO.6 線圖  """
# import os
# import pandas as pd
#
# import matplotlib.pyplot as plt
# plt.style.use('ggplot')
# # matplotlib inline
# import numpy as np
# PATH = r'E:\python_data\iris'
# df = pd.read_csv(PATH + 'iris.data', names=['sepal length', 'sepal width','petal length', 'petal width', 'class'])
#
# fig, ax = plt.subplots(figsize=(6, 6))
# ax.plot(df['petal length'], color='blue')
# ax.set_xlabel('Specimen Number')
# ax.set_ylabel('Petal Length')
# ax.set_title('Petal Length Plot')
#
# plt.show()

""""使用 seaborn 庫繪圖1  """
# import seaborn as sns
# import pandas as pd
# import matplotlib.pyplot as plt
#
# PATH = r'E:\python_data\iris'
# df = pd.read_csv(PATH + 'iris.data', names=['sepal length', 'sepal width','petal length', 'petal width', 'class'])
#
# sns.pairplot(df, hue='class')
#
# plt.show()
""""使用 seaborn 庫繪圖2  """
# import seaborn as sns
# import pandas as pd
# import matplotlib.pyplot as plt
#
# PATH = r'E:\python_data\iris'
# df = pd.read_csv(PATH + 'iris.data', names=['sepal length', 'sepal width','petal length', 'petal width', 'class'])
#
# fig, ax = plt.subplots(2, 2, figsize=(7, 7))
# sns.set(style='white', palette='muted')
# sns.violinplot(x=df['class'], y=df['sepal length'], ax=ax[0,0])
# sns.violinplot(x=df['class'], y=df['sepal width'], ax=ax[0,1])
# sns.violinplot(x=df['class'], y=df['petal length'], ax=ax[1,0])
# sns.violinplot(x=df['class'], y=df['petal width'], ax=ax[1,1])
# fig.suptitle('Violin Plots', fontsize=16, y=1.03)
#
# for i in ax.flat:
#     plt.setp(i.get_xticklabels(), rotation=-90)
# fig.tight_layout()
#
# plt.show()


"""使用Statsmodels 繪製散點圖以及尋找散點圖的關係公式並繪製迴歸曲線 """

# import pandas as pd
# import matplotlib.pyplot as plt
# import statsmodels.api as sm
#
# PATH = r'E:\python_data\iris'
# df = pd.read_csv(PATH + 'iris.data', names=['sepal length', 'sepal width','petal length', 'petal width', 'class'])
#
# fig, ax = plt.subplots(figsize=(7, 7))
# ax.scatter(df['sepal width'][:50], df['sepal length'][:50])
# ax.set_ylabel('Sepal Length')
# ax.set_xlabel('Sepal Width')
# ax.set_title('Setosa Sepal Width vs. Sepal Length', fontsize=14, y=1.02)
#
# # plt.show()
#
# y = df['sepal length'][:50]
# x = df['sepal width'][:50]
# X = sm.add_constant(x)
#
# results = sm.OLS(y, X).fit()
#
# print(results.summary())
#
# fig, ax = plt.subplots(figsize=(7,7))
# ax.plot(x, results.fittedvalues, label='regression line')
# ax.scatter(x, y, label='data point', color='r')
# ax.set_ylabel('Sepal Length')
# ax.set_xlabel('Sepal Width')
# ax.set_title('Setosa Sepal Width vs. Sepal Length', fontsize=14, y=1.02)
# ax.legend(loc=2)
#
# plt.show()


"""使用 scikit-learn 庫實現識別  """

import pandas as pd
import matplotlib.pyplot as plt

from sklearn.ensemble import RandomForestClassifier
# from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split


PATH = r'E:\python_data\iris'
df = pd.read_csv(PATH + 'iris.data', names=['sepal length', 'sepal width','petal length', 'petal width', 'class'])

clf = RandomForestClassifier(max_depth=5, n_estimators=10)
X = df.iloc[:, :4]
y = df.iloc[:, 4]

print(X)
print()
print(y)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
rf = pd.DataFrame(list(zip(y_pred, y_test)), columns=['predicted', 'actual'])
rf['correct'] = rf.apply(lambda r: 1 if r['predicted'] == r['actual'] else 0, axis=1)

# print(rf)
print(rf['correct'].sum()/rf['correct'].count())


 

第二章: import.io  抓取房源數據失敗,嘗試使用 八爪魚 來抓取房源數據,但是免費版本的軟件抓數據的能力實在太差 因此本章節沒有完成代碼實現功能。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章