资源简介
循环神经网络的python应用代码。可参考。但注释较少,适合一定基础的,下载时请慎重。
代码片段和文件信息
“““
Fetch the daily stock prices from Google Finance for stocks in S & P 500.
@author: lilianweng
“““
import click
import os
import pandas as pd
import random
import time
import urllib2
from BeautifulSoup import BeautifulSoup
from datetime import datetime
DATA_DIR = “data“
RANDOM_SLEEP_TIMES = (1 5)
# This repo “github.com/datasets/s-and-p-500-companies“ has some other information about
# S & P 500 companies.
SP500_LIST_URL = “https://raw.githubusercontent.com/datasets/s-and-p-500-companies/master/data/constituents-financials.csv“
SP500_LIST_PATH = os.path.join(DATA_DIR “constituents-financials.csv“)
def _download_sp500_list():
if os.path.exists(SP500_LIST_PATH):
return
f = urllib2.urlopen(SP500_LIST_URL)
#print “Downloading ...“ SP500_LIST_URL
with open(SP500_LIST_PATH ‘w‘) as fin:
print >> fin f.read()
return
def _load_symbols():
_download_sp500_list()
df_sp500 = pd.read_csv(SP500_LIST_PATH)
df_sp500.sort(‘Market Cap‘ ascending=False inplace=True)
stock_symbols = df_sp500[‘Symbol‘].unique().tolist()
#print “Loaded %d stock symbols“ % len(stock_symbols)
return stock_symbols
def fetch_prices(symbol out_name):
“““
Fetch daily stock prices for stock ‘symbol‘ since 1980-01-01.
Args:
symbol (str): a stock abbr. symbol like “GOOG“ or “AAPL“.
Returns: a bool whether the fetch is succeeded.
“““
# Format today‘s date to match Google‘s finance history api.
now_datetime = datetime.now().strftime(“%b+%d+%Y“)
base_URL = “https://finance.google.com/finance/historical?output=csv&q={0}&startdate=Jan+1%2C+1980&enddate={1}“
symbol_url = base_URL.format(
urllib2.quote(symbol)
urllib2.quote(now_datetime ‘+‘)
)
print (“Fetching {} ...“.format(symbol))
print (symbol_url)
try:
f = urllib2.urlopen(symbol_url)
with open(out_name ‘w‘) as fin:
print >> fin f.read()
except urllib2.HTTPError:
print (“Failed when fetching {}“.format(symbol))
return False
data = pd.read_csv(out_name)
if data.empty:
print (“Remove {} because the data set is empty.“.format(out_name))
os.remove(out_name)
else:
dates = data.iloc[:0].tolist()
print (“# Fetched rows: %d [%s to %s]“ % (data.shape[0] dates[-1] dates[0]))
# Take a rest
sleep_time = random.randint(*RANDOM_SLEEP_TIMES)
print (“Sleeping ... %ds“ % sleep_time)
time.sleep(sleep_time)
return True
@click.command(help=“Fetch stock prices data“)
@click.option(‘--continued‘ is_flag=True)
def main(continued):
random.seed(time.time())
num_failure = 0
# This is S&P 500 index
#fetch_prices(‘INDEXSP%3A.INX‘)
symbols = _load_symbols()
for idx sym in enumerate(symbols):
out_name = os.path.join(DATA_DIR sym + “.csv“)
if continued and os.path.exists(out_name):
print(“Fetched“ sym)
continue
succee 属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-06-14 15:42 stock-rnn-master\
文件 117 2018-03-13 15:08 stock-rnn-master\.gitignore
文件 2303 2018-03-13 15:08 stock-rnn-master\README.md
目录 0 2018-06-14 15:38 stock-rnn-master\data\
文件 0 2018-03-13 15:08 stock-rnn-master\data\.placeholder
文件 3234 2018-06-14 15:42 stock-rnn-master\data_fetcher.py
文件 2555 2018-03-13 15:08 stock-rnn-master\data_model.py
文件 3849 2018-03-13 15:08 stock-rnn-master\main.py
文件 13033 2018-03-13 15:08 stock-rnn-master\model_rnn.py
目录 0 2018-06-14 15:38 stock-rnn-master\sc
文件 2780 2018-03-13 15:08 stock-rnn-master\sc
文件 659 2018-03-13 15:08 stock-rnn-master\sc
文件 1131 2018-03-13 15:08 stock-rnn-master\sc
文件 4455 2018-03-13 15:08 stock-rnn-master\sc
- 上一篇:python资料.txt
- 下一篇:逻辑回归python实现
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
川公网安备 51152502000135号
评论
共有 条评论