<jupyter_start><jupyter_text>Регрессия<jupyter_code>data = pd.read_csv('task_1_capital.txt', sep='\s+')
data.shape
capital = data["Capital"].to_numpy()
rental  = data["Rental"].to_numpy()
A = np.ones((data.shape[0], 2),dtype=float)
A[:, 1], Y = capital, rental
T = A.transpose()
W = np.linalg.pinv(T @ A) @ T @ Y
W
plt.figure(figsize=(25, 5))
plt.scatter(capital, rental, c="black")
plt.title("task 1")
plt.xlabel("capital")
plt.ylabel("rental")
plt.plot(capital, A @ W, c="red");<jupyter_output><empty_output><jupyter_text>---  SVM<jupyter_code>metrics = {
    "accuracy"  : accuracy_score,
    "precision" : precision_score,
    "recall"    : recall_score,
    "f1"        : f1_score,
}
data = np.loadtxt("chips.txt", delimiter=",", dtype=float)
print(data.shape)
data[:10]
X, Y = data[:, :2], data[:, 2]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, shuffle=True, stratify=Y)
svc_param_grid = {
    "C"      : [0.001, 0.1, 1 , 10, 100],
    "kernel" : ["linear", "poly", "rbf"],
    "gamma"  : ["auto", "scale"]
}
svc = GridSearchCV(SVC(random_state=42), svc_param_grid, n_jobs=-1, scoring=["accuracy", "precision", "recall"], cv=5, iid=False, refit="accuracy")
svc.fit(X_train, Y_train);
svc.best_params_, svc.best_score_
fig = plt.figure(figsize=(10,5))
plot_decision_regions(X=X, y=Y.astype(np.integer), clf=svc.best_estimator_, legend=2)
plt.title('SVM Decision Region Boundary', size=16);<jupyter_output><empty_output><jupyter_text>----  KNN<jupyter_code>knn_param_grid = { 
    "n_neighbors" : [ i for i in range(3, X.shape[0] // 2) ],
    "weights"     : ["uniform", "distance"],
    "algorithm"   : ["ball_tree", "kd_tree", "brute"],
}
knn = GridSearchCV(KNeighborsClassifier(), knn_param_grid, n_jobs=-1, scoring=["accuracy", "precision", "recall"], cv=5, iid=False, refit="accuracy")
knn.fit(X_train, Y_train);
knn.best_params_, knn.best_score_
accuracy_score(Y_test, knn.best_estimator_.predict(X_test))
fig = plt.figure(figsize=(10, 5))
plot_decision_regions(X=X, y=Y.astype(np.integer), clf=knn.best_estimator_, legend=2)
plt.title('KNN Decision Region Boundary', size=16);<jupyter_output><empty_output><jupyter_text>---<jupyter_code>print("accuracy")
print("svc:", accuracy_score(Y_test, svc.best_estimator_.predict(X_test)))
print("knn:", accuracy_score(Y_test, knn.best_estimator_.predict(X_test)))<jupyter_output>accuracy
svc: 0.7777777777777778
knn: 0.7222222222222222
{'fraction_non_alphanumeric': 0.19246688741721854, 'fraction_numerical': 0.03435430463576159, 'mean_word_length': 6.324242424242424, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '253808', 'license': 'MIT', 'n_tokens_mistral': 1083, 'n_tokens_neox': 1015, 'n_words': 160, 'path': '7.sem/3.stochastic.programming/task1/task1.ipynb', 'repo_name': 'skoret/spbu.practice', 'size': '2416'}