美文网首页
股价预测

股价预测

作者: ForgetThatNight | 来源:发表于2018-07-07 11:07 被阅读11次
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression

data = pd.read_csv('Combined_News_DJIA.csv') 
data.head()

输出 : 5 rows × 27 columns


train = data[data['Date'] < '2015-01-01']
test = data[data['Date'] > '2014-12-31']

example = train.iloc[3,10]
print(example)

输出 : b"The commander of a Navy air reconnaissance squadron that provides the President and the defense secretary the airborne ability to command the nation's nuclear weapons has been relieved of duty"

example2 = example.lower()
print(example2)

输出 :
b"the commander of a navy air reconnaissance squadron that provides the president and the defense secretary the airborne ability to command the nation's nuclear weapons has been relieved of duty"

example3 = CountVectorizer().build_tokenizer()(example2)
print(example3)

输出 :
['the', 'commander', 'of', 'navy', 'air', 'reconnaissance', 'squadron', 'that', 'provides', 'the', 'president', 'and', 'the', 'defense', 'secretary', 'the', 'airborne', 'ability', 'to', 'command', 'the', 'nation', 'nuclear', 'weapons', 'has', 'been', 'relieved', 'of', 'duty']

pd.DataFrame([[x,example3.count(x)] for x in set(example3)], columns = ['Word', 'Count'])
trainheadlines = []
for row in range(0,len(train.index)):
    trainheadlines.append(' '.join(str(x) for x in train.iloc[row,2:27]))
basicvectorizer = CountVectorizer()
basictrain = basicvectorizer.fit_transform(trainheadlines)
print(basictrain.shape)

输出 : (1611, 31675)

basicmodel = LogisticRegression()
basicmodel = basicmodel.fit(basictrain, train["Label"])
testheadlines = []
for row in range(0,len(test.index)):
    testheadlines.append(' '.join(str(x) for x in test.iloc[row,2:27]))
basictest = basicvectorizer.transform(testheadlines)
predictions = basicmodel.predict(basictest)
pd.crosstab(test["Label"], predictions, rownames=["Actual"], colnames=["Predicted"])
#0.42
basicwords = basicvectorizer.get_feature_names()
basiccoeffs = basicmodel.coef_.tolist()[0]
coeffdf = pd.DataFrame({'Word' : basicwords, 
                        'Coefficient' : basiccoeffs})
coeffdf = coeffdf.sort_values(['Coefficient', 'Word'], ascending=[0, 1])
coeffdf.head(10)
coeffdf.tail(10)
advancedvectorizer = CountVectorizer(ngram_range=(2,2))
advancedtrain = advancedvectorizer.fit_transform(trainheadlines)

print(advancedtrain.shape)

输出 : (1611, 366721)

advancedmodel = LogisticRegression()
advancedmodel = advancedmodel.fit(advancedtrain, train["Label"])
testheadlines = []
for row in range(0,len(test.index)):
    testheadlines.append(' '.join(str(x) for x in test.iloc[row,2:27]))
advancedtest = advancedvectorizer.transform(testheadlines)
advpredictions = advancedmodel.predict(advancedtest)
pd.crosstab(test["Label"], advpredictions, rownames=["Actual"], colnames=["Predicted"])
#.57
advwords = advancedvectorizer.get_feature_names()
advcoeffs = advancedmodel.coef_.tolist()[0]
advcoeffdf = pd.DataFrame({'Words' : advwords, 
                        'Coefficient' : advcoeffs})
advcoeffdf = advcoeffdf.sort_values(['Coefficient', 'Words'], ascending=[0, 1])
advcoeffdf.head(10)
advcoeffdf.tail(10)
gensim

相关文章

  • 《自由之路》预测股价

    能预测股价吗,这个问题应该换成“可以预测短期的股价吗”和“可以预测长期的股价吗”,对于前者来说,答案是否定的,对于...

  • 股价预测

    输出 : 5 rows × 27 columns 输出 : b"The commander of a Navy a...

  • 选股的标准:好生意,好公司,好价格

    今天参加公司内部的培训,这个培训师当着那么多人的面居然公开预测股价,他预测五粮液的股价走势,预测张江高科的股价走势...

  • 股价不可预测

    股价不可预测 股价是否可以预测?研究股价的历史变化,是否有助于推测其未来变化?这是耐人寻味的问题。 1.首先要明确...

  • 股价预测软件有用么?

    薛老师说资本的现值等于资本预期收入的折现和。基于此论,股价预测软件有用么?应该是没有。股价预测软件的原理是根据股价...

  • 25|预测股价

    ——李笑来“财富自由”之25《你能准确预测股价吗?》摘要 问题的质量决定答案的质量。比如这个问题: ...

  • 如何在二级市场赚钱?

    有人能预测股价?别做梦了,股价当然是不可以准确预测的了,要不然…….在要弄明白这个问题之前,首先来看:如果...

  • 你想预测股价吗?——读李笑来《通往财富自由之路》(30)

    看了标题,许多人可能会说,废话,谁不想多挣钱。是的,能够预测股价,并且能够准确的预测股价,是每个股民所期望的。 然...

  • 巴菲特怎么投资,巴菲特投资理念二

    股价波动是根本无法预测的,其实他们说的是短期波动。长期波动是完全可以预测的,那就是最终会回归于价值。 不要关心股价...

  • 不要预测短期股价

    我们经常在影视作品中看到有一些所谓的预言家、神算子等等一类人物,它们具有能看到未来的神通,可以预测到未来,他们很受...

网友评论

      本文标题:股价预测

      本文链接:https://www.haomeiwen.com/subject/vbziuftx.html