Q:有一个疑问,既然模型本身不是线性的,为什么必须先用线性模型制作呢?
A:有核函数和hxsdfj展开等,可以无限近似地变换为线性吗?
小知识:
1.Sigmoid函数,即f(x )=1/) ) 1e-x )。 神经元的非线性作用函数。 广泛应用于神经网络。 也称为逻辑函数。
机器学习中重要的预测模型的逻辑回归(LR )是基于Sigmoid函数实现的。 LR模型的主要任务是给出几个历史{X,Y},其中x是样本的n个特征值,y的取值{ 0,1 }表示正例和负例,通过学习这些历史样本可以获得数学模型,并给出新的LR模型是针对x预测其发生或不发生的二分类模型。 但是,实际上,当一个事件发生时,多数情况下无法得到100%的预测,因此LR可以得到一个事件发生的可能性,超过50%则认为事件发生了,低于50%则事件不发生
参考: https://blog.csdn.net/su _ mo/article/details/79281623
3358 www.mami code.com/info-detail-2315826.html
33559 www.cn blogs.com/xitingxie/p/9924523.html
2.RBF-径向基核函数(Radial Basis Function ) ) )。
Radical: adj .放射状的; 放射状的
Gaussian函数还有另一个名字: ——径向基函数。
是径向对称的标量函数。 通常,它被定义为空间中任意点x到某个中心xc之间的镜头距离的单调函数,可以表示为k(|x-xc|| ),其作用往往是局部的。 也就是说,当x远离xc时,函数值变小。
参考: https://www.cn blogs.com/hx syl/p/5231389.html
一、什么是核函数?
举一个核函数将低维空间映射到高维空间的例子。 下图位于第一、二象限内。 我们关注红色的门和“北京四合院”一词下的紫色文字。 红门上的点视为“”数据,紫色文字上的点视为“-”数据,它们的横、纵坐标是两个特征。 很明显,在这个二维空间中,“”和“-”这两种数据不是线性的。
因为我以SVM的应用为主,对其理论不太了解,所以就不再赘述。
使用SVM的很多人不知道这个条件,也不关心; 不满足该条件的某些函数被用作核函数。
作者:王珻Maigo
链接: https://www.zhi Hu.com/question/24627666/answer/28440943
来源:知乎
版权归作者所有。 商业转载请联系作者取得许可。 非商业转载请注明出处。
参考: https://blog.csdn.net/robin _ Xu _ Shuai/article/details/76946333
二.相关实践
1 .利用SVR (支持向量回归机)的RBF (稳健白云核函数)拟合预测股票
(一)成果
拟合效果
)2)实际动向
(3)代码
import os
导入编号为NP
import pandas as pd
来自脚本导入状态
import matplotlib.pyplot as plt
来自日期导入日期
来自sk learn import preprocessing
from sklearn.svm import SVC,SVR
import plotly.offline as of
import plotly.graph_objs as go
import tushare as ts
# pip install ciso8601
# pip安装堆栈
defget_stock_data(stock_num,start ) :
“”'
下载数据
股票数据的特征
日期:日期
开放:开放价格
high :最高价格
关闭:收盘价
低:最低价格
volume :成交量
price_change :价格变动
p_change :涨幅
ma5:5日均价
ma10:10日均价
ma20:20日均价
v_ma5:5日均量
v_ma10:10日均量
/p>v_ma20:20日均量
:param stock_num:
:return:df
"""
df = ts.get_hist_data(stock_num, start=start, ktype='D')
return df
def draw_kchart(df, filename):
"""
画k线图
"""
Min_date = df.index.min()
Max_date = df.index.max()
print("First date is", Min_date)
print("Last date is", Max_date)
interval_date = dt.strptime(Max_date, "%Y-%m-%d") - dt.strptime(Min_date, "%Y-%m-%d")
print(interval_date)
trace = go.Ohlc(x=df.index, open=df['open'], high=df['high'], low=df['low'], close=df['close'])
data = [trace]
of.plot(data, filename=filename)
def stock_etl(df):
df.dropna(axis=0, inplace=True)
# print(df.isna().sum())
df.sort_values(by=['date'], inplace=True, ascending=True)
return df
def get_data(df):
data = df.copy()
# 年,月,天
# data['date'] = data.index.str.split('-').str[2]
# data['date'] = data.index.str.replace('-','')
# print(data.index.tolist())
data['date'] = [(dt.strptime(x, '%Y-%m-%d') - dt.strptime('2019-01-01', '%Y-%m-%d')).days for x in data.index.tolist()]
data['date'] = pd.to_numeric(data['date'])
return [data['date'].tolist(), data['close'].tolist()]
def predict_prices(dates, prices, x):
dates = np.reshape(dates, (len(dates), 1))
x = np.reshape(x, (len(x), 1))
svr_lin = SVR(kernel='linear', C=1e3,gamma=0.1, verbose=True, cache_size=1000)
svr_poly = SVR(kernel='poly', C=1e3, degree=2, gamma=0.1, verbose=True, cache_size=1000)
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1, verbose=True, cache_size=1000)
plt.scatter(dates, prices, c='k', label='Data')
# 训练
# svr_lin.fit(dates, prices)
# print(svr_lin)
# print(svr_lin.predict(x)[0])
# plt.plot(dates, svr_lin.predict(dates), c='g', label='svr_lin')
# svr_poly.fit(dates, prices)
# print(svr_poly)
# print(svr_poly.predict(x)[0])
# plt.plot(dates, svr_lin.predict(dates), c='g', label='svr_lin')
svr_rbf.fit(dates, prices)
print(svr_rbf)
print(svr_rbf.predict(x)[0])
plt.plot(dates, svr_rbf.predict(dates), c='b', label='svr_rbf')
plt.xlabel('date')
plt.ylabel('Price')
plt.grid(True)
plt.legend()
plt.show()
# return svr_lin.predict(x)[0], svr_poly.predict(x)[0], svr_rbf.predict(x)[0]
if __name__ == "__main__":
"""
预测股价和时间之间的关系
"""
# sh 获取上证指数k线数据
# sz 获取深圳成指k线数据
# cyb 获取创业板指数k线数据
df = get_stock_data('sh', '2019-01-01')
# + 张家港行
# df = get_stock_data('002839', '2019-01-01')
df = stock_etl(df)
curPath = os.path.abspath(os.path.dirname(__file__))
draw_kchart(df, curPath + '/simple_ohlc.html')
dates, prices = get_data(df)
print(dates)
print(prices)
# print(predict_prices(dates, prices, [31]))
# print(predict_prices(dates, prices, ['20190731']))
a = dt.strptime('2019-07-31', '%Y-%m-%d')
b = dt.strptime('2019-01-01', '%Y-%m-%d')
c = (a - b).days
predict_prices(dates, prices, )
参考:http://www.pythonheidong.com/blog/article/53122/
2.其他
http://www.sohu.com/a/123306028_505915