-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_test_data_in_ml.py
38 lines (30 loc) · 1.13 KB
/
train_test_data_in_ml.py
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
# -*- coding: utf-8 -*-
"""Train_Test_data_in_ML.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/12JOywSqJO1M8XPj4WaxhNw9wnfHS2d52
"""
#80% for training, and 20% for testing.
import numpy
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score
numpy.random.seed(2)
x = numpy.random.normal(3, 1, 100)
y = numpy.random.normal(150, 40, 100) / x
train_x = x[:80]
train_y = y[:80]
test_x = x[80:]
test_y = y[80:]
mymodel = numpy.poly1d(numpy.polyfit(train_x, train_y, 4)) #polynomial regression
myline = numpy.linspace(0, 6, 100) # myline = numpy.linspace(start, stop, number of elements in the array)
#r2
#It measures the relationship between the x axis and the y axis, and the value ranges from 0 to 1, where 0 means no relationship, and 1 means totally related.
r2 = r2_score(train_y, mymodel(train_x))
#we can also check r2 for test data as following
#r2 = r2_score(train_y, mymodel(test_x))
plt.scatter(train_x, train_y)
plt.plot(myline, mymodel(myline))
plt.show()
print(r2)
#now as model is trained and tested properly now we can predict values
print(mymodel(5))