深度学习

深度学习

Python爬虫学习者需要注意什么?

python爬虫wanbainip 发表了文章 • 0 个评论 • 2374 次浏览 • 2020-10-28 17:14 • 来自相关话题

在这个大数据时代里,学习Python网络爬虫的人越来越多,在学习过程中难免会遇到一些问题,学习者最常见的问题就是遇到ip被限制,因为你在爬虫采集数据过程中,同一个ip频繁的对网站进行访问,会给对方服务器会造成压力, 那么网站就根据ip访问的频率来限制你的ip,这也是反爬虫机制常见的一种判断。


最常见的解决方法就是使用大量的ip,就是借着代理ip保证IP被封时有替换IP可用,永远保持着续航能力。这里推荐51代理ip,作为一家提供代理IP的专业服务商,万变ip代理拥有强大的技术团队运营维护,全高匿系统所产生的高匿ip不仅安全稳定、而且速度快, 以及与爬虫用户多年来合作的宝贵经验,是Python爬虫首选代理IP。

Python是一种全栈计算机程序设计语言,全栈,顾名思义,应用范围广。你可能听说过很多编程语言,例如C语言,Java语言等,众所周知,这些语言都非常难学,更别说景桐使用了。而python不一样,比如完成一个Web服务,C语言要写1000行代码,Java要写100行,而python可能只要写20行。对!这就是差距!目前由于python“简单易懂”,已逐步成为网络爬虫主流语言。

在初学python爬虫时,很多程序员会被一些“小问题”阻碍脚步,为避免大家再次犯同样的错误,加快学习进程,在爬取网站信息时一定要使用大量代理IP。好用的代理IP服务商,

高效率的爬虫工作离不开ip代理的支持,这就是ip代理越来越受欢迎的原因!收藏举报投诉 查看全部
在这个大数据时代里,学习Python网络爬虫的人越来越多,在学习过程中难免会遇到一些问题,学习者最常见的问题就是遇到ip被限制,因为你在爬虫采集数据过程中,同一个ip频繁的对网站进行访问,会给对方服务器会造成压力, 那么网站就根据ip访问的频率来限制你的ip,这也是反爬虫机制常见的一种判断。


最常见的解决方法就是使用大量的ip,就是借着代理ip保证IP被封时有替换IP可用,永远保持着续航能力。这里推荐51代理ip,作为一家提供代理IP的专业服务商,万变ip代理拥有强大的技术团队运营维护,全高匿系统所产生的高匿ip不仅安全稳定、而且速度快, 以及与爬虫用户多年来合作的宝贵经验,是Python爬虫首选代理IP。

Python是一种全栈计算机程序设计语言,全栈,顾名思义,应用范围广。你可能听说过很多编程语言,例如C语言,Java语言等,众所周知,这些语言都非常难学,更别说景桐使用了。而python不一样,比如完成一个Web服务,C语言要写1000行代码,Java要写100行,而python可能只要写20行。对!这就是差距!目前由于python“简单易懂”,已逐步成为网络爬虫主流语言。

在初学python爬虫时,很多程序员会被一些“小问题”阻碍脚步,为避免大家再次犯同样的错误,加快学习进程,在爬取网站信息时一定要使用大量代理IP。好用的代理IP服务商,

高效率的爬虫工作离不开ip代理的支持,这就是ip代理越来越受欢迎的原因!收藏举报投诉

Python爬虫虎牙平台主播的图片代码

python爬虫wanbainip 发表了文章 • 0 个评论 • 2436 次浏览 • 2020-10-27 17:55 • 来自相关话题

今天万变ip就为大家分享一下,Python爬虫是如何爬取虎牙平台的主播图片。这里我们主要爬取虎牙星秀的主播图片,并下载保存到本地。在爬取之前,我们可先打开目标页面,F12审查元素,查找我们需要的信息所在的位置。

代码如下:

import urllib.request

import re

import os

# 全局变量用来记录图片的编号

gl_z = 0

def down_img(url1):

"""下载图片"""

# 处理图片链接,拼接http:

url = "https:" + re.sub(r"\?", "", url1)

global gl_z

print(url)

# 请求链接

response = urllib.request.urlopen(url)

# 读取内容

data = response.read()

# 切片取出图片名称

file_name = url[url.rfind('/') + 1:]

# 生成列表

a = [x for x in range(10000)]

# 打开文件用以写入

file = open(os.path.join("photo3", "img" + file_name + str(a[gl_z]) + ".jpg"), "wb")

file.write(data)

# 关闭文件

file.close()

# 编号加1

gl_z += 1

if __name__ == '__main__':

# 要抓去信息的网址

home = """http://www.huya.com/g/xingxiu"""

# 模拟请求头

headers = {

"Host": "www.huya.com",

"User-Agent": "agent信息"

}

# 构造好请求对象 将请求提交到服务器 获取的响应就是到首页的html代码

request = urllib.request.Request(url=home, headers=headers)

response = urllib.request.urlopen(request)

# 读取抓到的内容并解码

html_data = response.read().decode()

"""huyaimg.msstatic.com/avatar/1054/db/6590aa9bcf98e12e5d809d371e46cc_180_135.jpg

"""

# 使用正则 从首页中 提取出所有的图片链接

img_list = re.findall(r"//huyaimg\.msstatic\.com.+\.jpg\?", html_data)

print(img_list)

# 取出每张图片进行下载

for img_url in img_list:

print(img_url)

down_img(img_url) 查看全部
今天万变ip就为大家分享一下,Python爬虫是如何爬取虎牙平台的主播图片。这里我们主要爬取虎牙星秀的主播图片,并下载保存到本地。在爬取之前,我们可先打开目标页面,F12审查元素,查找我们需要的信息所在的位置。

代码如下:

import urllib.request

import re

import os

# 全局变量用来记录图片的编号

gl_z = 0

def down_img(url1):

"""下载图片"""

# 处理图片链接,拼接http:

url = "https:" + re.sub(r"\?", "", url1)

global gl_z

print(url)

# 请求链接

response = urllib.request.urlopen(url)

# 读取内容

data = response.read()

# 切片取出图片名称

file_name = url[url.rfind('/') + 1:]

# 生成列表

a = [x for x in range(10000)]

# 打开文件用以写入

file = open(os.path.join("photo3", "img" + file_name + str(a[gl_z]) + ".jpg"), "wb")

file.write(data)

# 关闭文件

file.close()

# 编号加1

gl_z += 1

if __name__ == '__main__':

# 要抓去信息的网址

home = """http://www.huya.com/g/xingxiu"""

# 模拟请求头

headers = {

"Host": "www.huya.com",

"User-Agent": "agent信息"

}

# 构造好请求对象 将请求提交到服务器 获取的响应就是到首页的html代码

request = urllib.request.Request(url=home, headers=headers)

response = urllib.request.urlopen(request)

# 读取抓到的内容并解码

html_data = response.read().decode()

"""huyaimg.msstatic.com/avatar/1054/db/6590aa9bcf98e12e5d809d371e46cc_180_135.jpg

"""

# 使用正则 从首页中 提取出所有的图片链接

img_list = re.findall(r"//huyaimg\.msstatic\.com.+\.jpg\?", html_data)

print(img_list)

# 取出每张图片进行下载

for img_url in img_list:

print(img_url)

down_img(img_url)

机器学习:港股首日上市价格预测

深度学习李魔佛 发表了文章 • 0 个评论 • 3637 次浏览 • 2020-09-25 22:57 • 来自相关话题

上一篇《python程序分析港股打新到底赚不赚钱》一文简单的分析了港股打新的盈利预期。 

    因为我们花了不少时间爬取了港股新股的数据,可以对这些数据加以利用,利用机器学习的模型,预测港股上市价格以及影响因素的权重。


    香港股市常年位于全球新股集资三甲之列,每年都有上百只新股上市。与已上市的正股相比,新股的特点是没有任何历史交易数据,这使新股的feature比较朴素,使其可以变成一个较为简单的机器学习问题。





我们在这里,以练手为目的,用新股首日涨跌幅的预测作为例子,介绍一个比较完整的机器学习流程。


数据获取

一个机器学习的项目,最重要的是数据。没有数据,一切再高级的算法都只是纸上谈兵。上一篇文中,已经获取了最近发行的新股的一些基本数据,还有一些详情数据在详细页里面,需要访问详情页获取。





 
比如农夫山泉,除了之前爬取的基本数据,如上市市值,招股价,中签率,超额倍数,现价等,还有一些保荐人,包销商等有价值数据,所以我们也需要顺带把这些数据获取过来。这时需要在上一篇文章的基础上,获取每一个新股的详情页地址,然后如法炮制,用xpath把数据提取出来。





基本数据页和详情页保存为2个csv文件:data/ipo_list.csv和data/ipo_details.csv







数据清理和特征提取

接下来要做的是对数据进行清理,扔掉无关的项目,然后做一些特征提取和特征处理。

