-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpH-vs-quality.py
58 lines (43 loc) · 1.07 KB
/
pH-vs-quality.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pandas as pd
import numpy as np
from cmath import sqrt
import scipy.stats as stats
from matplotlib import pyplot as plt
from sklearn.linear_model import LinearRegression
data = pd.read_csv('winequality-red.csv')
X = data['pH'].values[:,np.newaxis]
y = data['quality'].values
xtotal=0
ytotal =0
xytotal =0
x2total =0
y2total =0
n =1599
for i in range(n):
xtotal +=X[i]
for i in range(n):
ytotal +=y[i]
for i in range(n):
xytotal +=(X[i]*y[i])
for i in range(n):
x2total +=(X[i]*X[i])
for i in range(n):
y2total +=(y[i]*y[i])
num = xytotal - ((xtotal*ytotal)/n)
den1 = (x2total - ((xtotal*xtotal)/n))
den2 = (y2total - ((ytotal*ytotal)/n))
den = den1*den2
denf = sqrt(den)
r = num/denf
print("Correlation for pH and quality")
print(r)
model = LinearRegression()
model.fit(X, y)
plt.scatter(X, y,color='blue')
plt.plot(X, model.predict(X),color='k')
plt.title('quality Vs pH ', fontsize=14)
plt.xlabel('pH', fontsize=14)
plt.ylabel('quality', fontsize=14)
plt.show()
print('Intercept: \n', model.intercept_)
print('Coefficients: \n', model.coef_)