Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
l2902392385
machine-learning-in-action-python3
提交
af2a6337
M
machine-learning-in-action-python3
项目概览
l2902392385
/
machine-learning-in-action-python3
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
machine-learning-in-action-python3
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
af2a6337
编写于
8月 17, 2018
作者:
H
hathackerwang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ex7
上级
687237aa
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
65 addition
and
0 deletion
+65
-0
ch07-Linear_regrs/Price_predict.py
ch07-Linear_regrs/Price_predict.py
+65
-0
未找到文件。
ch07-Linear_regrs/Price_predict.py
0 → 100644
浏览文件 @
af2a6337
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 14 15:05:47 2018
@author: Administrator
"""
#收集数据
#添加时间函数库
from
time
import
sleep
#添加json库
import
json
#添加urllib2库
import
requests
#@retX:样本玩具特征矩阵
#@retY:样本玩具的真实价格
#@setNum:获取样本的数量
#@yr:样本玩具的年份
#@numPce:玩具套装的零件数
#@origPce:原始价格
def
searchForSet
(
retX
,
retY
,
setNum
,
yr
,
numPce
,
origPrc
):
#睡眠十秒
sleep
(
10
)
#拼接查询的url字符串
myAPIstr
=
'get from code.google.com'
searchURL
=
'https://www.googleapis.com/shopping/search/v1/public/products?
\
key=%s&country=US&q=lego+%d&alt=json'
%
(
myAPIstr
,
setNum
)
#利用urllib2访问url地址
pg
=
requests
.
urlopen
(
searchURL
)
#利用json打开和解析url获得的数据,数据信息存入字典中
retDict
=
json
.
load
(
pg
.
read
())
#遍历数据的每一个条目
for
i
in
range
(
len
(
retDict
[
'items'
])):
try
:
#获得当前条目
currItem
=
retDict
[
'items'
][
i
]
#当前条目对应的产品为新产品
if
currItem
[
'product'
][
'condition'
]
==
'new'
:
#标记为新
newFlag
=
1
else
:
newFlag
=
0
#得到当前目录产品的库存列表
listOfInv
=
currItem
[
'product'
][
'inventories'
]
#遍历库存中的每一个条目
for
item
in
listOfInv
:
#得到该条目玩具商品的价格
sellingPrice
=
item
[
'price'
]
#价格低于原价的50%视为不完整套装
if
sellingPrice
>
(
origPrc
*
0.5
):
print
(
'%d
\t
%d
\t
%d
\t
%f
\t
%f'
%
(
yr
,
numPce
,
newFlag
,
\
origPrc
,
sellingPrice
))
#将符合条件套装信息作为特征存入数据矩阵
retX
.
append
([
yr
,
numPce
,
newFlag
,
origPce
])
#将对应套装的出售价格存入矩阵
retY
.
append
(
sellingPrice
)
except
:
print
(
'problem with item %d'
%
i
)
#多次调用收集数据函数,获取多组不同年份,不同价格的数据
def
setDataCollect
(
retX
,
retY
):
searchForSet
(
retX
,
retY
,
8288
,
2006
,
800
,
49.99
)
searchForSet
(
retX
,
retY
,
10030
,
2002
,
3096
,
49.99
)
searchForSet
(
retX
,
retY
,
10179
,
2007
,
5195
,
499.99
)
searchForSet
(
retX
,
retY
,
10181
,
2007
,
3428
,
199.99
)
searchForSet
(
retX
,
retY
,
10189
,
2008
,
5922
,
299.99
)
searchForSet
(
retX
,
retY
,
10196
,
2009
,
3263
,
249.99
)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录