19
Demystifying machine learning for beginners (Pt.2)
In the last "Demystifying machine learning for beginners" blog post, I've explained and demonstrated how to plot data, as well as classify new pieces of data. In this blog post, I'll be demonstrating how to predict a value based on another value using linear regression!
First, import the required libraries(you can install them by using
pip install
or pip3 install
):from sklearn.linear_model import LinearRegression
import pandas
from sklearn import preprocessing
import numpy as np
For this tutorial, we're going to be using this dataset for medical expenses based on values such as: age, sex, bmi and more...
df = pandas.read_csv('insurance.csv')
Next, we're going make the "age" column represent the X axis, and let the "charges" column represent the Y axis, which will be the set of values which we are going to try to predict:
X = np.array(df["age"]).reshape((-1, 1))
y = np.array(df["charges"])
Before predicting new values, finally add these lines of code:
model = LinearRegression()
model.fit(X, y)
Put these lines of code at the end of your script:
X_predict = [[35]]
y_predict = model.predict(X_predict)
print(y_predict)
If you run your code, your output should look like this:
>[12186.1766594]
First of all, to add another value to our model, replace
X = np.array(df["age"]).reshape((-1, 1))
with:X = list(zip(df["age"], df["bmi"]))
Now we'll be able to predict a person's medical charges based not only on their age, but also based on their bmi:
X_predict = [[35, 45]]
y_predict = model.predict(X_predict)
print(y_predict)
If you run your script, the output should look something like this:
>[17026.20170095]
As you can see, this is different from our previous result because we've introduced new pieces of information into our model.

You can now experiment with this code, add more values, predict different sets of values and more!
If you're a beginner who likes discovering new things about python, try my weekly python newsletter

Byeeeeeđź‘‹
19