Dive Into Data Science by Bradford Tuckfield

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



Copyright Disclaimer:
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.