爬取的两个数据,我们先用pandas读取进来,用股票代码code做index,然后合并成为一个大的dataframe.#Read two files and merge
df1 = pd.read_csv('../data/ipo_list', sep='\t', index_col='code')
df2 = pd.read_csv('../data/ipo_details', sep= '\t', index_col = 0)
#Use combine_first to avoid duplicate columns
df = df1.combine_first(df2)
我们看看我们的dataframe有哪些column先:df.columns.values

array(['area', 'banks', 'buy_ratio', 'category', 'date', 'draw_prob',
'eipo', 'firstday_performance', 'hk_portion', 'ipo_price',
'ipo_price_range', 'market_type', 'name', 'now_price', 'one_hand',
'predict_profile_market_ratio', 'predict_profit_ratio',
'profit_ratio', 'recommender', 'sales', 'shares_per_hand',
'stock_type', 'total_performance', 'total_value', 'website'], dtype=object)
我们的目标,也就是我们要预测的值,是首日涨跌幅,即firstday_performance. 我们需要扔掉一些无关的项目,比如日期、收票银行、网址、当前的股价等等。还要扔掉那些没有公开发售的全配售的股票,因为这些股票没有任何散户参与,跟我们目标无关。# Drop unrelated columns
to_del = ['date', 'banks', 'eipo', 'name', 'now_price', 'website', 'total_performance','predict_profile_market_ratio', 'predict_profit_ratio', 'profit_ratio']
for item in to_del:
del df[item]

#Drop non_public ipo stocks
df = df[df.draw_prob.notnull()]
对于百分比的数据,我们要换成float的形式:def per2float(x):
if not pd.isnull(x):
x = x.strip('%')
return float(x)/100.
else:
return x

#Format percentage
df['draw_prob'] = df['draw_prob'].apply(per2float)
df['firstday_performance'] = df['firstday_performance'].apply(per2float)
df['hk_portion'] = df['hk_portion'].apply(per2float)


 
对于”认购不足”的情况,我们要把超购数替换成为0:def buy_ratio_process(x):
if x == '认购不足':
return 0.0
else:
return float(x)

#Format buy_ratio
df['buy_ratio'] = df['buy_ratio'].apply(buy_ratio_process)
新股招股的IPO价格是一个区间。有一些新股,招股价上下界拉得很开。因为我们已经有了股价作为另一个,所以我们这里希望能拿到IPO招股价格的上下界范围与招股价相比的一个比例,作为一个新的特征:def get_low_bound(x):
if ',' in str(x):
x = x.replace(',', '')
try:
if pd.isnull(x) or '-' not in x:
return float(x)
else:
x = x.split('-')
return float(x[0])
except Exception as e:
print(e)
print(x)

def get_up_bound(x):
if ',' in str(x):
x = x.replace(',', '')
try:
if pd.isnull(x) or '-' not in x:
return float(x)
else:
x = x.split('-')
return float(x[1])
except Exception as e:
print(e)
print(x)

def get_ipo_range_prop(x):
if pd.isnull(x):
return x
low_bound = get_low_bound(x)
up_bound = get_up_bound(x)
return (up_bound-low_bound)*2/(up_bound+low_bound)

#Merge ipo_price_range to proportion of middle
df['ipo_price_range_ratio'] = df['ipo_price_range'].apply(get_ipo_range_prop)
del df['ipo_price_range']
我们取新股招股价对应总市值的中位数作为另一个特征。因为总市值的绝对值是一个非常大的数字,我们把它按比例缩小,使它的取值和其它特征在一个差不多的范围里。def get_total_value_mid(x):
if pd.isnull(x):
return x
low_bound = get_low_bound(x)
up_bound = get_up_bound(x)
return (up_bound+low_bound)/2

df['total_value_mid'] = df['total_value'].apply(get_total_value_mid)/1000000000.
del df['total_value']
于是我们的数据变成了这样一个278 rows × 15 columns的dataframe,即我们有278个数据点和15个特征:
 





我们看到诸如地区、业务种类等这些特征是categorical的。同时,保荐人和包销商又有多个item的情况。对于这种特征的处理,我们使用one-hot encoding,对每一个种类创建一个新的category,然后用0-1来表示instance是否属于这个category。
 #Now do one-hot encoding for all categorical columns
#One problem is that we have to split('、') first for contents with multiple companies

dftest = df.copy()

def one_hot_encoding(df, column_name):
#Reads a df and target column, does tailored one-hot encoding, and return new df for merge

cat_list = df[column_name].unique().tolist()
cat_set = set()
for items in cat_list:
if pd.isnull(items):
continue
items = items.split('、')
for item in items:
item = item.strip()
cat_set.add(item)
for item in cat_set:
item = column_name + '_' + item
df[item] = 0

def check_onehot(x, cat):
if pd.isnull(x):
return 0
x = x.split('、')
for item in x:
if cat == item.strip():
return 1
return 0

for item in cat_set:
df[column_name + '_' + item] = df[column_name].apply(check_onehot, args=(item, ))

del df[column_name]
return df

dftest = one_hot_encoding(dftest, 'area')
dftest = one_hot_encoding(dftest, 'category')
dftest = one_hot_encoding(dftest, 'market_type')
dftest = one_hot_encoding(dftest, 'recommender')
dftest = one_hot_encoding(dftest, 'sales')
dftest = one_hot_encoding(dftest, 'stock_type')这下我们的数据变成了一个278 rows × 535 columns的dataframe,即我们之前的15个特征因为one-hot encoding,一下子变成了535个特征。这其实是机器学习很常见的一个情况,即我们的数据是一个sparce matrix。





 
训练模型

有了已经整理好特征的数据,我们可以开始建立机器学习模型了。

这里我们用xgboost为例子建立一个非常简单的模型。xgboost是一个基于boosted tree的模型。大家也可以尝试其它更多的算法模型。

我们把数据读入,然后随机把1/3的股票数据分出来做testing data. 我们这里只是一个示例,更高级的方法可以做诸如n-fold validation,以及grid search寻找最优参数等。
 # load data and split feature and label
df = pd.read_csv('../data/hk_ipo_feature_engineered', sep='\t', index_col='code', encoding='utf-8')
Y = df['firstday_performance']
X = df.drop('firstday_performance', axis = 1)
# split data into train and test sets
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)
# fit model no training data
eval_set = [(X_test, y_test)]
因为新股首日涨跌幅是一个float,所以这是一个regression的问题。我们跑xgboost模型,输出mean squared error (越接近0表明准确率越高):
 # fit model no training data
xgb_model = xgb.XGBRegressor().fit(X_train,y_train)
predictions = xgb_model.predict(X_test)
actuals = y_test
print mean_squared_error(actuals, predictions)

0.0643324471123

可见准确率还是蛮高的。 xgboost自带了画出特征重要性的方法xgb.plot_importance。 用来描述每个特征对结果的重要程度。
 importance = xgb_model.booster().get_score(importance_type='weight')
tuples = [(k, importance[k]) for k in importance]然后利用matplotlib绘制图形。 





 
我们看到几个最强的特征,比如超额倍数、在香港发售的比例、ipo的价格和总市值(细价股更容易涨很多)等。

同时我们还发现了几个比较有意思的特征,比如东南亚地区的股票,和某些包销商与保荐人。


模型预测

这里就略过了。大家大可以自己将即将上市的港股新股做和上面一样的特征处理,然后预测出一个首日涨跌幅,待股票上市后做个对比了!

总结

我们用预测港股新股首日涨跌幅的例子,介绍了一个比较简单的机器学习的流程,包括了数据获取、数据清理、特征处理、模型训练和模型预测等。这其中每一个步骤都可以钻研得非常深;这篇文章只是蜻蜓点水,隔靴搔痒。

最重要的是,掌握了机器学习的知识,也许真的能帮助我们解决很多生活中实际的问题。比如,赚点小钱?


由于微信改版后不再是按时间顺序推送文章,如果后续想持续关注笔者的最新观点,请务必将公众号设为星标,并点击右下角的“赞”和“在看”,不然我又懒得更新了哈,还有更多很好玩的数据等着你哦。
 





 
原创文章,转载请注明牛出处
http://30daydo.com/article/608
  查看全部
上一篇《python程序分析港股打新到底赚不赚钱》一文简单的分析了港股打新的盈利预期。 

    因为我们花了不少时间爬取了港股新股的数据,可以对这些数据加以利用,利用机器学习的模型,预测港股上市价格以及影响因素的权重。


    香港股市常年位于全球新股集资三甲之列,每年都有上百只新股上市。与已上市的正股相比,新股的特点是没有任何历史交易数据,这使新股的feature比较朴素,使其可以变成一个较为简单的机器学习问题。

fe4c03249a9aba12936be1a86d0b69da.jpg

我们在这里,以练手为目的,用新股首日涨跌幅的预测作为例子,介绍一个比较完整的机器学习流程。


数据获取

一个机器学习的项目,最重要的是数据。没有数据,一切再高级的算法都只是纸上谈兵。上一篇文中,已经获取了最近发行的新股的一些基本数据,还有一些详情数据在详细页里面,需要访问详情页获取。

