-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_linear_regression.py
More file actions
59 lines (44 loc) · 1.61 KB
/
simple_linear_regression.py
File metadata and controls
59 lines (44 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Simple Linear Regression
# Importing the libraries
#import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('/home/deepak/analytics/wine.csv') #you may change the dataset!!
print(type(dataset))
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)
# Fitting Simple Linear Regression to the Training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
#Calculating the coefficients:
print(regressor.coef_)
#Calculating the intercept:
print(regressor.intercept_)
# Predicting the Test set results
y_pred = regressor.predict(X_test)
# Accuracy of the model
#Calculating the r squared value:
from sklearn.metrics import r2_score
r2_score(y_test,y_pred)
#Score for training dataset and test dataset.
print('Train Score:', regressor.score(X_train, y_train))
print('Test Score:', regressor.score(X_test, y_test))
#Create a DataFrame
df1 = {'Actual Applicants':y_test,
'Predicted Applicants':y_pred}
df1 = pd.DataFrame(df1,columns=['Actual Applicants','Predicted Applicants'])
print(df1)
# change the dtype to 'float64'
y_test = y_test.astype('float64')
# Also print the data type
print(y_test.dtype)
# Visualising the predicted results
line_chart1 = plt.plot(X_test,y_pred, '--')
line_chart2 = plt.plot(X_test,y_test, ':')
plt.show()
#--------------------------------------------------------