ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

读 python 机器学习实践指南

2019-09-20 13:43:09  阅读:286  来源: 互联网

标签:指南 plt width python petal 实践 df ax import


本书分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  抓取房源数据失败,尝试使用 八爪鱼 来抓取房源数据,但是免费版本的软件抓数据的能力实在太差 因此本章节没有完成代码实现功能。

 

标签:指南,plt,width,python,petal,实践,df,ax,import
来源: https://blog.csdn.net/wzy15965343032/article/details/101054379

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有