chrome_IrnpNEI2Na.png

 
比如农夫山泉,除了之前爬取的基本数据,如上市市值,招股价,中签率,超额倍数,现价等,还有一些保荐人,包销商等有价值数据,所以我们也需要顺带把这些数据获取过来。这时需要在上一篇文章的基础上,获取每一个新股的详情页地址,然后如法炮制,用xpath把数据提取出来。





基本数据页和详情页保存为2个csv文件:data/ipo_list.csv和data/ipo_details.csv







数据清理和特征提取

接下来要做的是对数据进行清理,扔掉无关的项目,然后做一些特征提取和特征处理。

爬取的两个数据,我们先用pandas读取进来,用股票代码code做index,然后合并成为一个大的dataframe.
#Read two files and merge
df1 = pd.read_csv('../data/ipo_list', sep='\t', index_col='code')
df2 = pd.read_csv('../data/ipo_details', sep= '\t', index_col = 0)
#Use combine_first to avoid duplicate columns
df = df1.combine_first(df2)

我们看看我们的dataframe有哪些column先:
df.columns.values

array(['area', 'banks', 'buy_ratio', 'category', 'date', 'draw_prob',
'eipo', 'firstday_performance', 'hk_portion', 'ipo_price',
'ipo_price_range', 'market_type', 'name', 'now_price', 'one_hand',
'predict_profile_market_ratio', 'predict_profit_ratio',
'profit_ratio', 'recommender', 'sales', 'shares_per_hand',
'stock_type', 'total_performance', 'total_value', 'website'], dtype=object)

我们的目标,也就是我们要预测的值,是首日涨跌幅,即firstday_performance. 我们需要扔掉一些无关的项目,比如日期、收票银行、网址、当前的股价等等。还要扔掉那些没有公开发售的全配售的股票,因为这些股票没有任何散户参与,跟我们目标无关。
# Drop unrelated columns
to_del = ['date', 'banks', 'eipo', 'name', 'now_price', 'website', 'total_performance','predict_profile_market_ratio', 'predict_profit_ratio', 'profit_ratio']
for item in to_del:
del df[item]

#Drop non_public ipo stocks
df = df[df.draw_prob.notnull()]

对于百分比的数据,我们要换成float的形式:
def per2float(x):
if not pd.isnull(x):
x = x.strip('%')
return float(x)/100.
else:
return x

#Format percentage
df['draw_prob'] = df['draw_prob'].apply(per2float)
df['firstday_performance'] = df['firstday_performance'].apply(per2float)
df['hk_portion'] = df['hk_portion'].apply(per2float)


 
对于”认购不足”的情况,我们要把超购数替换成为0:
def buy_ratio_process(x):
if x == '认购不足':
return 0.0
else:
return float(x)

#Format buy_ratio
df['buy_ratio'] = df['buy_ratio'].apply(buy_ratio_process)

新股招股的IPO价格是一个区间。有一些新股,招股价上下界拉得很开。因为我们已经有了股价作为另一个,所以我们这里希望能拿到IPO招股价格的上下界范围与招股价相比的一个比例,作为一个新的特征:
def get_low_bound(x):
if ',' in str(x):
x = x.replace(',', '')
try:
if pd.isnull(x) or '-' not in x:
return float(x)
else:
x = x.split('-')
return float(x[0])
except Exception as e:
print(e)
print(x)

def get_up_bound(x):
if ',' in str(x):
x = x.replace(',', '')
try:
if pd.isnull(x) or '-' not in x:
return float(x)
else:
x = x.split('-')
return float(x[1])
except Exception as e:
print(e)
print(x)

def get_ipo_range_prop(x):
if pd.isnull(x):
return x
low_bound = get_low_bound(x)
up_bound = get_up_bound(x)
return (up_bound-low_bound)*2/(up_bound+low_bound)

#Merge ipo_price_range to proportion of middle
df['ipo_price_range_ratio'] = df['ipo_price_range'].apply(get_ipo_range_prop)
del df['ipo_price_range']

我们取新股招股价对应总市值的中位数作为另一个特征。因为总市值的绝对值是一个非常大的数字,我们把它按比例缩小,使它的取值和其它特征在一个差不多的范围里。
def get_total_value_mid(x):
if pd.isnull(x):
return x
low_bound = get_low_bound(x)
up_bound = get_up_bound(x)
return (up_bound+low_bound)/2

df['total_value_mid'] = df['total_value'].apply(get_total_value_mid)/1000000000.
del df['total_value']

于是我们的数据变成了这样一个278 rows × 15 columns的dataframe,即我们有278个数据点和15个特征:
 

2.jpg

我们看到诸如地区、业务种类等这些特征是categorical的。同时,保荐人和包销商又有多个item的情况。对于这种特征的处理,我们使用one-hot encoding,对每一个种类创建一个新的category,然后用0-1来表示instance是否属于这个category。
 
#Now do one-hot encoding for all categorical columns
#One problem is that we have to split('、') first for contents with multiple companies

dftest = df.copy()

def one_hot_encoding(df, column_name):
#Reads a df and target column, does tailored one-hot encoding, and return new df for merge

cat_list = df[column_name].unique().tolist()
cat_set = set()
for items in cat_list:
if pd.isnull(items):
continue
items = items.split('、')
for item in items:
item = item.strip()
cat_set.add(item)
for item in cat_set:
item = column_name + '_' + item
df[item] = 0

def check_onehot(x, cat):
if pd.isnull(x):
return 0
x = x.split('、')
for item in x:
if cat == item.strip():
return 1
return 0

for item in cat_set:
df[column_name + '_' + item] = df[column_name].apply(check_onehot, args=(item, ))

del df[column_name]
return df

dftest = one_hot_encoding(dftest, 'area')
dftest = one_hot_encoding(dftest, 'category')
dftest = one_hot_encoding(dftest, 'market_type')
dftest = one_hot_encoding(dftest, 'recommender')
dftest = one_hot_encoding(dftest, 'sales')
dftest = one_hot_encoding(dftest, 'stock_type')
这下我们的数据变成了一个278 rows × 535 columns的dataframe,即我们之前的15个特征因为one-hot encoding,一下子变成了535个特征。这其实是机器学习很常见的一个情况,即我们的数据是一个sparce matrix。

3.jpg

 
训练模型

有了已经整理好特征的数据,我们可以开始建立机器学习模型了。

这里我们用xgboost为例子建立一个非常简单的模型。xgboost是一个基于boosted tree的模型。大家也可以尝试其它更多的算法模型。

我们把数据读入,然后随机把1/3的股票数据分出来做testing data. 我们这里只是一个示例,更高级的方法可以做诸如n-fold validation,以及grid search寻找最优参数等。
 
# load data and split feature and label
df = pd.read_csv('../data/hk_ipo_feature_engineered', sep='\t', index_col='code', encoding='utf-8')
Y = df['firstday_performance']
X = df.drop('firstday_performance', axis = 1)
# split data into train and test sets
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)
# fit model no training data
eval_set = [(X_test, y_test)]

因为新股首日涨跌幅是一个float,所以这是一个regression的问题。我们跑xgboost模型,输出mean squared error (越接近0表明准确率越高):
 
# fit model no training data
xgb_model = xgb.XGBRegressor().fit(X_train,y_train)
predictions = xgb_model.predict(X_test)
actuals = y_test
print mean_squared_error(actuals, predictions)

0.0643324471123

可见准确率还是蛮高的。 xgboost自带了画出特征重要性的方法xgb.plot_importance。 用来描述每个特征对结果的重要程度。
 
importance = xgb_model.booster().get_score(importance_type='weight')
tuples = [(k, importance[k]) for k in importance]
然后利用matplotlib绘制图形。 

4.png

 
我们看到几个最强的特征,比如超额倍数、在香港发售的比例、ipo的价格和总市值(细价股更容易涨很多)等。

同时我们还发现了几个比较有意思的特征,比如东南亚地区的股票,和某些包销商与保荐人。


模型预测

这里就略过了。大家大可以自己将即将上市的港股新股做和上面一样的特征处理,然后预测出一个首日涨跌幅,待股票上市后做个对比了!

总结

我们用预测港股新股首日涨跌幅的例子,介绍了一个比较简单的机器学习的流程,包括了数据获取、数据清理、特征处理、模型训练和模型预测等。这其中每一个步骤都可以钻研得非常深;这篇文章只是蜻蜓点水,隔靴搔痒。

最重要的是,掌握了机器学习的知识,也许真的能帮助我们解决很多生活中实际的问题。比如,赚点小钱?


由于微信改版后不再是按时间顺序推送文章,如果后续想持续关注笔者的最新观点,请务必将公众号设为星标,并点击右下角的“赞”和“在看”,不然我又懒得更新了哈,还有更多很好玩的数据等着你哦。
 

微信图片_20200923020240.jpg

 
原创文章,转载请注明牛出处
http://30daydo.com/article/608
 

深圳住房公积金验证码 识别破解

