Data Science Practical 9
Data Science Practical 9
Practical 9
In [1]: import pandas as pd
In [2]: df=pd.read_csv('diabetes.csv')
In [3]: df.head(6)
Out[3]:
Pregnancies Glucose BloodPressure SkinThickness Insulin BMI DiabetesPedigreeFunction Age Outcome
1 1 85 66 29 0 26.6 0.351 31 0
3 1 89 66 23 94 28.1 0.167 21 0
In [4]: df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 768 entries, 0 to 767
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Pregnancies 768 non-null int64
1 Glucose 768 non-null int64
2 BloodPressure 768 non-null int64
3 SkinThickness 768 non-null int64
4 Insulin 768 non-null int64
5 BMI 768 non-null float64
6 DiabetesPedigreeFunction 768 non-null float64
7 Age 768 non-null int64
8 Outcome 768 non-null int64
dtypes: float64(2), int64(7)
memory usage: 54.1 KB
In [5]: df.describe()
Out[5]:
Pregnancies Glucose BloodPressure SkinThickness Insulin BMI DiabetesPedigreeFunction
In [6]: df.columns
In [7]: y=df['Outcome']
In [9]: df.shape
Out[9]: (768, 9)
In [10]: x.shape,y.shape
Out[20]: ▾ LinearRegression
LinearRegression()
In [21]: y_pred=model.predict(x_test)
In [22]: y_pred
Out[23]: 93 1
228 0
424 1
635 1
684 0
..
271 0
46 0
476 1
130 1
359 1
Name: Outcome, Length: 231, dtype: int64
In [77]: e1 =mean_absolute_error(y_test,y_pred)
In [27]: per_e
Out[27]: 773763381247726.2
In [79]: accuracy
Out[79]: 72.72727272727273
In [31]: mean_squared_error(y_test,y_pred)
Out[31]: 0.17082809946294492
Model 2
In [34]: model2.fit(x_train,y_train)
Out[34]: ▾ KNeighborsRegressor
KNeighborsRegressor()
In [36]: y_pred2
Out[36]: array([0.2, 0.6, 0. , 0.6, 0.4, 0.4, 0.8, 0.2, 0.4, 1. , 0. , 0.2, 0.4,
1. , 0.2, 0.8, 0. , 0. , 0.4, 0.8, 0.2, 0.6, 0. , 0.4, 0.6, 0.2,
0. , 0. , 0.4, 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Length: 231, dtype: int64
In [81]: error2
Out[81]: 0.3341991341991342
In [83]: accuracy2
Out[83]: 66.58008658008659
Model 3
In [50]: model3.fit(x_train,y_train)
Out[50]: ▾ DecisionTreeRegressor
DecisionTreeRegressor()
In [55]: y_pred3
Out[55]: array([0., 1., 1., 0., 0., 0., 1., 0., 0., 1., 0., 0., 1., 1., 0., 0., 0.,
0., 0., 1., 0., 1., 1., 1., 1., 0., 0., 0., 0., 1., 0., 0., 1., 0.,
0., 1., 0., 0., 0., 0., 0., 1., 1., 1., 0., 1., 0., 1., 1., 0., 0.,
0., 0., 1., 0., 0., 1., 1., 0., 0., 0., 1., 0., 0., 0., 1., 1., 1.,
0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 1., 1., 0., 0., 0., 1., 1.,
0., 0., 1., 1., 0., 1., 0., 0., 1., 0., 1., 0., 1., 0., 1., 0., 0.,
0., 0., 1., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.,
0., 0., 1., 0., 0., 0., 1., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0.,
1., 0., 0., 1., 1., 0., 0., 0., 1., 1., 0., 1., 1., 0., 0., 1., 0.,
1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 1., 1., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 0., 1., 1.,
0., 1., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 1., 0.,
1., 0., 0., 0., 0., 1., 0., 0., 1., 1., 1., 0., 0., 0., 0., 0., 0.,
1., 1., 1., 1., 0., 0., 0., 1., 0., 1.])
In [56]: y_test
Out[56]: 93 1
228 0
424 1
635 1
684 0
..
271 0
46 0
476 1
130 1
359 1
Name: Outcome, Length: 231, dtype: int64
In [85]: error3
Out[85]: 0.2727272727272727
In [87]: accuracy3
Out[87]: 72.72727272727273
Model 4
In [65]: model4.fit(x_train,y_train)
Out[65]: ▾ RandomForestRegressor
RandomForestRegressor()
In [66]: y_pred4=model4.predict(x_test)
In [67]: y_pred4
Out[67]: array([0.03, 0.6 , 0.68, 0.25, 0.18, 0.53, 0.65, 0.21, 0.21, 0.97, 0.52,
0.09, 0.66, 0.96, 0.06, 0.61, 0.13, 0.03, 0.06, 0.33, 0.3 , 0.61,
0.38, 0.69, 0.69, 0.15, 0.02, 0. , 0.02, 0.6 , 0.03, 0.11, 0.61,
0.11, 0.14, 0.35, 0.05, 0.54, 0.34, 0.36, 0.13, 0.7 , 0.37, 0.23,
0.21, 0.94, 0.1 , 0.77, 0.7 , 0.26, 0.42, 0.24, 0.01, 0.61, 0.15,
0.02, 0.21, 0.67, 0.01, 0.02, 0.21, 0.21, 0.1 , 0.3 , 0.23, 0.9 ,
0.66, 0.63, 0.19, 0.06, 0.02, 0.1 , 0.11, 0.05, 0.14, 0.98, 0.76,
0.67, 0.46, 0.69, 0.12, 0.1 , 0.11, 0.88, 0.34, 0.3 , 0.77, 0.4 ,
0.08, 0.28, 0.62, 0.04, 0.09, 0.64, 0.14, 0.76, 0.32, 0.42, 0.21,
0.52, 0.03, 0.69, 0. , 0.6 , 0.88, 0.36, 0.94, 0.57, 0.88, 0.01,
0.2 , 0.18, 0.39, 0.2 , 0.15, 0.69, 0.2 , 0.01, 0.29, 0.03, 0.06,
0.5 , 0.02, 0.26, 0. , 0.26, 0.23, 0.85, 0.66, 0.8 , 0.02, 0.23,
0.34, 0.91, 0. , 0.36, 0.37, 0.07, 0.46, 0.52, 0.44, 0.07, 0. ,
0.15, 0.89, 0.92, 0. , 0.67, 0.92, 0.05, 0. , 0.61, 0.24, 0.78,
0.5 , 0.2 , 0.02, 0.51, 0.43, 0.31, 0.47, 0.42, 0. , 0.91, 0.18,
0.66, 0.1 , 0.6 , 0.39, 0.04, 0.29, 0.16, 0.64, 0.07, 0.5 , 0.69,
0.22, 0. , 0.48, 0.06, 0.35, 0.52, 0.7 , 0.4 , 0.09, 0.28, 0.95,
0.2 , 0.75, 0.69, 0.64, 0.94, 0.23, 0.29, 0.1 , 0.48, 0.25, 0.03,
0.31, 0.61, 0.6 , 0.27, 0.45, 0.2 , 0.82, 0.3 , 0.42, 0. , 0.4 ,
0.58, 0.07, 0.25, 0.38, 0.44, 0.38, 0.03, 0.01, 0.04, 0.43, 0. ,
0.16, 0.64, 0.17, 0.95, 0.71, 0.31, 0.05, 0.57, 0.28, 0.72, 0.93])
In [68]: y_test
Out[68]: 93 1
228 0
424 1
635 1
684 0
..
271 0
46 0
476 1
130 1
359 1
Name: Outcome, Length: 231, dtype: int64
In [90]: accuracy4
Out[90]: 68.58008658008657
Linear Regressor
In [91]: accuracy
Out[91]: 72.72727272727273
KNeighborsRegressor
In [92]: accuracy2
Out[92]: 66.58008658008659
DecisionTreeRegressor
In [93]: accuracy3
Out[93]: 72.72727272727273
RandomForestRegressor
In [94]: accuracy4
Out[94]: 68.58008658008657
In [ ]: