<jupyter_start><jupyter_text>Topic Selection<jupyter_code>from sklearn import model_selection
from sklearn.model_selection import train_test_split
from sklearn import feature_selection
from sklearn.linear_model import LinearRegression<jupyter_output><empty_output><jupyter_text>LDA<jupyter_code>headers = np.arange(20)
df = pd.DataFrame(LDA_Matrix, columns=headers)
df['rating'] = rating
df = df.dropna()
df.head()
df.shape
y = df['rating']
x = df.drop(['rating'],axis=1)
# Feature selection
LR = LinearRegression()

percentiles = range(1, 100, 5)
results = []
for i in range(1, 101, 5):
    fs = feature_selection.SelectPercentile(feature_selection.f_regression, percentile=i)
    cm_fs = fs.fit_transform(x, y)
    scores = abs(model_selection.cross_val_score(LR, cm_fs, y, cv=5, scoring='neg_mean_absolute_error'))
    print(i,scores.mean())
    results = np.append(results, scores.mean())
cm_feature = x.columns
optimal_percentile = int(np.where(results == results.min())[0])
print("Optimal percentile of features:{0}".format(percentiles[optimal_percentile]), "\n")
optimal_num_features = int(percentiles[optimal_percentile]*len(cm_feature)/100)
print("Optimal number of features:{0}".format(optimal_num_features), "\n")
import pylab as pl
pl.figure()
pl.xlabel("Percentage of features selected")
pl.ylabel("Cross validation accuracy")
pl.plot(percentiles, results)
fs = feature_selection.SelectPercentile(feature_selection.f_regression, percentile=96)
x_fs = fs.fit_transform(x, y)
for i in range(len(cm_feature)):
    if fs.get_support()[i]:
        print(cm_feature[i],'\t', fs.scores_[i] )
from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler(copy=True, feature_range=(0,1)).fit(x_fs)
data_norm = min_max_scaler.transform(x_fs)
data_norm[0]
x_train_fs, x_test_fs, y_train_fs, y_test_fs = train_test_split(data_norm, y, test_size=0.33, random_state=123)
LR = LinearRegression()
LR.fit(x_train_fs, y_train_fs)<jupyter_output><empty_output><jupyter_text>LDA Feature Selection with Linear Regression Result<jupyter_code>from sklearn import metrics
y_pred_fs = LR.predict(x_test_fs)

# MAE
print(metrics.mean_absolute_error(y_test_fs, y_pred_fs))
# MSE
print(metrics.mean_squared_error(y_test_fs, y_pred_fs))
# RMSE
print(np.sqrt(metrics.mean_squared_error(y_test_fs, y_pred_fs)))
print ('Regression Coefficients: \n', LR.coef_)<jupyter_output>Regression Coefficients: 
 [-0.02621223 -0.10274409 -0.12407025  0.06289692  0.01347483 -0.20530169
 -0.03716582  0.06562148 -0.26250509 -0.12568571  0.01832149 -0.07666346
  0.02692166  0.04657694  0.07632491  0.06946034 -0.01348178 -0.08679014
  0.031802  ]<jupyter_text>LSA<jupyter_code>df = pd.DataFrame(LSA_Matrix, columns=headers)
df['rating'] = rating
df = df.dropna()

y = df['rating']
x = df.drop(['rating'],axis=1)

# Feature selection
LR = LinearRegression()

percentiles = range(1, 100, 5)
results = []
for i in range(1, 101, 5):
    fs = feature_selection.SelectPercentile(feature_selection.f_regression, percentile=i)
    cm_fs = fs.fit_transform(x, y)
    scores = abs(model_selection.cross_val_score(LR, cm_fs, y, cv=5, scoring='neg_mean_absolute_error'))
    print(i,scores.mean())
    results = np.append(results, scores.mean())
cm_feature = x.columns
optimal_percentile = int(np.where(results == results.min())[0])
print("Optimal percentile of features:{0}".format(percentiles[optimal_percentile]), "\n")
optimal_num_features = int(percentiles[optimal_percentile]*len(cm_feature)/100)
print("Optimal number of features:{0}".format(optimal_num_features), "\n")
import pylab as pl
pl.figure()
pl.xlabel("Percentage of features selected")
pl.ylabel("Cross validation accuracy")
pl.plot(percentiles, results)
fs = feature_selection.SelectPercentile(feature_selection.f_regression, percentile=96)
x_fs = fs.fit_transform(x, y)
for i in range(len(cm_feature)):
    if fs.get_support()[i]:
        print(cm_feature[i],'\t', fs.scores_[i] )
from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler(copy=True, feature_range=(0,1)).fit(x_fs)
data_norm = min_max_scaler.transform(x_fs)
x_train_fs, x_test_fs, y_train_fs, y_test_fs = train_test_split(data_norm, y, test_size=0.33, random_state=123)
LR = LinearRegression()
LR.fit(x_train_fs, y_train_fs)<jupyter_output><empty_output><jupyter_text>LSA Feature Selection with Linear Regression Result¶<jupyter_code>from sklearn import metrics
y_pred_fs = LR.predict(x_test_fs)

# MAE
print(metrics.mean_absolute_error(y_test_fs, y_pred_fs))
# MSE
print(metrics.mean_squared_error(y_test_fs, y_pred_fs))
# RMSE
print(np.sqrt(metrics.mean_squared_error(y_test_fs, y_pred_fs)))
print ('Regression Coefficients: \n', LR.coef_)<jupyter_output>Regression Coefficients: 
 [ -3.39449694  -4.22199043  -9.52949973 -13.38597806   5.32169287
  -2.16527296  -0.8487228   -0.70243086   0.77299687  -2.17950102
   1.18424808  -0.50771369  -0.91771828   0.01950765   0.69828719
   6.78278763  -1.19926709   5.09381653  -0.05375124]
{'fraction_non_alphanumeric': 0.13033843674456083, 'fraction_numerical': 0.08037872683319904, 'mean_word_length': 6.673879443585781, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '656244', 'license': 'MIT', 'n_tokens_mistral': 2123, 'n_tokens_neox': 1860, 'n_words': 327, 'path': 'LDA LSA/LDA and LSA to Matrix.ipynb', 'repo_name': 'AlfredYan/NLP-to-the-rescue', 'size': '4964'}