python爬虫李魔佛 发表了文章 • 0 个评论 • 2651 次浏览 • 2020-06-26 14:34 • 来自相关话题

 
http://gjj.sz.gov.cn/fzgn/zfcq/index.html
 
比较常规的验证码,使用keras全连接层,cv切割后每个字符只需要20个样本就达到准确率99%。
需要模型或者代码的私聊。 查看全部
1212.PNG

 
http://gjj.sz.gov.cn/fzgn/zfcq/index.html
 
比较常规的验证码,使用keras全连接层,cv切割后每个字符只需要20个样本就达到准确率99%。
需要模型或者代码的私聊。

多线程调用yolo模型会出错(包括使用cv2载入yolo模型)

深度学习李魔佛 发表了文章 • 3 个评论 • 4671 次浏览 • 2019-12-05 10:27 • 来自相关话题

占坑。
 
多线程调用yolo模型会出错(包括使用cv2载入yolo模型)
 
占坑。
 
多线程调用yolo模型会出错(包括使用cv2载入yolo模型)
 

keras yolo物体检测 入门教程

深度学习李魔佛 发表了文章 • 0 个评论 • 3040 次浏览 • 2019-11-28 16:03 • 来自相关话题

占坑
占坑

RuntimeError: `get_session` is not available when using TensorFlow 2.0.

深度学习李魔佛 发表了文章 • 0 个评论 • 12995 次浏览 • 2019-11-28 15:10 • 来自相关话题

这个问题是TensorFlow版本问题,在2.0以上get_session是被移除了。需要做一些修改,或者把tf降级。可以安装1.15版本
pip install tensorflow==1.15 --upgradeHere, we will see how we can upgrade our code to work with tensorflow 2.0.

This error is usually faced when we are loading pre-trained model with tensorflow session/graph or we are building flask api over a pre-trained model and loading model in tensorflow graph to avoid collision of sessions while application is getting multiple requests at once or say in case of multi-threadinng

After tensorflow 2.0 upgrade, i also started facing above error in one of my project when i had built api of pre-trained model with flask. So i looked around in tensorflow 2.0 documents to find a workaround, to avoid this runtime error and upgrade my code to work with tensorflow 2.0 as well rather than downgrading it to tensorflow 1.x .

I had a project on which i had written tutorial as well on how to build Flask api on trained keras model of text classification and then use it in production

But this project was not working after tensorflow upgrade and was facing runtime error.

Stacktrace of error was something like below:

File "/Users/Upasana/Documents/playground/deploy-keras-model-in-production/src/main.py", line 37, in model_predict
with backend.get_session().graph.as_default() as g:
File "/Users/Upasana/Documents/playground/deploy-keras-model-in-production/venv-tf2/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 379, in get_session
'`get_session` is not available '
RuntimeError: `get_session` is not available when using TensorFlow 2.0.
Related code to get model
with backend.get_session().graph.as_default() as g:
model = SentimentService.get_model1()
Related code to load model
def load_deep_model(self, model):
json_file = open('./src/mood-saved-models/' + model + '.json', 'r')
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json)

loaded_model.load_weights("./src/mood-saved-models/" + model + ".h5")

loaded_model._make_predict_function()
return loaded_model
get_session is removed in tensorflow 2.0 and hence not available.

so, in order to load saved model we switched methods. Rather than using keras’s load_model, we used tensorflow to load model so that we can load model using distribution strategy.

Note
The tf.distribute.Strategy API provides an abstraction for distributing your training across multiple processing units.

New code to get model
another_strategy = tf.distribute.MirroredStrategy()
with another_strategy.scope():
model = SentimentService.get_model1()
New code to load model
def load_deep_model(self, model):
loaded_model = tf.keras.models.load_model("./src/mood-saved-models/"model + ".h5")
return loaded_model
This worked and solved the problem with runtime error of get_session not available in tensorflow 2.0 . You can refer to Tensorflow 2.0 upgraded article too

Hope, this will solve your problem too. Thanks for following this article. 查看全部
这个问题是TensorFlow版本问题,在2.0以上get_session是被移除了。需要做一些修改,或者把tf降级。可以安装1.15版本
pip install tensorflow==1.15 --upgrade
Here, we will see how we can upgrade our code to work with tensorflow 2.0.

This error is usually faced when we are loading pre-trained model with tensorflow session/graph or we are building flask api over a pre-trained model and loading model in tensorflow graph to avoid collision of sessions while application is getting multiple requests at once or say in case of multi-threadinng

After tensorflow 2.0 upgrade, i also started facing above error in one of my project when i had built api of pre-trained model with flask. So i looked around in tensorflow 2.0 documents to find a workaround, to avoid this runtime error and upgrade my code to work with tensorflow 2.0 as well rather than downgrading it to tensorflow 1.x .

I had a project on which i had written tutorial as well on how to build Flask api on trained keras model of text classification and then use it in production

But this project was not working after tensorflow upgrade and was facing runtime error.

Stacktrace of error was something like below:

File "/Users/Upasana/Documents/playground/deploy-keras-model-in-production/src/main.py", line 37, in model_predict
with backend.get_session().graph.as_default() as g:
File "/Users/Upasana/Documents/playground/deploy-keras-model-in-production/venv-tf2/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 379, in get_session
'`get_session` is not available '
RuntimeError: `get_session` is not available when using TensorFlow 2.0.
Related code to get model
with backend.get_session().graph.as_default() as g:
model = SentimentService.get_model1()
Related code to load model
def load_deep_model(self, model):
json_file = open('./src/mood-saved-models/' + model + '.json', 'r')
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json)

loaded_model.load_weights("./src/mood-saved-models/" + model + ".h5")

loaded_model._make_predict_function()
return loaded_model
get_session is removed in tensorflow 2.0 and hence not available.

so, in order to load saved model we switched methods. Rather than using keras’s load_model, we used tensorflow to load model so that we can load model using distribution strategy.

Note
The tf.distribute.Strategy API provides an abstraction for distributing your training across multiple processing units.

New code to get model
another_strategy = tf.distribute.MirroredStrategy()
with another_strategy.scope():
model = SentimentService.get_model1()
New code to load model
def load_deep_model(self, model):
loaded_model = tf.keras.models.load_model("./src/mood-saved-models/"model + ".h5")
return loaded_model
This worked and solved the problem with runtime error of get_session not available in tensorflow 2.0 . You can refer to Tensorflow 2.0 upgraded article too

Hope, this will solve your problem too. Thanks for following this article.

sklearn中的Bunch数据类型

深度学习李魔佛 发表了文章 • 0 个评论 • 15635 次浏览 • 2018-06-07 19:10 • 来自相关话题

在sklearn中自带部分数据 如 datasets 包中




 
那么这个
<class 'sklearn.utils.Bunch'>
是什么数据格式 ?
 
打印一下:




好吧,原来就是一个字典结构。可以像调用字典一样使用Bunch。
比如 data['image'] 就获取 key为image的内容。
 

原创地址:http://30daydo.com/article/325 
欢迎转载,请注明出处。 查看全部
在sklearn中自带部分数据 如 datasets 包中
k1.PNG

 
那么这个
<class 'sklearn.utils.Bunch'>
是什么数据格式 ?
 
打印一下:
k3.PNG

好吧,原来就是一个字典结构。可以像调用字典一样使用Bunch。
比如 data['image'] 就获取 key为image的内容。
 

原创地址:http://30daydo.com/article/325 
欢迎转载,请注明出处。

sklearn中SGDClassifier分类器每次得到的结果都不一样?

深度学习李魔佛 发表了文章 • 0 个评论 • 7331 次浏览 • 2018-06-07 17:14 • 来自相关话题

如下代码:sgdc = SGDClassifier()
sgdc.fit(X_train,y_train)
sgdc_predict_y = sgdc.predict(X_test)
print 'Accuary of SGD classifier ', sgdc.score(X_test,y_test)
print classification_report(y_test,sgdc_predict_y,target_names=['Benign','Malignant'])
每次输出的结果都不一样? WHY
 
 
因为你使用了一个默认参数:
SGDClassifier(random_state = None)
 
所以这个随机种子每次不一样,所以得到的结果就可能不一样,如果你指定随机种子值,那么每次得到的结果都是一样的了。
 

原创地址:http://30daydo.com/article/323 
欢迎转载,请注明出处。 查看全部
如下代码:
sgdc = SGDClassifier()
sgdc.fit(X_train,y_train)
sgdc_predict_y = sgdc.predict(X_test)
print 'Accuary of SGD classifier ', sgdc.score(X_test,y_test)
print classification_report(y_test,sgdc_predict_y,target_names=['Benign','Malignant'])

每次输出的结果都不一样? WHY
 
 
因为你使用了一个默认参数:
SGDClassifier(random_state = None)
 
所以这个随机种子每次不一样,所以得到的结果就可能不一样,如果你指定随机种子值,那么每次得到的结果都是一样的了。
 

原创地址:http://30daydo.com/article/323 
欢迎转载,请注明出处。

