<jupyter_start><jupyter_text>Step 1 Problem Statement  * You are hired as a data scientist in serven which is a wine producing company and they want to you to predict the price of new variety of wine by the given data of old wine production   Step 2 Import the libs<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Step 3 Read the data from data csv in a dataframe<jupyter_code>train_data = pd.read_csv(r'D:\MachineLearning\DataScience-Projects\USAHousePricePredections(KFoldCrossValidation)\USA_Housing.csv')
train_data.head()
train_data.shape
train_data=train_data.drop(['Address'],axis=1)
train_data.describe()<jupyter_output><empty_output><jupyter_text>Some of the observations we made are,* The average price of the house in USA is 1.232073e+06* The minimum price of the house in USA is 1.593866e+04* The Maximum price of the house in USA is 2.469066e+06* The max age of an house in USA is 9.5 years  Step 4 Check for the missing values<jupyter_code>train_data.isnull().sum()<jupyter_output><empty_output><jupyter_text>* There are no missing data in the dataset, so we dont need data cleaning process  Step 5 Data Visualization<jupyter_code>sns.jointplot(x="Avg. Area House Age",y="Price",             
             data=train_data,kind='hex',color="#4CB391")
sns.scatterplot(x="Avg. Area Number of Bedrooms",y="Price",             
             data=train_data)<jupyter_output><empty_output><jupyter_text>Step 6 Modeling the data<jupyter_code>train_data.columns
X = train_data[['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms',
       'Avg. Area Number of Bedrooms', 'Area Population']]
y = train_data['Price']
X.head()
y.head()
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=101)
X_train.shape
X_test.shape
from sklearn.linear_model import LinearRegression
LinearReg = LinearRegression()
LinearReg.fit(X_train, y_train)
y_pred=LinearReg.predict(X_test)
print("Accuracy using Linear Reg is",LinearReg.score(X,y)*100)
from sklearn.svm import SVR
svr = SVR()
svr.fit(X_train, y_train)
y_pred = svr.predict(X_test)
print("Accuracy using SVR Reg is",svr.score(X,y)*100)
from sklearn.ensemble import RandomForestRegressor
randomforest = RandomForestRegressor(n_estimators=100)
randomforest.fit(X_train, y_train)
y_pred = randomforest.predict(X_test)
print("Accuracy using Random Forest is",randomforest.score(X,y)*100)<jupyter_output>Accuracy using Random Forest is 96.33716432526217<jupyter_text>* Now we can see the differnt models gives us different results and from all these random forest gives us the best results.* The problem here is if for each train test split our models gives us differnt resutls so it will be difficult to judge the model.* To overcome the above problem we are going to implement the K Fold Cross validation technique.  K Fold Cross Validation Implementation<jupyter_code>from sklearn.model_selection import KFold
scores_linearReg=[]
best_linear = LinearRegression()
cv = KFold(n_splits=10)
for train_index,test_index in cv.split(train_data):
    print("Train Index:", train_index,"\n")
    print("Test Index:", test_index,"\n")
    X_train,X_test,y_train,y_test=X.iloc[train_index],X.iloc[test_index],y.iloc[train_index],y.iloc[test_index]
    best_linear.fit(X_train,y_train)
    scores_linearReg.append(best_linear.score(X,y))<jupyter_output>Train Index: [ 500  501  502 ... 4997 4998 4999] 

Test Index: [  0   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 131 132 133 134 135 136 137 138 139 140 141 142 143
 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 [...]<jupyter_text>Here we have cretaed 10 folds of our data, * when  the first fold fold is taken as test data, the remaining 9 folds are taken as training data and similarly the score is calculated for each pair of train and test split * so at the end to get the  score of our model we can take the avg of the list which contains the score<jupyter_code>scores_linearReg<jupyter_output><empty_output><jupyter_text>In our model creation step we have created three models now we can apply K-Fold validation on all the three models<jupyter_code>##creating a method that generalize the model fitting process
def get_score(model,X,y):
    model.fit(X,y)
    return model.score(X,y)b
## Now we are copying the above code for k fold cross validation to apply it on all the three models
from sklearn.model_selection import KFold
scores_linearReg=[]
scores_svm=[]
scores_rf=[]
best_linear = LinearRegression()
cv = KFold(n_splits=10)
for train_index,test_index in cv.split(train_data):
    print("Train Index:", train_index,"\n")
    print("Test Index:", test_index,"\n")
    X_train,X_test,y_train,y_test=X.iloc[train_index],X.iloc[test_index],y.iloc[train_index],y.iloc[test_index]
    scores_linearReg.append(get_score(LinearRegression(),X_train,y_train))
    scores_svm.append(get_score(SVR(),X_train,y_train))
    scores_rf.append(get_score(RandomForestRegressor(n_estimators=100),X_train,y_train))
scores_linearReg
scores_svm
scores_rf
## Show the accuracy of each model 
print('The Accuracy of Linear Regression after K-Fold',np.mean(scores_linearReg)*100)
print('The Accuracy of SVM Regression after K-Fold',np.mean(scores_svm)*100)
print('The Accuracy of Random Forest Regression after K-Fold',np.mean(scores_rf)*100)<jupyter_output>The Accuracy of Linear Regression after K-Fold 91.80274663590706
The Accuracy of SVM Regression after K-Fold -0.0007917177826788359
The Accuracy of Random Forest Regression after K-Fold 98.40646576412078
{'fraction_non_alphanumeric': 0.07811772014287933, 'fraction_numerical': 0.11616710669358596, 'mean_word_length': 4.689045936395759, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '565857', 'license': 'MIT', 'n_tokens_mistral': 2797, 'n_tokens_neox': 2147, 'n_words': 773, 'path': 'USAHousePricePredections(KFoldCrossValidation)/HousePricePredection(KFold).ipynb', 'repo_name': 'Lokesh824/DataScience_Projects', 'size': '6439'}