Dive Into Data Science by Bradford Tuckfield
Author:Bradford Tuckfield [Tuckfield, Bradford]
Language: eng
Format: epub, mobi
ISBN: 9781718502895
Published: 2023-04-03T00:00:00+00:00
We can do a more rigorous test for this (lack of a) linear relationship by performing a linear regression, just as we did in Chapters 2 and 5:
from sklearn.linear_model import LinearRegression x = news[' global_sentiment_polarity'].values.reshape(-1,1) y = news[' shares'].values.reshape(-1,1) regressor = LinearRegression() regressor.fit(x, y) print(regressor.coef_) print(regressor.intercept_)
This snippet performs a linear regression predicting shares using sentiment polarity. It does so in the same way we outlined in Chapter 2. We start by importing from the module sklearn.linear_model, which contains the LinearRegression() function we want to use. Then, we reshape the data so that the module weâre importing can work with it. We create a variable called regressor, and we fit the regressor to our data. Finally, we print out the coefficient and intercept obtained by fitting the regression: 499.3 and 3,335.8.
Youâll remember from Chapter 2 that we can interpret these numbers as the slope and intercept of the regression line, respectively. In other words, our linear regression estimates the relationship between sentiment and shares as follows:
shares = 3335.8 + 499.3 · sentiment
We can plot this regression line together with our data as follows:
regline=regressor.predict(x) plt.scatter(news[' global_sentiment_polarity'],news[' shares'],color='blue') plt.plot(sorted(news[' global_sentiment_polarity'].tolist()),regline,'r') plt.title('Shares by Sentiment') plt.xlabel('Sentiment') plt.ylabel('Shares') plt.show()
The output should look like Figure 6-2.
Figure 6-2: A regression line showing the estimated relationship between sentiment and shares
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
International Integration of the Brazilian Economy by Elias C. Grivoyannis(71205)
The Radium Girls by Kate Moore(11582)
Turbulence by E. J. Noyes(7672)
Nudge - Improving Decisions about Health, Wealth, and Happiness by Thaler Sunstein(7206)
The Black Swan by Nassim Nicholas Taleb(6731)
Rich Dad Poor Dad by Robert T. Kiyosaki(6129)
Pioneering Portfolio Management by David F. Swensen(6048)
Man-made Catastrophes and Risk Information Concealment by Dmitry Chernov & Didier Sornette(5611)
Zero to One by Peter Thiel(5457)
Secrecy World by Jake Bernstein(4354)
Millionaire: The Philanderer, Gambler, and Duelist Who Invented Modern Finance by Janet Gleeson(4059)
The Age of Surveillance Capitalism by Shoshana Zuboff(3964)
Skin in the Game by Nassim Nicholas Taleb(3950)
The Money Culture by Michael Lewis(3805)
Bullshit Jobs by David Graeber(3797)
Skin in the Game: Hidden Asymmetries in Daily Life by Nassim Nicholas Taleb(3707)
The Dhandho Investor by Mohnish Pabrai(3542)
The Wisdom of Finance by Mihir Desai(3503)
Blockchain Basics by Daniel Drescher(3306)