Python爬虫学习者需要注意什么?

python爬虫wanbainip 发表了文章 • 0 个评论 • 2374 次浏览 • 2020-10-28 17:14 • 来自相关话题

在这个大数据时代里,学习Python网络爬虫的人越来越多,在学习过程中难免会遇到一些问题,学习者最常见的问题就是遇到ip被限制,因为你在爬虫采集数据过程中,同一个ip频繁的对网站进行访问,会给对方服务器会造成压力, 那么网站就根据ip访问的频率来限制你的ip,这也是反爬虫机制常见的一种判断。


最常见的解决方法就是使用大量的ip,就是借着代理ip保证IP被封时有替换IP可用,永远保持着续航能力。这里推荐51代理ip,作为一家提供代理IP的专业服务商,万变ip代理拥有强大的技术团队运营维护,全高匿系统所产生的高匿ip不仅安全稳定、而且速度快, 以及与爬虫用户多年来合作的宝贵经验,是Python爬虫首选代理IP。

Python是一种全栈计算机程序设计语言,全栈,顾名思义,应用范围广。你可能听说过很多编程语言,例如C语言,Java语言等,众所周知,这些语言都非常难学,更别说景桐使用了。而python不一样,比如完成一个Web服务,C语言要写1000行代码,Java要写100行,而python可能只要写20行。对!这就是差距!目前由于python“简单易懂”,已逐步成为网络爬虫主流语言。

在初学python爬虫时,很多程序员会被一些“小问题”阻碍脚步,为避免大家再次犯同样的错误,加快学习进程,在爬取网站信息时一定要使用大量代理IP。好用的代理IP服务商,

高效率的爬虫工作离不开ip代理的支持,这就是ip代理越来越受欢迎的原因!收藏举报投诉 查看全部
在这个大数据时代里,学习Python网络爬虫的人越来越多,在学习过程中难免会遇到一些问题,学习者最常见的问题就是遇到ip被限制,因为你在爬虫采集数据过程中,同一个ip频繁的对网站进行访问,会给对方服务器会造成压力, 那么网站就根据ip访问的频率来限制你的ip,这也是反爬虫机制常见的一种判断。


最常见的解决方法就是使用大量的ip,就是借着代理ip保证IP被封时有替换IP可用,永远保持着续航能力。这里推荐51代理ip,作为一家提供代理IP的专业服务商,万变ip代理拥有强大的技术团队运营维护,全高匿系统所产生的高匿ip不仅安全稳定、而且速度快, 以及与爬虫用户多年来合作的宝贵经验,是Python爬虫首选代理IP。

Python是一种全栈计算机程序设计语言,全栈,顾名思义,应用范围广。你可能听说过很多编程语言,例如C语言,Java语言等,众所周知,这些语言都非常难学,更别说景桐使用了。而python不一样,比如完成一个Web服务,C语言要写1000行代码,Java要写100行,而python可能只要写20行。对!这就是差距!目前由于python“简单易懂”,已逐步成为网络爬虫主流语言。

在初学python爬虫时,很多程序员会被一些“小问题”阻碍脚步,为避免大家再次犯同样的错误,加快学习进程,在爬取网站信息时一定要使用大量代理IP。好用的代理IP服务商,

高效率的爬虫工作离不开ip代理的支持,这就是ip代理越来越受欢迎的原因!收藏举报投诉

Python爬虫虎牙平台主播的图片代码

python爬虫wanbainip 发表了文章 • 0 个评论 • 2436 次浏览 • 2020-10-27 17:55 • 来自相关话题

今天万变ip就为大家分享一下,Python爬虫是如何爬取虎牙平台的主播图片。这里我们主要爬取虎牙星秀的主播图片,并下载保存到本地。在爬取之前,我们可先打开目标页面,F12审查元素,查找我们需要的信息所在的位置。

代码如下:

import urllib.request

import re

import os

# 全局变量用来记录图片的编号

gl_z = 0

def down_img(url1):

"""下载图片"""

# 处理图片链接,拼接http:

url = "https:" + re.sub(r"\?", "", url1)

global gl_z

print(url)

# 请求链接

response = urllib.request.urlopen(url)

# 读取内容

data = response.read()

# 切片取出图片名称

file_name = url[url.rfind('/') + 1:]

# 生成列表

a = [x for x in range(10000)]

# 打开文件用以写入

file = open(os.path.join("photo3", "img" + file_name + str(a[gl_z]) + ".jpg"), "wb")

file.write(data)

# 关闭文件

file.close()

# 编号加1

gl_z += 1

if __name__ == '__main__':

# 要抓去信息的网址

home = """http://www.huya.com/g/xingxiu"""

# 模拟请求头

headers = {

"Host": "www.huya.com",

"User-Agent": "agent信息"

}

# 构造好请求对象 将请求提交到服务器 获取的响应就是到首页的html代码

request = urllib.request.Request(url=home, headers=headers)

response = urllib.request.urlopen(request)

# 读取抓到的内容并解码

html_data = response.read().decode()

"""huyaimg.msstatic.com/avatar/1054/db/6590aa9bcf98e12e5d809d371e46cc_180_135.jpg

"""

# 使用正则 从首页中 提取出所有的图片链接

img_list = re.findall(r"//huyaimg\.msstatic\.com.+\.jpg\?", html_data)

print(img_list)

# 取出每张图片进行下载

for img_url in img_list:

print(img_url)

down_img(img_url) 查看全部
今天万变ip就为大家分享一下,Python爬虫是如何爬取虎牙平台的主播图片。这里我们主要爬取虎牙星秀的主播图片,并下载保存到本地。在爬取之前,我们可先打开目标页面,F12审查元素,查找我们需要的信息所在的位置。

代码如下:

import urllib.request

import re

import os

# 全局变量用来记录图片的编号

gl_z = 0

def down_img(url1):

"""下载图片"""

# 处理图片链接,拼接http:

url = "https:" + re.sub(r"\?", "", url1)

global gl_z

print(url)

# 请求链接

response = urllib.request.urlopen(url)

# 读取内容

data = response.read()

# 切片取出图片名称

file_name = url[url.rfind('/') + 1:]

# 生成列表

a = [x for x in range(10000)]

# 打开文件用以写入

file = open(os.path.join("photo3", "img" + file_name + str(a[gl_z]) + ".jpg"), "wb")

file.write(data)

# 关闭文件

file.close()

# 编号加1

gl_z += 1

if __name__ == '__main__':

# 要抓去信息的网址

home = """http://www.huya.com/g/xingxiu"""

# 模拟请求头

headers = {

"Host": "www.huya.com",

"User-Agent": "agent信息"

}

# 构造好请求对象 将请求提交到服务器 获取的响应就是到首页的html代码

request = urllib.request.Request(url=home, headers=headers)

response = urllib.request.urlopen(request)

# 读取抓到的内容并解码

html_data = response.read().decode()

"""huyaimg.msstatic.com/avatar/1054/db/6590aa9bcf98e12e5d809d371e46cc_180_135.jpg

"""

# 使用正则 从首页中 提取出所有的图片链接

img_list = re.findall(r"//huyaimg\.msstatic\.com.+\.jpg\?", html_data)

print(img_list)

# 取出每张图片进行下载

for img_url in img_list:

print(img_url)

down_img(img_url)

机器学习:港股首日上市价格预测

深度学习李魔佛 发表了文章 • 0 个评论 • 3637 次浏览 • 2020-09-25 22:57 • 来自相关话题

上一篇《python程序分析港股打新到底赚不赚钱》一文简单的分析了港股打新的盈利预期。 

    因为我们花了不少时间爬取了港股新股的数据,可以对这些数据加以利用,利用机器学习的模型,预测港股上市价格以及影响因素的权重。


    香港股市常年位于全球新股集资三甲之列,每年都有上百只新股上市。与已上市的正股相比,新股的特点是没有任何历史交易数据,这使新股的feature比较朴素,使其可以变成一个较为简单的机器学习问题。





我们在这里,以练手为目的,用新股首日涨跌幅的预测作为例子,介绍一个比较完整的机器学习流程。


数据获取

一个机器学习的项目,最重要的是数据。没有数据,一切再高级的算法都只是纸上谈兵。上一篇文中,已经获取了最近发行的新股的一些基本数据,还有一些详情数据在详细页里面,需要访问详情页获取。





 
比如农夫山泉,除了之前爬取的基本数据,如上市市值,招股价,中签率,超额倍数,现价等,还有一些保荐人,包销商等有价值数据,所以我们也需要顺带把这些数据获取过来。这时需要在上一篇文章的基础上,获取每一个新股的详情页地址,然后如法炮制,用xpath把数据提取出来。





基本数据页和详情页保存为2个csv文件:data/ipo_list.csv和data/ipo_details.csv







数据清理和特征提取

接下来要做的是对数据进行清理,扔掉无关的项目,然后做一些特征提取和特征处理。

