-
Notifications
You must be signed in to change notification settings - Fork 0
/
526_f.py
130 lines (101 loc) · 3.54 KB
/
526_f.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
"""526_F.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1AOYYHXUvCodecH5Hl6ZYLTwtPLiP-0GK
"""
#Importing the libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# import warnings
import warnings
warnings.filterwarnings("ignore")
# We will use some methods from the sklearn module
from sklearn import linear_model
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.model_selection import train_test_split, cross_val_score
# Reading the Dataset
df = pd.read_csv("526_data_pointsN.csv")
df = df.iloc[100:]
df = df.iloc[:-100]
df.head()
df.shape
#Setting the value for X and Y
X = df[['branch-misses', 'cache-misses', 'L1-dcache-load-misses', 'L1-icache-load-misses', 'LLC-load-misses', 'LLC-store-misses', 'branch-load-misses', 'dTLB-load-misses', 'dTLB-store-misses', 'iTLB-load-misses', 'l2_rqsts.code_rd_miss', 'l2_rqsts.demand_data_rd_miss', 'l2_rqsts.all_demand_miss']]
i = df['instructions'].values.reshape(-1,1)
y = df['CPI']
X = np.divide(X,i)
df_new = X.copy()
df_new['CPI'] = y
corr = df.corr()
plt.figure (figsize = (15,8))
sns.heatmap(corr, annot = True)
print(df.columns.values.tolist())
X.drop(['LLC-store-misses','branch-load-misses','l2_rqsts.code_rd_miss','LLC-load-misses','dTLB-load-misses','L1-icache-load-misses'],axis=1,inplace = True)
X.head()
#Fitting the Multiple Linear Regression model
mlr = LinearRegression()
#Splitting dataset
X_train,X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 100)
mlr.fit(X_train,y_train)
#Intercept and Coefficient
print("Intercept: ", mlr.intercept_)
print("Coefficients:")
list(zip(X, mlr.coef_))
#Prediction of test set
y_pred_mlr= mlr.predict(X_test)
#Predicted values
print("Prediction for test set: {}".format(y_pred_mlr))
#Actual value and the predicted value
mlr_diff = pd.DataFrame({'Actual value': y_test, 'Predicted value': y_pred_mlr})
mlr_diff.head()
#Model Evaluation
from sklearn import metrics
meanAbErr = metrics.mean_absolute_error(y_test, y_pred_mlr)
meanSqErr = metrics.mean_squared_error(y_test, y_pred_mlr)
rootMeanSqErr = np.sqrt(metrics.mean_squared_error(y_test, y_pred_mlr))
r2 = mlr.score(X_test,y_test)
print('R squared: {:.2f}'.format(r2*100))
print('Mean Absolute Error:', meanAbErr)
print('Mean Square Error:', meanSqErr)
print('Root Mean Square Error:', rootMeanSqErr)
final = X.mean()
R = np.multiply(final,mlr.coef_)
print(R)
print(R.sum()+mlr.intercept_)
finaly = y.mean()
print(finaly)
n = df.shape[0]
p = X.shape[1]
#Adjusted r2
Adjr2 = 1-(1-r2)*(n-1)/(n-p-1)
print(Adjr2)
#Residuals (added abs for now)
residuals = (y_test - y_pred_mlr)
print(residuals)
#F statistic
fstat = (r2/(1-r2))*((n-p-1)/p)
print(fstat)
from scipy.stats import f
p_value = 1-f.cdf(fstat,p,n-p-1)
print(p_value)
sns.set(style="whitegrid")
fig, ax = plt.subplots(figsize =(5,5))
sns.residplot(x=y_pred_mlr,y=residuals,ax=ax, lowess=True, line_kws={"color": "red"})
ax.set(ylabel='Residuals',xlabel='Predicted values')
import pandas as pd
R = pd.concat([pd.Series([mlr.intercept_],index = ['Base CPI']), R])
print(R)
fig, ax = plt.subplots()
groups = ['']
plt.figure (figsize = (2,5))
# Stacked bar chart with loop
for i in range(len(R)):
plt.bar(groups, R[i],label = R.index[i], bottom = np.sum(R[:i], axis = 0),width = 0.1)
plt.legend(bbox_to_anchor = (1.25, 0.6), loc='upper left')
plt.tight_layout()