<jupyter_start><jupyter_text>Part 1 - Univariate regression<jupyter_code>X_1 = df['volatile acidity'] #The first part of the task is to do the Univariate regression so I have selected one feature
y = df['quality'] #The target in this is the quality
X_1.shape #This code checks the shape of my data to see if I have what I want
y.shape#This code shows the shape of the targets
X1_train, X1_test, y1_train, y1_test = train_test_split(X_1,y, test_size=0.2, random_state=42) #This code split your data into training (80%) and testing (20%)
X1_train.shape #This code check the shape of my X train data to see if I have what I am looking for
X1_train = X1_train.values.reshape(-1,1) #This code is the reshape X1_train data
X1_train.shape #This code checks the shape of my data to see if I have what I want
y1_train = y1_train.values.reshape(-1,1)#This code is the reshape y1_train data
y1_train.shape #This code checks the shape of my data to see if I have what I want
y1_test = y1_test.values.reshape(-1,1)
X1_test = X1_test.values.reshape(-1,1)
y1_train.shape
X1_test.shape #This checks the shape of the testing data that I have splited
model = LinearRegression() #This code initialise the model
model.fit(X1_train, y1_train) #This code fit out the train data
model.coef_ #This code will get coefficient w_1
model.intercept_ #This code will give us w_0<jupyter_output><empty_output><jupyter_text>The model, given the above output, is as follows:$y = d_0w_0+d_1w_1, d_0=1, w_0=6,w_1=x,d_1=-1$$y = 6 -1w_0$$y = 6 -1d_1, d_1$ = feature 1 = 'volatile acidity'$y = 6d_0-1d_1, d_0 = 1, d_1 =$ 'volatile acidity'<jupyter_code>Y_pred = model.predict(X1_test) #The code shows the model to the testing features and get the predictions
mean_absolute_error(y1_test, Y_pred) #This code will give the mean squared error of the 'volatile acidity' feature test data
feature_col = ['fixed acidity','volatile acidity','citric acid','residual sugar','chlorides','free sulfur dioxide','total sulfur dioxide','density','pH','sulphates','alcohol'] #This code put all the features of the data file
X=df[feature_col] #This code takes the features of the dataset to be trained and get the best mean absolute error
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=42) #This code split your data into training (80%) and testing (20%)
mean_abs_error = dict() #This code creates a dictionary which store all mean absolute error for features
for i in range(len(feature_col)): #This for loop take one feature to get the best mean absolute error
  X_train_all = X_train.iloc[:, i].values.reshape(-1, 1) #This code reshape X_train to be checked for mean absolute error

  model.fit(X_train_all, y_train) #This code fits each univariate linear regression in the dataset

  X_test_all = X_test.iloc[:,i].values.reshape(-1, 1) #The code reshape X_test to predict the model

  Y_pred = model.predict(X_test_1) #This code predict the targets for test dataset
  mean_abs_error.update({feature_col[i]:mean_absolute_error(y_test, Y_pred)}) #This code updates the dictionary will all the mean absolute error 
mean_abs_error #This code checks all the mean absolute error of the feature to compare
x=min(mean_abs_error, key=mean_abs_error.get) #This find the minimum mean absolute error which is the best
print("The best mean absolute error is : {} with the value = {}".format(x,mean_abs_error[x]))#This code feature resulted in the best mean absolute error on the test data<jupyter_output>The best mean absolute error is : alcohol with the value = 0.5749664687393711<jupyter_text>Part 2 - multi-variate regression Since we have already split all the feature before the loop I can just use it for multi-variate regression<jupyter_code>model.fit(X_train, y_train) #This code fits the data for multi-variate on trained data
Y_pred = model.predict(X_test) #This code predicts our outcome of the model
multi_error = mean_absolute_error(y_test, Y_pred) #This code give us the mean absolute error of multi-variate model

print("The mean absolute error of the multi-variate is : {}".format(multi_error)) #This code print out the mean absolute error of the multi-variate<jupyter_output>The mean absolute error of the multi-variate is : 0.5035304415524372
{'fraction_non_alphanumeric': 0.0805384978743505, 'fraction_numerical': 0.028105810108644307, 'mean_word_length': 5.4854517611026035, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '407823', 'license': 'MIT', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1336, 'n_words': 548, 'path': 'musawenkosi_DS_2022_Regression_prac2.ipynb', 'repo_name': 'musawenkosikhulu/Linear-regression', 'size': '4234'}