爬取的两个数据,我们先用pandas读取进来,用股票代码code做index,然后合并成为一个大的dataframe.#Read two files and merge
df1 = pd.read_csv('../data/ipo_list', sep='\t', index_col='code')
df2 = pd.read_csv('../data/ipo_details', sep= '\t', index_col = 0)
#Use combine_first to avoid duplicate columns
df = df1.combine_first(df2)
我们看看我们的dataframe有哪些column先:df.columns.values

array(['area', 'banks', 'buy_ratio', 'category', 'date', 'draw_prob',
'eipo', 'firstday_performance', 'hk_portion', 'ipo_price',
'ipo_price_range', 'market_type', 'name', 'now_price', 'one_hand',
'predict_profile_market_ratio', 'predict_profit_ratio',
'profit_ratio', 'recommender', 'sales', 'shares_per_hand',
'stock_type', 'total_performance', 'total_value', 'website'], dtype=object)
我们的目标,也就是我们要预测的值,是首日涨跌幅,即firstday_performance. 我们需要扔掉一些无关的项目,比如日期、收票银行、网址、当前的股价等等。还要扔掉那些没有公开发售的全配售的股票,因为这些股票没有任何散户参与,跟我们目标无关。# Drop unrelated columns
to_del = ['date', 'banks', 'eipo', 'name', 'now_price', 'website', 'total_performance','predict_profile_market_ratio', 'predict_profit_ratio', 'profit_ratio']
for item in to_del:
del df[item]

#Drop non_public ipo stocks
df = df[df.draw_prob.notnull()]
对于百分比的数据,我们要换成float的形式:def per2float(x):
if not pd.isnull(x):
x = x.strip('%')
return float(x)/100.
else:
return x

#Format percentage
df['draw_prob'] = df['draw_prob'].apply(per2float)
df['firstday_performance'] = df['firstday_performance'].apply(per2float)
df['hk_portion'] = df['hk_portion'].apply(per2float)


 
对于”认购不足”的情况,我们要把超购数替换成为0:def buy_ratio_process(x):
if x == '认购不足':
return 0.0
else:
return float(x)

#Format buy_ratio
df['buy_ratio'] = df['buy_ratio'].apply(buy_ratio_process)
新股招股的IPO价格是一个区间。有一些新股,招股价上下界拉得很开。因为我们已经有了股价作为另一个,所以我们这里希望能拿到IPO招股价格的上下界范围与招股价相比的一个比例,作为一个新的特征:def get_low_bound(x):
if ',' in str(x):
x = x.replace(',', '')
try:
if pd.isnull(x) or '-' not in x:
return float(x)
else:
x = x.split('-')
return float(x[0])
except Exception as e:
print(e)
print(x)

def get_up_bound(x):
if ',' in str(x):
x = x.replace(',', '')
try:
if pd.isnull(x) or '-' not in x:
return float(x)
else:
x = x.split('-')
return float(x[1])
except Exception as e:
print(e)
print(x)

def get_ipo_range_prop(x):
if pd.isnull(x):
return x
low_bound = get_low_bound(x)
up_bound = get_up_bound(x)
return (up_bound-low_bound)*2/(up_bound+low_bound)

#Merge ipo_price_range to proportion of middle
df['ipo_price_range_ratio'] = df['ipo_price_range'].apply(get_ipo_range_prop)
del df['ipo_price_range']
我们取新股招股价对应总市值的中位数作为另一个特征。因为总市值的绝对值是一个非常大的数字,我们把它按比例缩小,使它的取值和其它特征在一个差不多的范围里。def get_total_value_mid(x):
if pd.isnull(x):
return x
low_bound = get_low_bound(x)
up_bound = get_up_bound(x)
return (up_bound+low_bound)/2

df['total_value_mid'] = df['total_value'].apply(get_total_value_mid)/1000000000.
del df['total_value']
于是我们的数据变成了这样一个278 rows × 15 columns的dataframe,即我们有278个数据点和15个特征:
 





我们看到诸如地区、业务种类等这些特征是categorical的。同时,保荐人和包销商又有多个item的情况。对于这种特征的处理,我们使用one-hot encoding,对每一个种类创建一个新的category,然后用0-1来表示instance是否属于这个category。
 #Now do one-hot encoding for all categorical columns
#One problem is that we have to split('、') first for contents with multiple companies

dftest = df.copy()

def one_hot_encoding(df, column_name):
#Reads a df and target column, does tailored one-hot encoding, and return new df for merge

cat_list = df[column_name].unique().tolist()
cat_set = set()
for items in cat_list:
if pd.isnull(items):
continue
items = items.split('、')
for item in items:
item = item.strip()
cat_set.add(item)
for item in cat_set:
item = column_name + '_' + item
df[item] = 0

def check_onehot(x, cat):
if pd.isnull(x):
return 0
x = x.split('、')
for item in x:
if cat == item.strip():
return 1
return 0

for item in cat_set:
df[column_name + '_' + item] = df[column_name].apply(check_onehot, args=(item, ))

del df[column_name]
return df

dftest = one_hot_encoding(dftest, 'area')
dftest = one_hot_encoding(dftest, 'category')
dftest = one_hot_encoding(dftest, 'market_type')
dftest = one_hot_encoding(dftest, 'recommender')
dftest = one_hot_encoding(dftest, 'sales')
dftest = one_hot_encoding(dftest, 'stock_type')这下我们的数据变成了一个278 rows × 535 columns的dataframe,即我们之前的15个特征因为one-hot encoding,一下子变成了535个特征。这其实是机器学习很常见的一个情况,即我们的数据是一个sparce matrix。





 
训练模型

有了已经整理好特征的数据,我们可以开始建立机器学习模型了。

这里我们用xgboost为例子建立一个非常简单的模型。xgboost是一个基于boosted tree的模型。大家也可以尝试其它更多的算法模型。

我们把数据读入,然后随机把1/3的股票数据分出来做testing data. 我们这里只是一个示例,更高级的方法可以做诸如n-fold validation,以及grid search寻找最优参数等。
 # load data and split feature and label
df = pd.read_csv('../data/hk_ipo_feature_engineered', sep='\t', index_col='code', encoding='utf-8')
Y = df['firstday_performance']
X = df.drop('firstday_performance', axis = 1)
# split data into train and test sets
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)
# fit model no training data
eval_set = [(X_test, y_test)]
因为新股首日涨跌幅是一个float,所以这是一个regression的问题。我们跑xgboost模型,输出mean squared error (越接近0表明准确率越高):
 # fit model no training data
xgb_model = xgb.XGBRegressor().fit(X_train,y_train)
predictions = xgb_model.predict(X_test)
actuals = y_test
print mean_squared_error(actuals, predictions)

0.0643324471123

可见准确率还是蛮高的。 xgboost自带了画出特征重要性的方法xgb.plot_importance。 用来描述每个特征对结果的重要程度。
 importance = xgb_model.booster().get_score(importance_type='weight')
tuples = [(k, importance[k]) for k in importance]然后利用matplotlib绘制图形。 





 
我们看到几个最强的特征,比如超额倍数、在香港发售的比例、ipo的价格和总市值(细价股更容易涨很多)等。

同时我们还发现了几个比较有意思的特征,比如东南亚地区的股票,和某些包销商与保荐人。


模型预测

这里就略过了。大家大可以自己将即将上市的港股新股做和上面一样的特征处理,然后预测出一个首日涨跌幅,待股票上市后做个对比了!

总结

我们用预测港股新股首日涨跌幅的例子,介绍了一个比较简单的机器学习的流程,包括了数据获取、数据清理、特征处理、模型训练和模型预测等。这其中每一个步骤都可以钻研得非常深;这篇文章只是蜻蜓点水,隔靴搔痒。

最重要的是,掌握了机器学习的知识,也许真的能帮助我们解决很多生活中实际的问题。比如,赚点小钱?


由于微信改版后不再是按时间顺序推送文章,如果后续想持续关注笔者的最新观点,请务必将公众号设为星标,并点击右下角的“赞”和“在看”,不然我又懒得更新了哈,还有更多很好玩的数据等着你哦。
 





 
原创文章,转载请注明牛出处
http://30daydo.com/article/608
  查看全部
上一篇《python程序分析港股打新到底赚不赚钱》一文简单的分析了港股打新的盈利预期。 

    因为我们花了不少时间爬取了港股新股的数据,可以对这些数据加以利用,利用机器学习的模型,预测港股上市价格以及影响因素的权重。


    香港股市常年位于全球新股集资三甲之列,每年都有上百只新股上市。与已上市的正股相比,新股的特点是没有任何历史交易数据,这使新股的feature比较朴素,使其可以变成一个较为简单的机器学习问题。

fe4c03249a9aba12936be1a86d0b69da.jpg

我们在这里,以练手为目的,用新股首日涨跌幅的预测作为例子,介绍一个比较完整的机器学习流程。


数据获取

一个机器学习的项目,最重要的是数据。没有数据,一切再高级的算法都只是纸上谈兵。上一篇文中,已经获取了最近发行的新股的一些基本数据,还有一些详情数据在详细页里面,需要访问详情页获取。

