<jupyter_start><jupyter_text>Multi-Colinearity<jupyter_code>dk=df.drop(['Labels_2019','Area Code'],axis='columns')
y=df.Labels_2019

import statsmodels.api as sm

import scipy.misc
X = sm.add_constant(dk)
est = sm.OLS(y.astype(float), X.astype(float)).fit()
est.summary() # here i see P value of each feature and select those that P value is less then 0.05
# after the multicolinearity we have only 41 features out of 56
#https://www.graphpad.com/support/faq/multicollinearity-in-multiple-regression/ says to remove everything with low p value
new=X.drop(['16_% aged 0 to 4 years','16_% aged 5 to 14 years','16_% reporting good overall experience of making appointment',
           '16_% satisfied with phone access','17_% aged 0 to 4 years','17_% aged 5 to 14 years','17_% reporting good overall experience of making appointment',
           '17_% satisfied with phone access','18_% aged 0 to 4 years','18_% aged 5 to 14 years','18_% reporting good overall experience of making appointment',
           '18_% satisfied with phone access','const'],axis=1)
new['label 2019']=y
new
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(new)
scaled_data = scaler.transform(new)
scaled_data.shape
from sklearn.decomposition import PCA
pca = PCA(0.99)
pca.fit(scaled_data)
x_pca = pca.transform(scaled_data)
x_pca.shape

X_train, X_test, y_train, y_test = train_test_split(x_pca, y, test_size=0.20,random_state=42)
X_train.shape
from sklearn.neighbors import KNeighborsRegressor #here i put n_neighbor = 2 and i  get 65% accuracy
neigh = KNeighborsRegressor(n_neighbors=2,p=2)  # n_neighbor = 1 you get 100% accuracy
neigh.fit(x_pca,y)
pr=neigh.predict(X_test)
neigh.score(X_test,y_test) # you get 65% accuracy
from sklearn.metrics import r2_score
cr=r2_score(y_test,pr)
cr
%matplotlib inline
fig = plt.figure()
ax = fig.add_subplot(111)
ax.errorbar(y_test, pr, fmt='o')
ax.errorbar([1, y_test.max()], [1, y_test.max()])

from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
cr=r2_score(y_test,pr)
mae=mean_absolute_error(y_test, pr)
mse=mean_squared_error(y_test,pr)
print("this is R2 score:", cr)
print("This is Mean Absolute Error:", mae)
print("This is Mean squared Error:", mse)<jupyter_output>this is R2 score: 0.8815514754434712
This is Mean Absolute Error: 1.164971269000719
This is Mean squared Error: 2.217713981967749<jupyter_text>linear<jupyter_code>from sklearn import linear_model
mode=linear_model.LinearRegression()

mode.fit(x_pca,y)
mode.coef_
bb=mode.predict(X_test)
bb
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
cr=r2_score(y_test,pr)
mae=mean_absolute_error(y_test, bb)
mse=mean_squared_error(y_test,bb)
print("this is R2 score:", cr)
print("This is Mean Absolute Error:", mae)
print("This is Mean squared Error:", mse)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.errorbar(y_test, bb, fmt='o')
ax.errorbar([1, y_test.max()], [1, y_test.max()])<jupyter_output><empty_output><jupyter_text>SVR<jupyter_code>from sklearn.model_selection import cross_val_score
from sklearn.svm import SVR
regr=SVR(kernel='rbf')
scores = cross_val_score(regr,x_pca,y, cv=10)
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
print(scores)
reg.fit(x_pca,y)
reg.score(X_test,y_test)
svrpred=reg.predict(X_test)
%matplotlib inline
fig = plt.figure()
ax = fig.add_subplot(111)
ax.errorbar(y_test, svrpred, fmt='o')
ax.errorbar([1, y_test.max()], [1, y_test.max()])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12874678846702825, 'fraction_numerical': 0.04510419640308307, 'mean_word_length': 6.551724137931035, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '606966', 'license': 'MIT', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1316, 'n_words': 322, 'path': 'Multi-Collinearity & Normalization/K-neigh_SVR_Linear - regression using PCA (Multi-Colinearity).ipynb', 'repo_name': 'Hamza-Jamil121/Feature-Engineering-Feature-selection-with-Python-Programming', 'size': '3503'}