Demystifying Machine Learning for Beginners (Pt.2)
Tags: python, machinelearning, ai, programming
Published: 2021-08-01 10:57:57

### If you're a confused beginner like I was when just starting out with machine learning in python, then stick around, because today, I'll be trying my best at demystifying and simplifying machine learning for you!

**************
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!

**************
## Let's get started!

First, import the required libraries(you can install them by using `pip install` or `pip3 install`):
```python
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](https://www.kaggle.com/mirichoi0218/insurance) for medical expenses based on values such as: age, sex, bmi and more...
```python
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:
```python
X = np.array(df["age"]).reshape((-1, 1))
y = np.array(df["charges"])
```
* _The "age" and "charges" columns are turned into arrays using the "np.array" method._
* _The "reshape" function then makes sure to turn the "age" column into a 2D array._

************
Before predicting new values, finally add these lines of code:
```python
model = LinearRegression()
model.fit(X, y)
```
* _This will define our linear regression model, as well as fit it with the "X" and "y" values defined earlier._

**************
###Now we can finally predict new values!
Put these lines of code at the end of your script:
```python
X_predict = [[35]]
y_predict = model.predict(X_predict)
print(y_predict)
```
* _This will try to predict the insurance price based on the age, in this case, the age is "35 years old".

**If you run your code, your output should look like this:**
```python
>[12186.1766594]
```
* _As you can see, our python script predicted that someone aged 35 years old would have medical charges of about 12186$_

********
#### Now of course, this prediction isn't very accurate because a person and a person's medical expenses aren't going to be defined solely by their age, that's why, we're going to add some more values to our model.

**********
First of all, to add another value to our model, replace `X = np.array(df["age"]).reshape((-1, 1))` with:
```python
X = list(zip(df["age"], df["bmi"]))
```
* _This will add the values of the "bmi" column to the age values to create a 2D array._

************
Now we'll be able to predict a person's medical charges based not only on their age, but also based on their bmi:
```python
X_predict = [[35, 45]]
y_predict = model.predict(X_predict)
print(y_predict)
```
* _Here our python script is going to predict one's medical charges based on their age and their bmi, where "35" is their age and "45" is their bmi._

************
If you run your script, the output should look something like this:
```python
>[17026.20170095]
```
As you can see, this is different from our previous result because we've introduced new pieces of information into our model.

************
### Nice! your first working linear regression model that can predict values.

![noiiiice](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6csmx6bu47jnodi57le.jpeg)

************
**You can now experiment with this code, add more values, predict different sets of values and more!**

************

Byeeeee👋