chrome_IrnpNEI2Na.png

 
比如农夫山泉,除了之前爬取的基本数据,如上市市值,招股价,中签率,超额倍数,现价等,还有一些保荐人,包销商等有价值数据,所以我们也需要顺带把这些数据获取过来。这时需要在上一篇文章的基础上,获取每一个新股的详情页地址,然后如法炮制,用xpath把数据提取出来。





基本数据页和详情页保存为2个csv文件:data/ipo_list.csv和data/ipo_details.csv







数据清理和特征提取

接下来要做的是对数据进行清理,扔掉无关的项目,然后做一些特征提取和特征处理。

爬取的两个数据,我们先用pandas读取进来,用股票代码code做index,然后合并成为一个大的dataframe.
#Read two files and merge
df1 = pd.read_csv('../data/ipo_list', sep='\t', index_col='code')
df2 = pd.read_csv('../data/ipo_details', sep= '\t', index_col = 0)
#Use combine_first to avoid duplicate columns
df = df1.combine_first(df2)

我们看看我们的dataframe有哪些column先:
df.columns.values

array(['area', 'banks', 'buy_ratio', 'category', 'date', 'draw_prob',
'eipo', 'firstday_performance', 'hk_portion', 'ipo_price',
'ipo_price_range', 'market_type', 'name', 'now_price', 'one_hand',
'predict_profile_market_ratio', 'predict_profit_ratio',
'profit_ratio', 'recommender', 'sales', 'shares_per_hand',
'stock_type', 'total_performance', 'total_value', 'website'], dtype=object)

我们的目标,也就是我们要预测的值,是首日涨跌幅,即firstday_performance. 我们需要扔掉一些无关的项目,比如日期、收票银行、网址、当前的股价等等。还要扔掉那些没有公开发售的全配售的股票,因为这些股票没有任何散户参与,跟我们目标无关。
# Drop unrelated columns
to_del = ['date', 'banks', 'eipo', 'name', 'now_price', 'website', 'total_performance','predict_profile_market_ratio', 'predict_profit_ratio', 'profit_ratio']
for item in to_del:
del df[item]

#Drop non_public ipo stocks
df = df[df.draw_prob.notnull()]

对于百分比的数据,我们要换成float的形式:
def per2float(x):
if not pd.isnull(x):
x = x.strip('%')
return float(x)/100.
else:
return x

#Format percentage
df['draw_prob'] = df['draw_prob'].apply(per2float)
df['firstday_performance'] = df['firstday_performance'].apply(per2float)
df['hk_portion'] = df['hk_portion'].apply(per2float)


 
对于”认购不足”的情况,我们要把超购数替换成为0:
def buy_ratio_process(x):
if x == '认购不足':
return 0.0
else:
return float(x)

#Format buy_ratio
df['buy_ratio'] = df['buy_ratio'].apply(buy_ratio_process)

新股招股的IPO价格是一个区间。有一些新股,招股价上下界拉得很开。因为我们已经有了股价作为另一个,所以我们这里希望能拿到IPO招股价格的上下界范围与招股价相比的一个比例,作为一个新的特征:
def get_low_bound(x):
if ',' in str(x):
x = x.replace(',', '')
try:
if pd.isnull(x) or '-' not in x:
return float(x)
else:
x = x.split('-')
return float(x[0])
except Exception as e:
print(e)
print(x)

def get_up_bound(x):
if ',' in str(x):
x = x.replace(',', '')
try:
if pd.isnull(x) or '-' not in x:
return float(x)
else:
x = x.split('-')
return float(x[1])
except Exception as e:
print(e)
print(x)

def get_ipo_range_prop(x):
if pd.isnull(x):
return x
low_bound = get_low_bound(x)
up_bound = get_up_bound(x)
return (up_bound-low_bound)*2/(up_bound+low_bound)

#Merge ipo_price_range to proportion of middle
df['ipo_price_range_ratio'] = df['ipo_price_range'].apply(get_ipo_range_prop)
del df['ipo_price_range']

我们取新股招股价对应总市值的中位数作为另一个特征。因为总市值的绝对值是一个非常大的数字,我们把它按比例缩小,使它的取值和其它特征在一个差不多的范围里。
def get_total_value_mid(x):
if pd.isnull(x):
return x
low_bound = get_low_bound(x)
up_bound = get_up_bound(x)
return (up_bound+low_bound)/2

df['total_value_mid'] = df['total_value'].apply(get_total_value_mid)/1000000000.
del df['total_value']

于是我们的数据变成了这样一个278 rows × 15 columns的dataframe,即我们有278个数据点和15个特征:
 

2.jpg

我们看到诸如地区、业务种类等这些特征是categorical的。同时,保荐人和包销商又有多个item的情况。对于这种特征的处理,我们使用one-hot encoding,对每一个种类创建一个新的category,然后用0-1来表示instance是否属于这个category。
 
#Now do one-hot encoding for all categorical columns
#One problem is that we have to split('、') first for contents with multiple companies

dftest = df.copy()

def one_hot_encoding(df, column_name):
#Reads a df and target column, does tailored one-hot encoding, and return new df for merge

cat_list = df[column_name].unique().tolist()
cat_set = set()
for items in cat_list:
if pd.isnull(items):
continue
items = items.split('、')
for item in items:
item = item.strip()
cat_set.add(item)
for item in cat_set:
item = column_name + '_' + item
df[item] = 0

def check_onehot(x, cat):
if pd.isnull(x):
return 0
x = x.split('、')
for item in x:
if cat == item.strip():
return 1
return 0

for item in cat_set:
df[column_name + '_' + item] = df[column_name].apply(check_onehot, args=(item, ))

del df[column_name]
return df

dftest = one_hot_encoding(dftest, 'area')
dftest = one_hot_encoding(dftest, 'category')
dftest = one_hot_encoding(dftest, 'market_type')
dftest = one_hot_encoding(dftest, 'recommender')
dftest = one_hot_encoding(dftest, 'sales')
dftest = one_hot_encoding(dftest, 'stock_type')
这下我们的数据变成了一个278 rows × 535 columns的dataframe,即我们之前的15个特征因为one-hot encoding,一下子变成了535个特征。这其实是机器学习很常见的一个情况,即我们的数据是一个sparce matrix。

3.jpg

 
训练模型

有了已经整理好特征的数据,我们可以开始建立机器学习模型了。

这里我们用xgboost为例子建立一个非常简单的模型。xgboost是一个基于boosted tree的模型。大家也可以尝试其它更多的算法模型。

我们把数据读入,然后随机把1/3的股票数据分出来做testing data. 我们这里只是一个示例,更高级的方法可以做诸如n-fold validation,以及grid search寻找最优参数等。
 
# load data and split feature and label
df = pd.read_csv('../data/hk_ipo_feature_engineered', sep='\t', index_col='code', encoding='utf-8')
Y = df['firstday_performance']
X = df.drop('firstday_performance', axis = 1)
# split data into train and test sets
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)
# fit model no training data
eval_set = [(X_test, y_test)]

因为新股首日涨跌幅是一个float,所以这是一个regression的问题。我们跑xgboost模型,输出mean squared error (越接近0表明准确率越高):
 
# fit model no training data
xgb_model = xgb.XGBRegressor().fit(X_train,y_train)
predictions = xgb_model.predict(X_test)
actuals = y_test
print mean_squared_error(actuals, predictions)

0.0643324471123

可见准确率还是蛮高的。 xgboost自带了画出特征重要性的方法xgb.plot_importance。 用来描述每个特征对结果的重要程度。
 
importance = xgb_model.booster().get_score(importance_type='weight')
tuples = [(k, importance[k]) for k in importance]
然后利用matplotlib绘制图形。 

4.png

 
我们看到几个最强的特征,比如超额倍数、在香港发售的比例、ipo的价格和总市值(细价股更容易涨很多)等。

同时我们还发现了几个比较有意思的特征,比如东南亚地区的股票,和某些包销商与保荐人。


模型预测

这里就略过了。大家大可以自己将即将上市的港股新股做和上面一样的特征处理,然后预测出一个首日涨跌幅,待股票上市后做个对比了!

总结

我们用预测港股新股首日涨跌幅的例子,介绍了一个比较简单的机器学习的流程,包括了数据获取、数据清理、特征处理、模型训练和模型预测等。这其中每一个步骤都可以钻研得非常深;这篇文章只是蜻蜓点水,隔靴搔痒。

最重要的是,掌握了机器学习的知识,也许真的能帮助我们解决很多生活中实际的问题。比如,赚点小钱?


由于微信改版后不再是按时间顺序推送文章,如果后续想持续关注笔者的最新观点,请务必将公众号设为星标,并点击右下角的“赞”和“在看”,不然我又懒得更新了哈,还有更多很好玩的数据等着你哦。
 

微信图片_20200923020240.jpg

 
原创文章,转载请注明牛出处
http://30daydo.com/article/608
 

深圳住房公积金验证码 识别破解

