<jupyter_start><jupyter_text>Filter methods<jupyter_code>ames = pd.read_csv(path)
ames.head()
X = ames[ames.columns[ames.columns != "SalePrice"]]
y = ames["SalePrice"]
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0, test_size=0.3)
X_train["Class"] = y_train
X_train.head()
t_corr = X_train.corr()["Class"]
corr_columns = list(t_corr[abs(t_corr)>0.5].index)
corr_columns
if "Class" in corr_columns:
    corr_columns.remove("Class")
X_train_new = X_train[corr_columns]
X_test_new = X_test[corr_columns]
model = LinearRegression()
model.fit(X_train_new, y_train)
corr_score = r2_score(y_test, model.predict(X_test_new))
print(corr_score)
model.score(X_test_new, y_test)
from sklearn.feature_selection import chi2
from sklearn.feature_selection import SelectKBest
X = ames.drop(columns="SalePrice", axis=1)
y = ames["SalePrice"].copy()
test = SelectKBest(score_func=chi2, k=60)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0, test_size=0.3)
X_train.shape
test.fit(X_train, y_train)
X_train = test.transform(X_train)
X_train.shape
X_test = test.transform(X_test)
X_test.shape
model = LinearRegression()
model.fit(X_train, y_train)
chi2_score = model.score(X_test, y_test)
print(chi2_score)
from sklearn.feature_selection import f_regression
X = ames.drop(columns="SalePrice", axis=1)
y = ames["SalePrice"].copy()
test = SelectKBest(score_func=f_regression, k=60)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)
test.fit(X_train, y_train)
X_train = test.transform(X_train)
X_test = test.transform(X_test)
model = LinearRegression()
model.fit(X_train, y_train)
f_regress_score = model.score(X_test, y_test)
print(f_regress_score)<jupyter_output>0.7566701199447428<jupyter_text>Wrapper Method<jupyter_code>from sklearn.feature_selection import RFE
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import ExtraTreesRegressor
nof_list=[20,30,40,50,60,70,80]
high_score=0
nof=0
X = ames.drop(columns="SalePrice", axis=1)
y = ames["SalePrice"].copy()
for n in nof_list:
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 0, test_size=0.3)
    model = LinearRegression()
    rfe = RFE(model, n)
    rfe.fit(X_train, y_train)
    X_train_rfe = rfe.transform(X_train)
    X_test_rfe = rfe.transform(X_test)
    model.fit(X_train_rfe, y_train)
    r2s = model.score(X_test_rfe, y_test)
    if r2s>high_score:
        high_score = r2s
        nof = n
print(high_score, nof)<jupyter_output>0.7627843526086595 30<jupyter_text>Embedded methods<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso
from sklearn.linear_model import Ridge
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0, test_size=0.3)
lasso = Lasso(random_state=0)
lasso.fit(X_train, y_train)
lasso_score = lasso.score(X_test, y_test)
print(lasso_score)
ridge = Ridge(random_state=0)
ridge.fit(X_train, y_train)
ridge_score = ridge.score(X_test, y_test)
print(ridge_score)<jupyter_output>0.67883944067785<jupyter_text>PCA<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
X = ames.drop(columns="SalePrice", axis=1)
y = ames["SalePrice"].copy()
X.shape, y.shape
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
pca = PCA(n_components=35, random_state=0)
X_train_pca = pca.fit_transform(X_train_scaled)
X_test_pca = pca.transform(X_test_scaled)
model = LinearRegression()
model.fit(X_train_pca, y_train)
pca_score = model.score(X_test_pca, y_test)
print(pca_score)<jupyter_output>0.7614586077016585<jupyter_text>Assignment<jupyter_code>path = r"Data\Forest Type Cover.csv"
dataset = pd.read_csv(path)
if "Id" in dataset.columns:
    dataset.drop(columns="Id", axis=1, inplace=True)
print(dataset.head())
print(dataset.describe())
cols = dataset.columns
size = len(cols)-1
size
x = dataset.Cover_Type
y = dataset.drop(columns="Cover_Type", axis=1)
fig, ax = plt.subplots(nrows=18, ncols=3, figsize=[15,66])
for i in range(size):
    sns.violinplot(x,y.iloc[:,i], ax=ax[i//3][i%3])
upper_threshold = 0.5
lower_threshold = -0.5
subset_train = dataset.iloc[:,0:10]
data_corr = subset_train.corr()
data_corr
sns.heatmap(data_corr)
correlation = data_corr.unstack().sort_values(kind='quicksort')
correlation
corr_var_list = correlation[((correlation>upper_threshold) | (correlation<lower_threshold)) & (correlation!=1)]
corr_var_list
dataset.drop(columns=['Soil_Type7', 'Soil_Type15'], inplace=True)
X = dataset.drop(columns="Cover_Type", axis=1)
y = dataset.Cover_Type
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_validate
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0, test_size=0.2)
scaler = StandardScaler()
X_train_temp = scaler.fit_transform(X_train)
X_test_temp = scaler.transform(X_test)
# pd.DataFrame(np.concatenate((X_train_temp, X_train), axis=1))
X_train1 = np.concatenate((X_train_temp, X_train), axis=1)
# X_test1 = pd.concat((X_test, pd.DataFrame(X_test_temp)))
X_test1 = np.concatenate((X_test_temp, X_test), axis=1)
X_train1
X_train1 = np.concatenate((X_train_temp,X_train.iloc[:,size:]),axis=1)
X_test1 = np.concatenate((X_test_temp,X_test.iloc[:,size:]),axis=1)
scaled_features_train_df = pd.DataFrame(X_train1, columns=X_train.columns, index=X_train.index)
scaled_features_test_df = pd.DataFrame(X_test1, columns=X_test.columns, index=X_test.index)
scaled_features_test_df.head()
from sklearn.feature_selection import SelectPercentile
from sklearn.feature_selection import f_classif
skb = SelectPercentile(score_func=f_classif, percentile=90)
predictors = skb.fit_transform(X_train1, y_train)
scores = skb.scores_
scores
Features = X_train.columns
dataframe = pd.concat((pd.DataFrame(Features), pd.DataFrame(scores)), axis=1)
dataframe.columns=["Features", "scores"]
dataframe = dataframe.sort_values(by="scores",ascending=False)
top_k_predictors = list(dataframe.iloc[:int(dataframe.shape[0]*0.9//1)]["Features"])
top_k_predictors
from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, precision_score
clf = OneVsRestClassifier(LogisticRegression())
clf1 = OneVsRestClassifier(LogisticRegression())
model_fit_all_features = clf1.fit(X_train, y_train)
predictions_all_features = model_fit_all_features.predict(X_test)
score_all_features = accuracy_score(y_test, predictions_all_features)
score_all_features
model_fit_top_features = clf.fit(scaled_features_train_df[top_k_predictors], y_train)
predictions_top_features = model_fit_top_features.predict(scaled_features_test_df[top_k_predictors])
score_top_features = accuracy_score(y_test, predictions_top_features)
score_top_features<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12018140589569161, 'fraction_numerical': 0.0248015873015873, 'mean_word_length': 8.421895861148197, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 20, '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': '308540', 'license': 'MIT', 'n_tokens_mistral': 2827, 'n_tokens_neox': 2724, 'n_words': 440, 'path': 'ipynb/Feature Selection.ipynb', 'repo_name': 'abhishekkumaribt/Grey_Atom', 'size': '7056'}