<jupyter_start><jupyter_text><NAME>E19CSE332<jupyter_code>import numpy as np
import pandas as pd
path = 'https://raw.githubusercontent.com/Yoshibansal/Machine-learning/main/Regression/communities.data'
raw_df = pd.read_csv(path, sep=',', header=None)
pd.set_option('display.max_columns', None)
raw_df.head()
raw_df.shape
df = raw_df.replace("?", np.nan)
pd.set_option('display.max_rows', None)
nul_col = df.isnull().sum()
print(nul_col[nul_col > 0])
nan_cols = [i for i in df.columns if df[i].isnull().any()]
nan_cols
print("Total values in 3rd coloumn: ", len(df[3]))
print("Unique values in 3rd coloumn: ", len(df[3].unique()))
df.head()
df[nan_cols] = df[nan_cols].fillna(method='ffill')
df.head()
df = df.replace(np.nan, 1);
df.head()
from sklearn.preprocessing import MinMaxScaler
scale = MinMaxScaler()

df[[0,1,2,4]] = pd.DataFrame(scale.fit_transform(df[[0,1,2,4]].values), columns=[0,1,2,4], index=df.index)
df.head()
from sklearn.preprocessing import LabelEncoder
enc = LabelEncoder()

df[3] = enc.fit_transform(df[3])
df.head()
df = df.to_numpy()

X = df[:, :-1]
y = df[:, -1]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=10)
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.linear_model import LinearRegression
#@title Degree
degree = 2 #@param {type:"slider", min:1, max:4, step:1}

model = make_pipeline(PolynomialFeatures(degree),LinearRegression())
model.fit(X_train, y_train)
y_train_pred = model.predict(X_train)
rmse_train = np.sqrt(mean_squared_error(y_train, y_train_pred))
r2_train = r2_score(y_train, y_train_pred)
print("RMSE train: ", rmse_train)
print("R2 train: ", r2_train)
y_test_pred = model.predict(X_test)
rmse_test = np.sqrt(mean_squared_error(y_test, y_test_pred))
r2_test = r2_score(y_test, y_test_pred)
print("RMSE test: ", rmse_test)
print("R2 test: ", r2_test)
print("Accuracy/Score (Train): ", model.score(X_train, y_train))<jupyter_output>Accuracy/Score (Train):  1.0<jupyter_text>Linear Regression<jupyter_code>lr = LinearRegression()

lr.fit(X_train, y_train)
y_pred = lr.predict(X_test)
from sklearn.metrics import mean_squared_error

print("MSE: ", mean_squared_error(y_test, y_pred))
print("Accuracy/Score (Train): ", lr.score(X_train, y_train))
# print("Accuracy/Score (Test): ", lr.score(X_test, y_test))<jupyter_output>Accuracy/Score (Train):  0.7180172383801056<jupyter_text>Random Forest Regression<jupyter_code>from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor()

regressor.fit(X_train, y_train)
print("Accuracy/Score (Train): ", regressor.score(X_train, y_train))
# print("Accuracy/Score (Test): ", regressor.score(X_test, y_test))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14965034965034965, 'fraction_numerical': 0.022377622377622378, 'mean_word_length': 7.830246913580247, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 1, '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': '565344', 'license': 'Apache-2.0', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1109, 'n_words': 200, 'path': 'Regression/Regression_data_preprocessing_p2.ipynb', 'repo_name': 'Yoshibansal/Machine-learning', 'size': '2866'}