獨家 | 基于新聞標題的股價走勢分析(附鏈接)

作者: Ronil Patil
翻譯:王闖 (Chuck)
校對:詹好
標簽:自然語言處理、情感分析、股價預測
“不要在草堆里找一根藏針,而是要買下整個草堆!”
數(shù)據集介紹
標簽為1–股價上漲。
標簽為0–股價持平或下跌。
開始
首先引入相關庫
import pandas as pdfrom sklearn.feature_extraction.text import CountVectorizerfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.metrics import classification_report,confusion_matrix,accuracy_score
讀取數(shù)據集
df = pd.read_csv('F:Stock-Sentiment-Analysis-master/Stock News Dataset.csv', encoding = "ISO-8859-1")
將數(shù)據集劃分為訓練集和測試集
train = df[df['Date'] < '20150101']test = df[df['Date'] > '20141231']
特征工程
# Removing special charactersdata=train.iloc[:,2:27]data.replace("[^a-zA-Z]"," ",regex=True, inplace=True)# Renaming column names for better understanding and ease of accesslist1= [i for i in range(25)]new_Index=[str(i) for i in list1]data.columns= new_Indexdata.head(5)

# Convertng headlines to lower casefor index in new_Index:data[index] = data[index].str.lower()data.head(1)
根據索引來合并所有新聞標題:
headlines = []for row in range(0,len(data.index)):headlines.append(' '.join(str(x) for x in data.iloc[row,0:25]))

應用CountVectorizer和RandomForestClassifier方法
## implement BAG OF WORDScountvector=CountVectorizer(ngram_range=(2,2))traindataset=countvector.fit_transform(headlines)## implement RandomForest Classifier
randomclassifier=RandomForestClassifier(n_estimators=200,criterion='entropy')randomclassifier.fit(traindataset,train['Label'])
在測試集上進行預測
## Predict for the Test Datasettest_transform= []for row in range(0,len(test.index)):test_transform.append(' '.join(str(x) for x in test.iloc[row,2:27]))test_dataset = countvector.transform(test_transform)predictions = randomclassifier.predict(test_dataset)
最后,檢查準確性
matrix = confusion_matrix(test['Label'],predictions)print(matrix)score = accuracy_score(test['Label'],predictions)print(score)report = classification_report(test['Label'],predictions)print(report)

關于作者
Ronil Patil
https://www.linkedin.com/in/ronil08/
原文標題:
Stock Price Movement Based On News Headline
原文鏈接:
https://www.analyticsvidhya.com/blog/2021/05/stock-price-movement-based-on-news-headline
編輯:王菁
校對:林亦霖
譯者簡介
王闖(Chuck),臺灣清華大學資訊工程碩士。曾任奧浦諾管理咨詢公司數(shù)據分析主管,現(xiàn)任尼爾森市場研究公司數(shù)據科學經理。很榮幸有機會通過數(shù)據派THU微信公眾平臺和各位老師、同學以及同行前輩們交流學習。
翻譯組招募信息
工作內容:需要一顆細致的心,將選取好的外文文章翻譯成流暢的中文。如果你是數(shù)據科學/統(tǒng)計學/計算機類的留學生,或在海外從事相關工作,或對自己外語水平有信心的朋友歡迎加入翻譯小組。
你能得到:定期的翻譯培訓提高志愿者的翻譯水平,提高對于數(shù)據科學前沿的認知,海外的朋友可以和國內技術應用發(fā)展保持聯(lián)系,THU數(shù)據派產學研的背景為志愿者帶來好的發(fā)展機遇。
其他福利:來自于名企的數(shù)據科學工作者,北大清華以及海外等名校學生他們都將成為你在翻譯小組的伙伴。
點擊文末“閱讀原文”加入數(shù)據派團隊~
轉載須知
如需轉載,請在開篇顯著位置注明作者和出處(轉自:數(shù)據派ID:DatapiTHU),并在文章結尾放置數(shù)據派醒目二維碼。有原創(chuàng)標識文章,請發(fā)送【文章名稱-待授權公眾號名稱及ID】至聯(lián)系郵箱,申請白名單授權并按要求編輯。
發(fā)布后請將鏈接反饋至聯(lián)系郵箱(見下方)。未經許可的轉載以及改編者,我們將依法追究其法律責任。
點擊“閱讀原文”擁抱組織

