Stocktwits — The Twitter of stocks, offers a free API to access trending stocks on its site. This list of trending stock tickers can be iterated over to scrape any number of useful sentiment indicators. One such indicator is a weighed opinion of thousands of investors, and it’s a good predictor of short-term valuation.

import requests

from bs4 import BeautifulSoup

import timestocktwits_trending = requests.get('https://api.stocktwits.com/api/2/streams/trending.json').json()stocktwits_trending_tickers = [stocktwits_trending['messages'][index]['symbols'][0]['symbol'] for index in range(len(stocktwits_trending['messages']))]trending_sentiment = {}for index in stocktwits_trending_tickers: try: time.sleep(2) x = requests.get('https://www.stocktwits.com/symbol/{}'.format(index)) soup = BeautifulSoup(x.text, 'html.parser') texts = soup.findAll(text=True) Real_time=texts.index('Real-Time') Sentiment_index=texts[Real_time+1].index('sentimentChange') trending_sentiment['%s'%index]=texts[Real_time+1][Sentiment_index:Sentiment_index+21] except: pass…

The Flask micro web framework for Python allows for rapid development of web applications. Apps can be deployed to any free web hosting service, but one is particular makes the process seamless, and that is PythonAnywhere.

Below is the full code for a (very) simple application, explained in detail further down. For the sake of simplicity and speed, everything is kept inside one Python file, including the HTML which is normally separate:

This application is asking the user for data, sending a request to the server with the HTTP method ‘POST’, and then returning a string as HTML.

The…

Regression is fundamental to Predictive Analytics, and a good example of an optimization problem. The simplest cases of regression are calculable by hand (the results of which we can verify with Python). Below is the linear regression model of some predictor variable ‘x’ with response variable ‘y’.

Populations that undergo a fixed set of age transitions can be modeled as a system of linear equations, from which age distributions can be calculated.

As a brief review of linear algebra, a system of linear equations can be represented as a matrix equation of the form **Ax **=** b**. In this form,** **‘A*’* is the set of coefficients as some *m* × *n *matrix, ‘x*’* is some input vector in **ℝ***n*, and ‘b’ is the* *product of ‘A’ and ‘x’ as an output vector in **ℝ***m***, **shown below: