CRIM 0
ZN 0
INDUS 0
CHAS 0
NOX 0
RM 0
AGE 0
DIS 0
RAD 0
TAX 0
PTRATIO 0
B 0
LSTAT 0
PRICE 0
dtype: int64
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 506 entries, 0 to 505
Data columns (total 14 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 CRIM 506 non-null float64
1 ZN 506 non-null float64
2 INDUS 506 non-null float64
3 CHAS 506 non-null float64
4 NOX 506 non-null float64
5 RM 506 non-null float64
6 AGE 506 non-null float64
7 DIS 506 non-null float64
8 RAD 506 non-null float64
9 TAX 506 non-null float64
10 PTRATIO 506 non-null float64
11 B 506 non-null float64
12 LSTAT 506 non-null float64
13 PRICE 506 non-null float64
dtypes: float64(14)
memory usage: 55.5 KB
# creating feature and target variable
X = data_.drop(['PRICE'], axis=1)
y = data_['PRICE']
# splitting into training and testing set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=1)
print("X training shape : ", X_train.shape )
print("X test shape : ", X_test.shape )
print("y training shape :" , y_train.shape )
print("y test shape :", y_test.shape )
# creating model
from sklearn.ensemble import RandomForestRegressor
classifier = RandomForestRegressor()
classifier.fit(X_train, y_train)
X training shape : (404, 13)
X test shape : (102, 13)
y training shape : (404,)
y test shape : (102,)
RandomForestRegressor()
# Model evaluation for training data
prediction = classifier.predict(X_train)
print("r^2 : ", metrics.r2_score(y_train, prediction))
print("Mean Absolute Error: ", metrics.mean_absolute_error(y_train, prediction))
print("Mean Squared Error: ", metrics.mean_squared_error(y_train, prediction))
print("Root Mean Squared Error : ", np.sqrt(metrics.mean_squared_error(y_train, prediction)))
# Model evaluation for testing data
prediction_test = classifier.predict(X_test)
print("r^2 : ", metrics.r2_score(y_test, prediction_test))
print("Mean Absolute Error : ", metrics.mean_absolute_error(y_test, prediction_test))
print("Mean Squared Error : ", metrics.mean_squared_error(y_test, prediction_test))
print("Root Mean Absolute Error : ", np.sqrt(metrics.mean_squared_error(y_test, prediction_test)))
r^2 : 0.9830647954820073
Mean Absolute Error: 0.8002103960396031
Mean Squared Error: 1.3680492747524753
Root Mean Squared Error : 1.1696363856996221
r^2 : 0.9132857647425381
Mean Absolute Error : 2.2969901960784305
Mean Squared Error : 8.569741499999996
Root Mean Absolute Error : 2.9274120823689986
'[{"CRIM": 0.04932, "ZN": 33.0, "INDUS": 2.18, "CHAS": 0.0, "NOX": 0.472, "RM": 6.849, "AGE": 70.3, "DIS": 3.1827, "RAD": 7.0, "TAX": 222.0, "PTRATIO": 18.4, "B": 396.9, "LSTAT": 7.53}]'