import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC

corpus, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
### BEGIN SOLUTION
svc = LinearSVC(penalty='l1', dual=False)
svc.fit(X, y)
selected_feature_names = np.asarray(vectorizer.get_feature_names_out())[np.flatnonzero(svc.coef_)]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(selected_feature_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import xgboost.sklearn as xgb
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import TimeSeriesSplit

gridsearch, testX, testY, trainX, trainY = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
fit_params = {"early_stopping_rounds": 42,
              "eval_metric": "mae",
              "eval_set": [[testX, testY]]}
gridsearch.fit(trainX, trainY, **fit_params)
### END SOLUTION
b = gridsearch.score(trainX, trainY)
c = gridsearch.predict(trainX)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((b, c), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn.cluster

simM = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
model = sklearn.cluster.AgglomerativeClustering(affinity='precomputed', n_clusters=2, linkage='complete').fit(simM)
cluster_labels = model.labels_

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cluster_labels, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
clf = ExtraTreesClassifier(random_state=42)
clf = clf.fit(X, y)
### BEGIN SOLUTION
model = SelectFromModel(clf, prefit=True)
column_names = X.columns[model.get_support()]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(column_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
cv = StratifiedKFold(5).split(X, y)
logreg = LogisticRegression(random_state=42)
### BEGIN SOLUTION
from sklearn.model_selection import cross_val_predict

proba = cross_val_predict(logreg, X, y, cv=cv, method='predict_proba')
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(proba, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression

# np_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def creat_data():
    df = pd.DataFrame(
        {"Name": ["T-Rex", "Crocodile", "Lion", "Bear", "Tiger", "Hyena", "Jaguar", "Cheetah", "KomodoDragon"],
         "teethLength": [12, 4, 2.7, 3.6, 3, 0.27, 2, 1.5, 0.4],
         "weight": [15432, 2400, 416, 600, 260, 160, 220, 154, 150],
         "length": [40, 23, 9.8, 7, 12, 5, 5.5, 4.9, 8.5],
         "hieght": [20, 1.6, 3.9, 3.35, 3, 2, 2.5, 2.9, 1],
         "speed": [33, 8, 50, 40, 40, 37, 40, 70, 13],
         "Calorie Intake": [40000, 2500, 7236, 20000, 7236, 5000, 5000, 2200, 1994],
         "Bite Force": [12800, 3700, 650, 975, 1050, 1100, 1350, 475, 240],
         "Prey Speed": [20, 30, 35, 0, 37, 20, 15, 56, 24],
         "PreySize": [19841, 881, 1300, 0, 160, 40, 300, 185, 110],
         "EyeSight": [0, 0, 0, 0, 0, 0, 0, 0, 0],
         "Smell": [0, 0, 0, 0, 0, 0, 0, 0, 0],
         "Class": ["Primary Hunter", "Primary Hunter", "Primary Hunter", "Primary Scavenger", "Primary Hunter",
                   "Primary Scavenger", "Primary Hunter", "Primary Hunter", "Primary Scavenger"]})

    filename = "animalData.csv"
    df.to_csv(filename, index=False, sep=",")

creat_data()
filename = "animalData.csv"
dataframe = pd.read_csv(filename, dtype='category')
# dataframe = df
# Git rid of the name of the animal
# And change the hunter/scavenger to 0/1
dataframe = dataframe.drop(["Name"], axis=1)
cleanup = {"Class": {"Primary Hunter": 0, "Primary Scavenger": 1}}
dataframe.replace(cleanup, inplace=True)
### BEGIN SOLUTION
# Seperating the data into dependent and independent variables
X = dataframe.iloc[:, 0:-1].astype(float)
y = dataframe.iloc[:, -1]

logReg = LogisticRegression()
logReg.fit(X[:None], y)
### END SOLUTION
predict = logReg.predict(X)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split

features_dataframe = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
n = features_dataframe.shape[0]
train_size = 0.2
train_dataframe = features_dataframe.iloc[:int(n * train_size)]
test_dataframe = features_dataframe.iloc[int(n * train_size):]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((train_dataframe, test_dataframe), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Transform(df):
### BEGIN SOLUTION
# def Transform(df):
    ### BEGIN SOLUTION
    le = LabelEncoder()
    transformed_df = df.copy()
    transformed_df['Sex'] = le.fit_transform(df['Sex'])
    ### END SOLUTION
    # return transformed_df
# transformed_df = Transform(df)
### END SOLUTION
    return transformed_df
transformed_df = Transform(df)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed_df, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

text = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
vent = CountVectorizer(token_pattern=r"(?u)\b\w\w+\b|!|\?|\"|\'")
transformed_text = vent.fit_transform([text])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed_text, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer

queries, documents = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
tfidf = TfidfVectorizer()
tfidf.fit_transform(documents)
### BEGIN SOLUTION
from sklearn.metrics.pairwise import cosine_similarity

cosine_similarities_of_queries = []
for query in queries:
    query_tfidf = tfidf.transform([query])
    cosine_similarities_of_queries.append(cosine_similarity(query_tfidf, tfidf.transform(documents)).flatten())

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cosine_similarities_of_queries, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.model_selection import GridSearchCV

GridSearch_fitted = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
full_results = pd.DataFrame(GridSearch_fitted.cv_results_)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(full_results, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
le = LabelEncoder()
transformed_df = df.copy()
transformed_df['Sex'] = le.fit_transform(df['Sex'])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed_df, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.svm import SVR

svr_rbf = SVR(kernel='rbf')
svr_rbf.fit(X, y)
predict = svr_rbf.predict(X)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.svm import SVR

svr_poly = SVR(kernel='poly', degree=2)
svr_poly.fit(X, y)
predict = svr_poly.predict(X)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(data.iloc[:, :-1], data.iloc[:, -1], test_size=0.2,
                                                    random_state=42)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((x_train, x_test, y_train, y_test), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

features = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

new_features = MultiLabelBinarizer().fit_transform(features)
rows, cols = new_features.shape
for i in range(rows):
    for j in range(cols):
        if new_features[i, j] == 1:
            new_features[i, j] = 0
        else:
            new_features[i, j] = 1

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_features, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression

df1 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
slopes = []
for col in df1.columns:
    if col == "Time":
        continue
    mask = ~np.isnan(df1[col])
    x = np.atleast_2d(df1.Time[mask].values).T
    y = np.atleast_2d(df1[col][mask].values).T
    reg = LinearRegression().fit(x, y)
    slopes.append(reg.coef_[0])
slopes = np.array(slopes).reshape(-1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(slopes, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.model_selection import GridSearchCV

GridSearch_fitted = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
full_results = pd.DataFrame(GridSearch_fitted.cv_results_).sort_values(by="mean_fit_time")
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(full_results, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

estimators = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
# estimators = [('reduce_dim', PCA()), ('poly', PolynomialFeatures()), ('svm', SVC())]
clf = Pipeline(estimators)
### BEGIN SOLUTION
clf.steps.pop(-1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(len(clf.steps), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()

df_out = df.join(
    pd.DataFrame(
        mlb.fit_transform(df.pop('Col4')),
        index=df.index,
        columns=mlb.classes_))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np

dataset = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(data):
### BEGIN SOLUTION
# def solve(data):
    ### BEGIN SOLUTION
    from sklearn.model_selection import train_test_split

    x_train, x_test, y_train, y_test = train_test_split(data.iloc[:, :-1], data.iloc[:, -1], test_size=0.2,
                                                        random_state=42)
    ### END SOLUTION
    # return x_train, y_train, x_test, y_test
# x_train, y_train, x_test, y_test = solve(data)

### END SOLUTION
    return x_train, y_train, x_test, y_test
x_train, y_train, x_test, y_test = solve(dataset)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((x_train, x_test, y_train, y_test), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn import preprocessing

pt = preprocessing.PowerTransformer(method="yeo-johnson")
yeo_johnson_data = pt.fit_transform(data)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(yeo_johnson_data, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn.cluster

data_matrix = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
model = sklearn.cluster.AgglomerativeClustering(affinity='precomputed', n_clusters=2, linkage='complete').fit(data_matrix)
cluster_labels = model.labels_

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cluster_labels, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
kmeans = KMeans(n_clusters=2)
labels = kmeans.fit_predict(df[['mse']])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(labels, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier

X = [['asdf', '1'], ['asdf', '0']]
clf = DecisionTreeClassifier()
### BEGIN SOLUTION
from sklearn.feature_extraction import DictVectorizer

X = [dict(enumerate(x)) for x in X]
vect = DictVectorizer(sparse=False)
new_X = vect.fit_transform(X)
### END SOLUTION
clf.fit(new_X, ['2', '3'])
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_X, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

words = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
count = CountVectorizer(lowercase=False, token_pattern='[a-zA-Z0-9$&+:;=@#|<>^*()%-]+')
vocabulary = count.fit_transform([words])
feature_names = count.get_feature_names_out()
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(feature_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans

p, X = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
km = KMeans(n_clusters=8, random_state=42)
def get_samples(p, X, km):
    # calculate the closest 50 samples
### BEGIN SOLUTION
# def get_samples(p, X, km):
    # calculate the closest 50 samples
    ### BEGIN SOLUTION
    km.fit(X)
    d = km.transform(X)[:, p]
    indexes = np.argsort(d)[::][:50]
    samples = X[indexes]
    ### END SOLUTION
    # return samples
# closest_50_samples = get_samples(p, X, km)

### END SOLUTION
    return samples
closest_50_samples = get_samples(p, X, km)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(closest_50_samples, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

# corpus, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
corpus = [
    'We are looking for Java developer',
    'Frontend developer with knowledge in SQL and Jscript',
    'And this is the third one.',
    'Is this the first document?',
]
### BEGIN SOLUTION
vectorizer = CountVectorizer(stop_words="english", binary=True, lowercase=False,
                             vocabulary=['Jscript', '.Net', 'TypeScript', 'SQL', 'NodeJS', 'Angular', 'Mongo',
                                         'CSS',
                                         'Python', 'PHP', 'Photoshop', 'Oracle', 'Linux', 'C++', "Java", 'TeamCity',
                                         'Frontend', 'Backend', 'Full stack', 'UI Design', 'Web', 'Integration',
                                         'Database design', 'UX'])

X = vectorizer.fit_transform(corpus).toarray()
X = 1 - X
feature_names = vectorizer.get_feature_names_out()
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((feature_names, X), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_selection import SelectKBest
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline

data, target = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
pipe = Pipeline(steps=[
    ('select', SelectKBest(k=2)),
    ('clf', LogisticRegression())]
)
### BEGIN SOLUTION
select_out = pipe.named_steps['select'].fit_transform(data, target)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(select_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.ensemble import GradientBoostingClassifier

X_train, y_train = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
X_train[0] = ['a'] * 40 + ['b'] * 40
### BEGIN SOLUTION
catVar = pd.get_dummies(X_train[0]).to_numpy()
X_train = np.concatenate((X_train.iloc[:, 1:], catVar), axis=1)

### END SOLUTION
clf = GradientBoostingClassifier(learning_rate=0.01, max_depth=8, n_estimators=50).fit(X_train, y_train)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(X_train, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler

# features_dataframe = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
myData = pd.DataFrame({
    'Month': [3, 3, 3, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8],
    'A1': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2],
    'A2': [31, 13, 13, 13, 33, 33, 81, 38, 18, 38, 18, 18, 118],
    'A3': [81, 38, 18, 38, 18, 18, 118, 31, 13, 13, 13, 33, 33],
    'A4': [1, 1, 1, 1, 1, 1, 8, 8, 8, 8, 8, 8, 8],
})
scaler = MinMaxScaler()
### BEGIN SOLUTION
cols = myData.columns[2:4]

def scale(X):
    X_ = np.atleast_2d(X)
    return pd.DataFrame(scaler.fit_transform(X_), X.index)

myData['new_' + cols] = myData.groupby('Month')[cols].apply(scale)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(myData, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor

X, y, X_test = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
regressor = RandomForestRegressor(n_estimators=150, min_samples_split=1.0, random_state=42)
regressor.fit(X.reshape(-1, 1), y)
### END SOLUTION
predict = regressor.predict(X_test)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
clf = ExtraTreesClassifier(random_state=42)
clf = clf.fit(X, y)
### BEGIN SOLUTION
model = SelectFromModel(clf, prefit=True)
column_names = X.columns[model.get_support()]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(column_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import scipy.cluster

data_matrix = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
Z = scipy.cluster.hierarchy.linkage(np.array(data_matrix), 'ward')
cluster_labels = scipy.cluster.hierarchy.cut_tree(Z, n_clusters=2).reshape(-1, ).tolist()
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cluster_labels, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

features = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(features):
### BEGIN SOLUTION
# def solve(features):
    ### BEGIN SOLUTION
    from sklearn.preprocessing import MultiLabelBinarizer

    new_features = MultiLabelBinarizer().fit_transform(features)
    ### END SOLUTION
    # return new_features
# new_features = solve(features)

### END SOLUTION
    return new_features
new_features = solve(features)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_features, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.svm import SVR

svr_poly = SVR(kernel='poly', degree=2)
svr_poly.fit(X, y)
predict = svr_poly.predict(X)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.svm import SVR

svr_rbf = SVR(kernel='rbf')
svr_rbf.fit(X, y)
predict = svr_rbf.predict(X)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn import tree
import pandas_datareader as web

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))

df['B/S'] = (df['Close'].diff() < 0).astype(int)

closing = (df.loc['2013-02-15':'2016-05-21'])
ma_50 = (df.loc['2013-02-15':'2016-05-21'])
ma_100 = (df.loc['2013-02-15':'2016-05-21'])
ma_200 = (df.loc['2013-02-15':'2016-05-21'])
buy_sell = (df.loc['2013-02-15':'2016-05-21'])  # Fixed

close = pd.DataFrame(closing)
ma50 = pd.DataFrame(ma_50)
ma100 = pd.DataFrame(ma_100)
ma200 = pd.DataFrame(ma_200)
buy_sell = pd.DataFrame(buy_sell)

clf = tree.DecisionTreeRegressor(random_state=42)
x = np.concatenate([close, ma50, ma100, ma200], axis=1)
y = buy_sell

clf.fit(x, y)
### BEGIN SOLUTION
close_buy1 = close[:-1]
m5 = ma_50[:-1]
m10 = ma_100[:-1]
ma20 = ma_200[:-1]
# b = np.concatenate([close_buy1, m5, m10, ma20], axis=1)

predict = clf.predict(pd.concat([close_buy1, m5, m10, ma20], axis=1))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.svm import LinearSVC

model = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
model_name = type(model).__name__
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(model_name, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression

# np_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def creat_data():
    df = pd.DataFrame(
        {"Name": ["T-Rex", "Crocodile", "Lion", "Bear", "Tiger", "Hyena", "Jaguar", "Cheetah", "KomodoDragon"],
         "teethLength": [12, 4, 2.7, 3.6, 3, 0.27, 2, 1.5, 0.4],
         "weight": [15432, 2400, 416, 600, 260, 160, 220, 154, 150],
         "length": [40, 23, 9.8, 7, 12, 5, 5.5, 4.9, 8.5],
         "hieght": [20, 1.6, 3.9, 3.35, 3, 2, 2.5, 2.9, 1],
         "speed": [33, 8, 50, 40, 40, 37, 40, 70, 13],
         "Calorie Intake": [40000, 2500, 7236, 20000, 7236, 5000, 5000, 2200, 1994],
         "Bite Force": [12800, 3700, 650, 975, 1050, 1100, 1350, 475, 240],
         "Prey Speed": [20, 30, 35, 0, 37, 20, 15, 56, 24],
         "PreySize": [19841, 881, 1300, 0, 160, 40, 300, 185, 110],
         "EyeSight": [0, 0, 0, 0, 0, 0, 0, 0, 0],
         "Smell": [0, 0, 0, 0, 0, 0, 0, 0, 0],
         "Class": ["Primary Hunter", "Primary Hunter", "Primary Hunter", "Primary Scavenger", "Primary Hunter",
                   "Primary Scavenger", "Primary Hunter", "Primary Hunter", "Primary Scavenger"]})

    filename = "animalData.csv"
    df.to_csv(filename, index=False, sep=",")

creat_data()
filename = "animalData.csv"
dataframe = pd.read_csv(filename, dtype='category')
# dataframe = df
# Git rid of the name of the animal
# And change the hunter/scavenger to 0/1
dataframe = dataframe.drop(["Name"], axis=1)
cleanup = {"Class": {"Primary Hunter": 0, "Primary Scavenger": 1}}
dataframe.replace(cleanup, inplace=True)
### BEGIN SOLUTION
# Seperating the data into dependent and independent variables
X = dataframe.iloc[:, 0:-1].astype(float)
y = dataframe.iloc[:, -1]

logReg = LogisticRegression()
logReg.fit(X[:None], y)
### END SOLUTION
predict = logReg.predict(X)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer

# X, y, X_test = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
def prePro(s):
    return s.lower()

tfidf = TfidfVectorizer(preprocessor=prePro)

### END SOLUTION
try:
    assert prePro("asdfASDFASDFWEQRqwerASDFAqwerASDFASDF") == "asdfasdfasdfweqrqwerasdfaqwerasdfasdf"
    assert prePro == tfidf.preprocessor
    with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
        pickle.dump("accept", f)
except:
    with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
        pickle.dump("reject", f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn import preprocessing

pt = preprocessing.PowerTransformer(method="box-cox")
box_cox_data = pt.fit_transform(data)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(box_cox_data, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

# corpus, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
corpus = [
    'We are looking for Java developer',
    'Frontend developer with knowledge in SQL and Jscript',
    'And this is the third one.',
    'Is this the first document?',
]
### BEGIN SOLUTION
vectorizer = CountVectorizer(stop_words="english", binary=True, lowercase=False,
                             vocabulary=['Jscript', '.Net', 'TypeScript', 'SQL', 'NodeJS', 'Angular', 'Mongo',
                                         'CSS',
                                         'Python', 'PHP', 'Photoshop', 'Oracle', 'Linux', 'C++', "Java", 'TeamCity',
                                         'Frontend', 'Backend', 'Full stack', 'UI Design', 'Web', 'Integration',
                                         'Database design', 'UX'])
X = vectorizer.fit_transform(corpus).toarray()
feature_names = vectorizer.get_feature_names_out()
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((feature_names, X), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
scaler = StandardScaler()
scaler.fit(data)
scaled = scaler.transform(data)
### BEGIN SOLUTION
inversed = scaler.inverse_transform(scaled)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(inversed, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler

np_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
scaler = MinMaxScaler()
X_one_column = np_array.reshape([-1, 1])
result_one_column = scaler.fit_transform(X_one_column)
transformed = result_one_column.reshape(np_array.shape)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression

df1 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
slopes = []
for col in df1.columns:
    if col == "Time":
        continue
    mask = ~np.isnan(df1[col])
    x = np.atleast_2d(df1.Time[mask].values).T
    y = np.atleast_2d(df1[col][mask].values).T
    reg = LinearRegression().fit(x, y)
    slopes.append(reg.coef_[0])
slopes = np.array(slopes).reshape(-1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(slopes, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn import svm

X, y, x_predict = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
model = svm.LinearSVC(random_state=42)
### BEGIN SOLUTION
from sklearn.calibration import CalibratedClassifierCV

calibrated_svc = CalibratedClassifierCV(model, cv=5, method='sigmoid')
calibrated_svc.fit(X, y)
proba = calibrated_svc.predict_proba(x_predict)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(proba, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression

model = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
model_name = type(model).__name__
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(model_name, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import NMF
from sklearn.pipeline import Pipeline

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
pipe = Pipeline([
    ("tf_idf", TfidfVectorizer()),
    ("nmf", NMF())
])
### BEGIN SOLUTION
pipe.fit_transform(data.test)
tf_idf_out = pipe.named_steps['tf_idf'].transform(data.test)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(tf_idf_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from scipy.sparse import csr_matrix

df_origin, transform_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(df, transform_output):
### BEGIN SOLUTION
# def solve(df, transform_output):
    ### BEGIN SOLUTION
    result = pd.concat([df, pd.DataFrame(transform_output.toarray())], axis=1)
    ### END SOLUTION
    # return result
# df = solve(df_origin, transform_output)

### END SOLUTION
    return result
df = solve(df_origin, transform_output)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()

df_out = df.join(
    pd.DataFrame(
        mlb.fit_transform(df.pop('Col3')),
        index=df.index,
        columns=mlb.classes_))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
scaler = StandardScaler()
scaler.fit(data)
scaled = scaler.transform(data)
def solve(data, scaler, scaled):
### BEGIN SOLUTION
# def solve(data, scaler, scaled):
    ### BEGIN SOLUTION
    inversed = scaler.inverse_transform(scaled)
    ### END SOLUTION
    # return inversed
# inversed = solve(data, scaler, scaled)

### END SOLUTION
    return inversed
inversed = solve(data, scaler, scaled)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(inversed, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

estimators = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
# estimators = [('reduce_poly', PolynomialFeatures()), ('dim_svm', PCA()), ('sVm_233', SVC())]
clf = Pipeline(estimators)
### BEGIN SOLUTION
clf.steps.pop(-1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(len(clf.steps), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(data):
### BEGIN SOLUTION
# def solve(data):
    ### BEGIN SOLUTION
    result = pd.DataFrame(data=np.c_[data['data'], data['target']], columns=data['feature_names'] + ['target'])
    ### END SOLUTION
    # return result
# data1 = solve(data)

### END SOLUTION
    return result
data1 = solve(data)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(data1, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import scipy.cluster

simM = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
Z = scipy.cluster.hierarchy.linkage(np.array(simM), 'ward')
cluster_labels = scipy.cluster.hierarchy.cut_tree(Z, n_clusters=2).reshape(-1, ).tolist()
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cluster_labels, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier

X = [['asdf', '1'], ['asdf', '0']]
clf = DecisionTreeClassifier()
### BEGIN SOLUTION
from sklearn.feature_extraction import DictVectorizer

X = [dict(enumerate(x)) for x in X]
vect = DictVectorizer(sparse=False)
new_X = vect.fit_transform(X)
### END SOLUTION
clf.fit(new_X, ['2', '3'])
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_X, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split

features_dataframe = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
n = features_dataframe.shape[0]
train_size = 0.8
test_size = 1 - train_size + 0.005
train_dataframe = features_dataframe.iloc[int(n * test_size):]
test_dataframe = features_dataframe.iloc[:int(n * test_size)]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((train_dataframe, test_dataframe), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

features = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

new_features = MultiLabelBinarizer().fit_transform(features)
rows, cols = new_features.shape
for i in range(rows):
    for j in range(cols):
        if new_features[i, j] == 1:
            new_features[i, j] = 0
        else:
            new_features[i, j] = 1

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_features, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn import preprocessing

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
df_out = pd.DataFrame(preprocessing.scale(data), index=data.index, columns=data.columns)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
clf = ExtraTreesClassifier(random_state=42)
clf = clf.fit(X, y)
### BEGIN SOLUTION
model = SelectFromModel(clf, prefit=True)
column_names = X.columns[model.get_support()]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(column_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC

corpus, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
### BEGIN SOLUTION
svc = LinearSVC(penalty='l1', dual=False)
svc.fit(X, y)
selected_feature_names = np.asarray(vectorizer.get_feature_names_out())[np.flatnonzero(svc.coef_)]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(selected_feature_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor

X, y, X_test = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
regressor = RandomForestRegressor(n_estimators=150, min_samples_split=1.0, random_state=42)
regressor.fit(X.reshape(-1, 1), y)
### END SOLUTION
predict = regressor.predict(X_test)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np

dataset = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(dataset.iloc[:, :-1], dataset.iloc[:, -1], test_size=0.4,
                                                    random_state=42)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((x_train, x_test, y_train, y_test), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler

# features_dataframe = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
df = pd.DataFrame({
    'Month': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2],
    'X1': [12, 10, 100, 55, 65, 60, 35, 25, 10, 15, 30, 40, 50],
    'X2': [10, 15, 24, 32, 8, 6, 10, 23, 24, 56, 45, 10, 56],
    'X3': [12, 90, 20, 40, 10, 15, 30, 40, 60, 42, 2, 4, 10]
})
scaler = MinMaxScaler()
### BEGIN SOLUTION
cols = df.columns[2:4]

def scale(X):
    X_ = np.atleast_2d(X)
    return pd.DataFrame(scaler.fit_transform(X_), X.index)

df[cols + '_scale'] = df.groupby('Month')[cols].apply(scale)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()

df_out = df.join(
    pd.DataFrame(
        mlb.fit_transform(df.pop(df.columns[-1])),
        index=df.index,
        columns=mlb.classes_))
for idx in df_out.index:
    for col in mlb.classes_:
        df_out.loc[idx, col] = 1 - df_out.loc[idx, col]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn.cluster

data_matrix = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
model = sklearn.cluster.AgglomerativeClustering(affinity='precomputed', n_clusters=2, linkage='complete').fit(data_matrix)
cluster_labels = model.labels_

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cluster_labels, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

f = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

new_f = MultiLabelBinarizer().fit_transform(f)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as _f:
    pickle.dump(new_f, _f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression

model = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
model_name = type(model).__name__
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(model_name, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

features = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

new_features = MultiLabelBinarizer().fit_transform(features)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_features, f)


import argparse
import os

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np

if os.path.exists("sklearn_model"):
    os.remove("sklearn_model")
def creat():
    import pickle

    fitted_model = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
    return fitted_model
fitted_model = creat()
### BEGIN SOLUTION
import pickle

with open('sklearn_model', 'wb') as f:
    pickle.dump(fitted_model, f)

### END SOLUTION
if os.path.exists("sklearn_model") and not os.path.isdir("sklearn_model"):
    import pickle

    with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
        pickle.dump("accept", f)
else:
    import pickle

    with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
        pickle.dump("reject", f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
tfidf = TfidfVectorizer()
### BEGIN SOLUTION
from sklearn.metrics.pairwise import cosine_similarity

response = tfidf.fit_transform(df['description']).toarray()
tf_idf = response
cosine_similarity_matrix = np.zeros((len(df), len(df)))
for i in range(len(df)):
    for j in range(len(df)):
        cosine_similarity_matrix[i, j] = cosine_similarity([tf_idf[i, :]], [tf_idf[j, :]])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cosine_similarity_matrix, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

# corpus, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
corpus = [
    'We are looking for Java developer',
    'Frontend developer with knowledge in SQL and Jscript',
    'And this is the third one.',
    'Is this the first document?',
]
### BEGIN SOLUTION
vectorizer = CountVectorizer(stop_words="english", binary=True, lowercase=False,
                             vocabulary=['Jscript', '.Net', 'TypeScript', 'NodeJS', 'Angular', 'Mongo',
                                         'CSS',
                                         'Python', 'PHP', 'Photoshop', 'Oracle', 'Linux', 'C++', "Java", 'TeamCity',
                                         'Frontend', 'Backend', 'Full stack', 'UI Design', 'Web', 'Integration',
                                         'Database design', 'UX'])

X = vectorizer.fit_transform(corpus).toarray()
X = 1 - X
feature_names = vectorizer.get_feature_names_out()

### END SOLUTION
feature_names = vectorizer.get_feature_names_out()
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((feature_names, X), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans

p, X = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
km = KMeans(n_clusters=8, random_state=42)
### BEGIN SOLUTION
km.fit(X)
d = km.transform(X)[:, p]
indexes = np.argsort(d)[::][:100]
closest_100_samples = X[indexes]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(closest_100_samples, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

# corpus, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
corpus = [
    'We are looking for Java developer',
    'Frontend developer with knowledge in SQL and Jscript',
    'And this is the third one.',
    'Is this the first document?',
]
### BEGIN SOLUTION
vectorizer = CountVectorizer(stop_words="english", binary=True, lowercase=False,
                             vocabulary=['Jscript', '.Net', 'TypeScript', 'NodeJS', 'Angular', 'Mongo',
                                         'CSS',
                                         'Python', 'PHP', 'Photoshop', 'Oracle', 'Linux', 'C++', "Java", 'TeamCity',
                                         'Frontend', 'Backend', 'Full stack', 'UI Design', 'Web', 'Integration',
                                         'Database design', 'UX'])
X = vectorizer.fit_transform(corpus).toarray()
feature_names = vectorizer.get_feature_names_out()

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((feature_names, X), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans

p, X = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
km = KMeans(n_clusters=8, random_state=42)
### BEGIN SOLUTION
km.fit(X)
d = km.transform(X)[:, p]
indexes = np.argsort(d)[::][:50]
closest_50_samples = X[indexes]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(closest_50_samples, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn.svm as suppmach

X, y, x_test = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
svmmodel=suppmach.LinearSVC(random_state=42)
### BEGIN SOLUTION
from sklearn.calibration import CalibratedClassifierCV

calibrated_svc = CalibratedClassifierCV(svmmodel, cv=5, method='sigmoid')
calibrated_svc.fit(X, y)
proba = calibrated_svc.predict_proba(x_test)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(proba, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn import preprocessing

pt = preprocessing.PowerTransformer(method="yeo-johnson")
yeo_johnson_data = pt.fit_transform(data)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(yeo_johnson_data, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

estimators = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
# estimators = [('reduce_dim', PCA()), ('poly', PolynomialFeatures()), ('svm', SVC())]
clf = Pipeline(estimators)
### BEGIN SOLUTION
clf.steps.insert(0, ('reduce_dim', PCA()))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(len(clf.steps), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from scipy.sparse import csr_matrix

df_origin, transform_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
df = pd.concat([df_origin, pd.DataFrame(transform_output.toarray())], axis=1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np

dataset = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(dataset.iloc[:, :-1], dataset.iloc[:, -1], test_size=0.2,
                                                    random_state=42)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((x_train, x_test, y_train, y_test), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

words = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
count = CountVectorizer(lowercase=False, token_pattern='[a-zA-Z0-9$&+:;=@#|<>^*()%-]+')
vocabulary = count.fit_transform([words])
feature_names = count.get_feature_names_out()
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(feature_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC

corpus, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
def solve(corpus, y, vectorizer, X):
### BEGIN SOLUTION
# def solve(corpus, y, vectorizer, X):
    ### BEGIN SOLUTION
    svc = LinearSVC(penalty='l1', dual=False)
    svc.fit(X, y)
    selected_feature_names = np.asarray(vectorizer.get_feature_names_out())[np.flatnonzero(svc.coef_)]
    ### END SOLUTION
    # return selected_feature_names
# selected_feature_names = solve(corpus, y, vectorizer, X)
    return selected_feature_names

### END SOLUTION
selected_feature_names = solve(corpus, y, vectorizer, X)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(selected_feature_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn import preprocessing

centered_scaled_data = preprocessing.scale(data)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(centered_scaled_data, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.ensemble import GradientBoostingClassifier

X_train, y_train = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
X_train[0] = ['a'] * 40 + ['b'] * 40
### BEGIN SOLUTION
catVar = pd.get_dummies(X_train[0]).to_numpy()
X_train = np.concatenate((X_train.iloc[:, 1:], catVar), axis=1)

### END SOLUTION
clf = GradientBoostingClassifier(learning_rate=0.01, max_depth=8, n_estimators=50).fit(X_train, y_train)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(X_train, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
data1 = pd.DataFrame(data=np.c_[data['data'], data['target']], columns=data['feature_names'] + ['target'])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(data1, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler

np_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Transform(a):
### BEGIN SOLUTION
# def Transform(a):
    ### BEGIN SOLUTION
    scaler = MinMaxScaler()
    a_one_column = a.reshape([-1, 1])
    result_one_column = scaler.fit_transform(a_one_column)
    new_a = result_one_column.reshape(a.shape)
    ### END SOLUTION
    # return new_a
# transformed = Transform(np_array)

    return new_a

### END SOLUTION
transformed = Transform(np_array)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import scipy.cluster

data_matrix = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
Z = scipy.cluster.hierarchy.linkage(np.array(data_matrix), 'ward')
cluster_labels = scipy.cluster.hierarchy.cut_tree(Z, n_clusters=2).reshape(-1, ).tolist()
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cluster_labels, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import RidgeClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
pipe = Pipeline([
    ("scale", StandardScaler()),
    ("model", RidgeClassifier(random_state=24))
])
grid = GridSearchCV(pipe, param_grid={"model__alpha": [2e-4, 3e-3, 4e-2, 5e-1]}, cv=7)
### BEGIN SOLUTION
grid.fit(X, y)
coef = grid.best_estimator_.named_steps['model'].coef_

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(coef, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()

df_out = df.join(
    pd.DataFrame(
        mlb.fit_transform(df.pop('Col3')),
        index=df.index,
        columns=mlb.classes_))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier

X = [['dsa', '2'], ['sato', '3']]
clf = DecisionTreeClassifier()
### BEGIN SOLUTION
from sklearn.feature_extraction import DictVectorizer

X = [dict(enumerate(x)) for x in X]
vect = DictVectorizer(sparse=False)
new_X = vect.fit_transform(X)
### END SOLUTION
clf.fit(new_X, ['4', '5'])
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_X, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler

np_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
scaler = MinMaxScaler()
X_one_column = np_array.reshape([-1, 1])
result_one_column = scaler.fit_transform(X_one_column)
transformed = result_one_column.reshape(np_array.shape)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

estimators = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
# estimators = [('reduce_dIm', PCA()), ('pOly', PolynomialFeatures()), ('svdm', SVC())]
clf = Pipeline(estimators)
### BEGIN SOLUTION
clf.steps.insert(2, ('t1919810', PCA()))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(str(clf.named_steps), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

estimators = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
# estimators = [('reduce_dIm', PCA()), ('pOly', PolynomialFeatures()), ('svdm', SVC())]
clf = Pipeline(estimators)
### BEGIN SOLUTION
clf.steps.pop(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(str(clf.named_steps), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

estimators = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
# estimators = [('reduce_poly', PolynomialFeatures()), ('dim_svm', PCA()), ('sVm_233', SVC())]
clf = Pipeline(estimators)
### BEGIN SOLUTION
clf.steps.insert(0, ('reduce_dim', PCA()))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(len(clf.steps), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()

df_out = df.join(
    pd.DataFrame(
        mlb.fit_transform(df.pop(df.columns[-1])),
        index=df.index,
        columns=mlb.classes_))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import NMF
from sklearn.pipeline import Pipeline

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
pipe = Pipeline([
    ("tf_idf", TfidfVectorizer()),
    ("nmf", NMF())
])
### BEGIN SOLUTION
pipe.fit_transform(data.test)
tf_idf_out = pipe.named_steps['tf_idf'].transform(data.test)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(tf_idf_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
cv = StratifiedKFold(5).split(X, y)
logreg = LogisticRegression(random_state=42)
### BEGIN SOLUTION
from sklearn.model_selection import cross_val_predict

proba = cross_val_predict(logreg, X, y, cv=cv, method='predict_proba')
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(proba, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn import preprocessing

pt = preprocessing.PowerTransformer(method="box-cox")
box_cox_data = pt.fit_transform(data)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(box_cox_data, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

pipe = Pipeline([
    ("scale", StandardScaler()),
    ("model", SGDClassifier(random_state=42))
])
grid = GridSearchCV(pipe, param_grid={"model__alpha": [1e-3, 1e-2, 1e-1, 1]}, cv=5)
X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
grid.fit(X, y)
coef = grid.best_estimator_.named_steps['model'].coef_

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(coef, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
clf = ExtraTreesClassifier(random_state=42)
clf = clf.fit(X, y)
### BEGIN SOLUTION
model = SelectFromModel(clf, prefit=True)
column_names = list(X.columns[model.get_support()])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(column_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from scipy.sparse import csr_matrix

df_origin, transform_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
df = pd.concat([df_origin, pd.DataFrame(transform_output.toarray())], axis=1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.ensemble import BaggingClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier

X_train, y_train = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
X_test = X_train
param_grid = {
    'base_estimator__max_depth': [1, 2, 3, 4, 5],
    'max_samples': [0.05, 0.1, 0.2, 0.5]
}
dt = DecisionTreeClassifier(max_depth=1, random_state=42)
bc = BaggingClassifier(dt, n_estimators=20, max_samples=0.5, max_features=0.5, random_state=42)
### BEGIN SOLUTION
clf = GridSearchCV(bc, param_grid)
clf.fit(X_train, y_train)

### END SOLUTION
proba = clf.predict_proba(X_test)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(proba, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import xgboost.sklearn as xgb
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import TimeSeriesSplit

gridsearch, testX, testY, trainX, trainY = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
fit_params = {"early_stopping_rounds": 42,
              "eval_metric": "mae",
              "eval_set": [[testX, testY]]}
gridsearch.fit(trainX, trainY, **fit_params)
### END SOLUTION
b = gridsearch.score(trainX, trainY)
c = gridsearch.predict(trainX)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((b, c), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
data1 = pd.DataFrame(data=np.c_[data['data'], data['target']], columns=data['feature_names'] + ['target'])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(data1, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer

# X, y, X_test = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
def preprocess(s):
    return s.upper()

tfidf = TfidfVectorizer(preprocessor=preprocess)

### END SOLUTION
try:
    assert preprocess("asdfASDFASDFWEQRqwerASDFAqwerASDFASDF") == "ASDFASDFASDFWEQRQWERASDFAQWERASDFASDF"
    assert preprocess == tfidf.preprocessor
    with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
        pickle.dump("accept", f)
except:
    with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
        pickle.dump("reject", f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans

p, X = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
km = KMeans(n_clusters=8, random_state=42)
### BEGIN SOLUTION
km.fit(X)
d = km.transform(X)[:, p]
indexes = np.argsort(d)[::][:50]
closest_50_samples = X[indexes]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(closest_50_samples, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn import preprocessing

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
df_out = pd.DataFrame(preprocessing.scale(data), index=data.index, columns=data.columns)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.datasets import load_boston

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
data1 = pd.DataFrame(data.data, columns=data.feature_names)
data1['target'] = pd.Series(data.target)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(data1, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split

features_dataframe = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(features_dataframe):
### BEGIN SOLUTION
# def solve(features_dataframe):
    ### BEGIN SOLUTION
    n = features_dataframe.shape[0]
    train_size = 0.2
    train_dataframe = features_dataframe.iloc[:int(n * train_size)]
    test_dataframe = features_dataframe.iloc[int(n * train_size):]
    ### END SOLUTION
    # return train_dataframe, test_dataframe
# train_dataframe, test_dataframe = solve(features_dataframe)
    return train_dataframe, test_dataframe

### END SOLUTION
train_dataframe, test_dataframe = solve(features_dataframe)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((train_dataframe, test_dataframe), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
le = LabelEncoder()
transformed_df = df.copy()
transformed_df['Sex'] = le.fit_transform(df['Sex'])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed_df, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn import linear_model
import statsmodels.api as sm

X_train, y_train, X_test, y_test = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
ElasticNet = linear_model.ElasticNet()
ElasticNet.fit(X_train, y_train)
training_set_score = ElasticNet.score(X_train, y_train)
test_set_score = ElasticNet.score(X_test, y_test)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((training_set_score, test_set_score), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer

queries, documents = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(queries, documents):
    tfidf = TfidfVectorizer()
    tfidf.fit_transform(documents)
### BEGIN SOLUTION
# def solve(queries, documents):
    ### BEGIN SOLUTION
    from sklearn.metrics.pairwise import cosine_similarity

    cosine_similarities_of_queries = []
    for query in queries:
        query_tfidf = tfidf.transform([query])
        cosine_similarities_of_queries.append(cosine_similarity(query_tfidf, tfidf.transform(documents)).flatten())
    ### END SOLUTION
    # return cosine_similarities_of_queries
# cosine_similarities_of_queries = solve(queries, documents)

    return cosine_similarities_of_queries

### END SOLUTION
cosine_similarities_of_queries = solve(queries, documents)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cosine_similarities_of_queries, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC

corpus, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
### BEGIN SOLUTION
svc = LinearSVC(penalty='l1', dual=False)
svc.fit(X, y)
selected_feature_names = np.asarray(vectorizer.get_feature_names_out())[np.flatnonzero(svc.coef_)]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(selected_feature_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn.cluster

simM = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
model = sklearn.cluster.AgglomerativeClustering(affinity='precomputed', n_clusters=2, linkage='complete').fit(simM)
cluster_labels = model.labels_

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cluster_labels, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
clf = ExtraTreesClassifier(random_state=42)
clf = clf.fit(X, y)
### BEGIN SOLUTION
model = SelectFromModel(clf, prefit=True)
column_names = X.columns[model.get_support()]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(column_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
cv = StratifiedKFold(5).split(X, y)
logreg = LogisticRegression(random_state=42)
### BEGIN SOLUTION
from sklearn.model_selection import cross_val_predict

proba = cross_val_predict(logreg, X, y, cv=cv, method='predict_proba')
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(proba, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression

# np_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def creat_data():
    df = pd.DataFrame(
        {"Name": ["T-Rex", "Crocodile", "Lion", "Bear", "Tiger", "Hyena", "Jaguar", "Cheetah", "KomodoDragon"],
         "teethLength": [12, 4, 2.7, 3.6, 3, 0.27, 2, 1.5, 0.4],
         "weight": [15432, 2400, 416, 600, 260, 160, 220, 154, 150],
         "length": [40, 23, 9.8, 7, 12, 5, 5.5, 4.9, 8.5],
         "hieght": [20, 1.6, 3.9, 3.35, 3, 2, 2.5, 2.9, 1],
         "speed": [33, 8, 50, 40, 40, 37, 40, 70, 13],
         "Calorie Intake": [40000, 2500, 7236, 20000, 7236, 5000, 5000, 2200, 1994],
         "Bite Force": [12800, 3700, 650, 975, 1050, 1100, 1350, 475, 240],
         "Prey Speed": [20, 30, 35, 0, 37, 20, 15, 56, 24],
         "PreySize": [19841, 881, 1300, 0, 160, 40, 300, 185, 110],
         "EyeSight": [0, 0, 0, 0, 0, 0, 0, 0, 0],
         "Smell": [0, 0, 0, 0, 0, 0, 0, 0, 0],
         "Class": ["Primary Hunter", "Primary Hunter", "Primary Hunter", "Primary Scavenger", "Primary Hunter",
                   "Primary Scavenger", "Primary Hunter", "Primary Hunter", "Primary Scavenger"]})

    filename = "animalData.csv"
    df.to_csv(filename, index=False, sep=",")

creat_data()
filename = "animalData.csv"
dataframe = pd.read_csv(filename, dtype='category')
# dataframe = df
# Git rid of the name of the animal
# And change the hunter/scavenger to 0/1
dataframe = dataframe.drop(["Name"], axis=1)
cleanup = {"Class": {"Primary Hunter": 0, "Primary Scavenger": 1}}
dataframe.replace(cleanup, inplace=True)
### BEGIN SOLUTION
# Seperating the data into dependent and independent variables
X = dataframe.iloc[:, 0:-1].astype(float)
y = dataframe.iloc[:, -1]

logReg = LogisticRegression()
logReg.fit(X[:None], y)
### END SOLUTION
predict = logReg.predict(X)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split

features_dataframe = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
n = features_dataframe.shape[0]
train_size = 0.2
train_dataframe = features_dataframe.iloc[:int(n * train_size)]
test_dataframe = features_dataframe.iloc[int(n * train_size):]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((train_dataframe, test_dataframe), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Transform(df):
### BEGIN SOLUTION
# def Transform(df):
    ### BEGIN SOLUTION
    le = LabelEncoder()
    transformed_df = df.copy()
    transformed_df['Sex'] = le.fit_transform(df['Sex'])
    ### END SOLUTION
    # return transformed_df
# transformed_df = Transform(df)
    return transformed_df

### END SOLUTION
transformed_df = Transform(df)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed_df, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

text = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
vent = CountVectorizer(token_pattern=r"(?u)\b\w\w+\b|!|\?|\"|\'")
transformed_text = vent.fit_transform([text])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed_text, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer

queries, documents = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
tfidf = TfidfVectorizer()
tfidf.fit_transform(documents)
### BEGIN SOLUTION
from sklearn.metrics.pairwise import cosine_similarity

cosine_similarities_of_queries = []
for query in queries:
    query_tfidf = tfidf.transform([query])
    cosine_similarities_of_queries.append(cosine_similarity(query_tfidf, tfidf.transform(documents)).flatten())

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cosine_similarities_of_queries, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.model_selection import GridSearchCV

GridSearch_fitted = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
full_results = pd.DataFrame(GridSearch_fitted.cv_results_)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(full_results, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.svm import SVR

svr_rbf = SVR(kernel='rbf')
svr_rbf.fit(X, y)
predict = svr_rbf.predict(X)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.svm import SVR

svr_poly = SVR(kernel='poly', degree=2)
svr_poly.fit(X, y)
predict = svr_poly.predict(X)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(predict, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(data.iloc[:, :-1], data.iloc[:, -1], test_size=0.2,
                                                    random_state=42)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((x_train, x_test, y_train, y_test), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

features = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

new_features = MultiLabelBinarizer().fit_transform(features)
rows, cols = new_features.shape
for i in range(rows):
    for j in range(cols):
        if new_features[i, j] == 1:
            new_features[i, j] = 0
        else:
            new_features[i, j] = 1

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_features, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression

df1 = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
slopes = []
for col in df1.columns:
    if col == "Time":
        continue
    mask = ~np.isnan(df1[col])
    x = np.atleast_2d(df1.Time[mask].values).T
    y = np.atleast_2d(df1[col][mask].values).T
    reg = LinearRegression().fit(x, y)
    slopes.append(reg.coef_[0])
slopes = np.array(slopes).reshape(-1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(slopes, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.model_selection import GridSearchCV

GridSearch_fitted = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
full_results = pd.DataFrame(GridSearch_fitted.cv_results_).sort_values(by="mean_fit_time")
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(full_results, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

estimators = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
# estimators = [('reduce_dim', PCA()), ('poly', PolynomialFeatures()), ('svm', SVC())]
clf = Pipeline(estimators)
### BEGIN SOLUTION
clf.steps.pop(-1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(len(clf.steps), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()

df_out = df.join(
    pd.DataFrame(
        mlb.fit_transform(df.pop('Col4')),
        index=df.index,
        columns=mlb.classes_))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np

dataset = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(data):
### BEGIN SOLUTION
# def solve(data):
    ### BEGIN SOLUTION
    from sklearn.model_selection import train_test_split

    x_train, x_test, y_train, y_test = train_test_split(data.iloc[:, :-1], data.iloc[:, -1], test_size=0.2,
                                                        random_state=42)
    ### END SOLUTION
    # return x_train, y_train, x_test, y_test
# x_train, y_train, x_test, y_test = solve(data)

    return x_train, y_train, x_test, y_test

### END SOLUTION
x_train, y_train, x_test, y_test = solve(dataset)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((x_train, x_test, y_train, y_test), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn import preprocessing

pt = preprocessing.PowerTransformer(method="yeo-johnson")
yeo_johnson_data = pt.fit_transform(data)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(yeo_johnson_data, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn.cluster

data_matrix = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
model = sklearn.cluster.AgglomerativeClustering(affinity='precomputed', n_clusters=2, linkage='complete').fit(data_matrix)
cluster_labels = model.labels_

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cluster_labels, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
kmeans = KMeans(n_clusters=2)
labels = kmeans.fit_predict(df[['mse']])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(labels, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier

X = [['asdf', '1'], ['asdf', '0']]
clf = DecisionTreeClassifier()
### BEGIN SOLUTION
from sklearn.feature_extraction import DictVectorizer

X = [dict(enumerate(x)) for x in X]
vect = DictVectorizer(sparse=False)
new_X = vect.fit_transform(X)
### END SOLUTION
clf.fit(new_X, ['2', '3'])
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_X, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

words = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
count = CountVectorizer(lowercase=False, token_pattern='[a-zA-Z0-9$&+:;=@#|<>^*()%-]+')
vocabulary = count.fit_transform([words])
feature_names = count.get_feature_names_out()
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(feature_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans

p, X = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
km = KMeans(n_clusters=8, random_state=42)
def get_samples(p, X, km):
    # calculate the closest 50 samples
### BEGIN SOLUTION
# def get_samples(p, X, km):
    # calculate the closest 50 samples
    ### BEGIN SOLUTION
    km.fit(X)
    d = km.transform(X)[:, p]
    indexes = np.argsort(d)[::][:50]
    samples = X[indexes]
    ### END SOLUTION
    # return samples
# closest_50_samples = get_samples(p, X, km)

    return samples

### END SOLUTION
closest_50_samples = get_samples(p, X, km)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(closest_50_samples, f)