python爬虫李魔佛 发表了文章 • 0 个评论 • 2651 次浏览 • 2020-06-26 14:34 • 来自相关话题

 
http://gjj.sz.gov.cn/fzgn/zfcq/index.html
 
比较常规的验证码,使用keras全连接层,cv切割后每个字符只需要20个样本就达到准确率99%。
需要模型或者代码的私聊。 查看全部
1212.PNG

 
http://gjj.sz.gov.cn/fzgn/zfcq/index.html
 
比较常规的验证码,使用keras全连接层,cv切割后每个字符只需要20个样本就达到准确率99%。
需要模型或者代码的私聊。

多线程调用yolo模型会出错(包括使用cv2载入yolo模型)

深度学习李魔佛 发表了文章 • 3 个评论 • 4671 次浏览 • 2019-12-05 10:27 • 来自相关话题

占坑。
 
多线程调用yolo模型会出错(包括使用cv2载入yolo模型)
 
占坑。
 
多线程调用yolo模型会出错(包括使用cv2载入yolo模型)
 

keras yolo物体检测 入门教程

深度学习李魔佛 发表了文章 • 0 个评论 • 3040 次浏览 • 2019-11-28 16:03 • 来自相关话题

占坑
占坑

RuntimeError: `get_session` is not available when using TensorFlow 2.0.

深度学习李魔佛 发表了文章 • 0 个评论 • 12995 次浏览 • 2019-11-28 15:10 • 来自相关话题

这个问题是TensorFlow版本问题,在2.0以上get_session是被移除了。需要做一些修改,或者把tf降级。可以安装1.15版本
pip install tensorflow==1.15 --upgradeHere, we will see how we can upgrade our code to work with tensorflow 2.0.

This error is usually faced when we are loading pre-trained model with tensorflow session/graph or we are building flask api over a pre-trained model and loading model in tensorflow graph to avoid collision of sessions while application is getting multiple requests at once or say in case of multi-threadinng

After tensorflow 2.0 upgrade, i also started facing above error in one of my project when i had built api of pre-trained model with flask. So i looked around in tensorflow 2.0 documents to find a workaround, to avoid this runtime error and upgrade my code to work with tensorflow 2.0 as well rather than downgrading it to tensorflow 1.x .

I had a project on which i had written tutorial as well on how to build Flask api on trained keras model of text classification and then use it in production

But this project was not working after tensorflow upgrade and was facing runtime error.

Stacktrace of error was something like below:

File "/Users/Upasana/Documents/playground/deploy-keras-model-in-production/src/main.py", line 37, in model_predict
with backend.get_session().graph.as_default() as g:
File "/Users/Upasana/Documents/playground/deploy-keras-model-in-production/venv-tf2/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 379, in get_session
'`get_session` is not available '
RuntimeError: `get_session` is not available when using TensorFlow 2.0.
Related code to get model
with backend.get_session().graph.as_default() as g:
model = SentimentService.get_model1()
Related code to load model
def load_deep_model(self, model):
json_file = open('./src/mood-saved-models/' + model + '.json', 'r')
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json)

loaded_model.load_weights("./src/mood-saved-models/" + model + ".h5")

loaded_model._make_predict_function()
return loaded_model
get_session is removed in tensorflow 2.0 and hence not available.

so, in order to load saved model we switched methods. Rather than using keras’s load_model, we used tensorflow to load model so that we can load model using distribution strategy.

Note
The tf.distribute.Strategy API provides an abstraction for distributing your training across multiple processing units.

New code to get model
another_strategy = tf.distribute.MirroredStrategy()
with another_strategy.scope():
model = SentimentService.get_model1()
New code to load model
def load_deep_model(self, model):
loaded_model = tf.keras.models.load_model("./src/mood-saved-models/"model + ".h5")
return loaded_model
This worked and solved the problem with runtime error of get_session not available in tensorflow 2.0 . You can refer to Tensorflow 2.0 upgraded article too

Hope, this will solve your problem too. Thanks for following this article. 查看全部
这个问题是TensorFlow版本问题,在2.0以上get_session是被移除了。需要做一些修改,或者把tf降级。可以安装1.15版本
pip install tensorflow==1.15 --upgrade
Here, we will see how we can upgrade our code to work with tensorflow 2.0.

This error is usually faced when we are loading pre-trained model with tensorflow session/graph or we are building flask api over a pre-trained model and loading model in tensorflow graph to avoid collision of sessions while application is getting multiple requests at once or say in case of multi-threadinng

After tensorflow 2.0 upgrade, i also started facing above error in one of my project when i had built api of pre-trained model with flask. So i looked around in tensorflow 2.0 documents to find a workaround, to avoid this runtime error and upgrade my code to work with tensorflow 2.0 as well rather than downgrading it to tensorflow 1.x .

I had a project on which i had written tutorial as well on how to build Flask api on trained keras model of text classification and then use it in production

But this project was not working after tensorflow upgrade and was facing runtime error.

Stacktrace of error was something like below:

File "/Users/Upasana/Documents/playground/deploy-keras-model-in-production/src/main.py", line 37, in model_predict
with backend.get_session().graph.as_default() as g:
File "/Users/Upasana/Documents/playground/deploy-keras-model-in-production/venv-tf2/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 379, in get_session
'`get_session` is not available '
RuntimeError: `get_session` is not available when using TensorFlow 2.0.
Related code to get model
with backend.get_session().graph.as_default() as g:
model = SentimentService.get_model1()
Related code to load model
def load_deep_model(self, model):
json_file = open('./src/mood-saved-models/' + model + '.json', 'r')
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json)

loaded_model.load_weights("./src/mood-saved-models/" + model + ".h5")

loaded_model._make_predict_function()
return loaded_model
get_session is removed in tensorflow 2.0 and hence not available.

so, in order to load saved model we switched methods. Rather than using keras’s load_model, we used tensorflow to load model so that we can load model using distribution strategy.

Note
The tf.distribute.Strategy API provides an abstraction for distributing your training across multiple processing units.

New code to get model
another_strategy = tf.distribute.MirroredStrategy()
with another_strategy.scope():
model = SentimentService.get_model1()
New code to load model
def load_deep_model(self, model):
loaded_model = tf.keras.models.load_model("./src/mood-saved-models/"model + ".h5")
return loaded_model
This worked and solved the problem with runtime error of get_session not available in tensorflow 2.0 . You can refer to Tensorflow 2.0 upgraded article too

Hope, this will solve your problem too. Thanks for following this article.

sklearn中的Bunch数据类型

深度学习李魔佛 发表了文章 • 0 个评论 • 15635 次浏览 • 2018-06-07 19:10 • 来自相关话题

在sklearn中自带部分数据 如 datasets 包中




 
那么这个
<class 'sklearn.utils.Bunch'>
是什么数据格式 ?
 
打印一下:




好吧,原来就是一个字典结构。可以像调用字典一样使用Bunch。
比如 data['image'] 就获取 key为image的内容。
 

原创地址:http://30daydo.com/article/325 
欢迎转载,请注明出处。 查看全部
在sklearn中自带部分数据 如 datasets 包中
k1.PNG

 
那么这个
<class 'sklearn.utils.Bunch'>
是什么数据格式 ?
 
打印一下:
k3.PNG

好吧,原来就是一个字典结构。可以像调用字典一样使用Bunch。
比如 data['image'] 就获取 key为image的内容。
 

原创地址:http://30daydo.com/article/325 
欢迎转载,请注明出处。

sklearn中SGDClassifier分类器每次得到的结果都不一样?

深度学习李魔佛 发表了文章 • 0 个评论 • 7331 次浏览 • 2018-06-07 17:14 • 来自相关话题

如下代码:sgdc = SGDClassifier()
sgdc.fit(X_train,y_train)
sgdc_predict_y = sgdc.predict(X_test)
print 'Accuary of SGD classifier ', sgdc.score(X_test,y_test)
print classification_report(y_test,sgdc_predict_y,target_names=['Benign','Malignant'])
每次输出的结果都不一样? WHY
 
 
因为你使用了一个默认参数:
SGDClassifier(random_state = None)
 
所以这个随机种子每次不一样,所以得到的结果就可能不一样,如果你指定随机种子值,那么每次得到的结果都是一样的了。
 

原创地址:http://30daydo.com/article/323 
欢迎转载,请注明出处。 查看全部
如下代码:
sgdc = SGDClassifier()
sgdc.fit(X_train,y_train)
sgdc_predict_y = sgdc.predict(X_test)
print 'Accuary of SGD classifier ', sgdc.score(X_test,y_test)
print classification_report(y_test,sgdc_predict_y,target_names=['Benign','Malignant'])

每次输出的结果都不一样? WHY
 
 
因为你使用了一个默认参数:
SGDClassifier(random_state = None)
 
所以这个随机种子每次不一样,所以得到的结果就可能不一样,如果你指定随机种子值,那么每次得到的结果都是一样的了。
 

原创地址:http://30daydo.com/article/323 
欢迎转载,请注明出处。