# Machine Learning: Simple Linear Regression with Python Example- To predict students scores based on their study hours

0 like 0 dislike
5.6k views

edited

Prediction using Supervised Machine Learning using Simple Linear Regression

In this task we have to find the students scores based on their study hours. This is a simple Regression problem type because it has only two variables.

0 like 0 dislike
by (278 points)
selected
1 like 0 dislike
by (113 points)

import pandas as pd

# In[3]:

print(data.tail())

print(data.shape)

print(data.dtypes)

print(data.columns)

print(data.corr())

# In[4]:

data.describe()

# In[5]:

type(data)

# In[13]:

x = data.iloc[:,0:-1]

y = data.iloc[:,-1]

print(x.shape,y.shape)

# In[14]:

from sklearn.model_selection import train_test_split

x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=.2,random_state=1)

print(x_train.shape)

print(x_test.shape)

print(y_train.shape)

print(y_test.shape)

# In[15]:

from sklearn.linear_model import LinearRegression

std_model  = LinearRegression()

std_model.fit(x_train,y_train)

# In[17]:

y_pred = std_model.predict(x_test)

print(y_pred)

print(y_test)

print(x_test)

# In[22]:

print(std_model.coef_)

print(std_model.intercept_)

from sklearn.metrics import r2_score, mean_squared_error

print("accuracy is: ", r2_score(y_test,y_pred))

print(mean_squared_error(y_test,y_pred))

# In[24]:

vif = 1/1-(r2_score(y_test,y_pred))

vif

```coef:  [9.9073553]
intercept: 0.2569567372371395
accuracy is:  0.9311770139374417
mean squared error: 3.7164412473781487```

### Best Online Learning Opportunities

UDEMY::  Attend All Udemy Courses in Just INR 450[Coupon]