<jupyter_start><jupyter_text>Sklearn  sklearn.linear_model<jupyter_code>import pandas as pd
import numpy as np
import scipy.stats as sts
from sklearn import datasets, linear_model, metrics
from sklearn import model_selection as mod_sel

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns

sns.set()
sns.set_style("whitegrid")

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Линейная регрессия  Генерация данных<jupyter_code>data, target, coef = datasets.make_regression(n_features = 2, n_informative = 1, n_targets = 1, 
                                              noise = 5., coef = True, random_state = 2)
plt.scatter(list(map(lambda x:x[0], data)), target, color = 'r')
plt.scatter(list(map(lambda x:x[1], data)), target, color = 'b')
train_data, test_data, train_labels, test_labels = mod_sel.train_test_split(
    data, target,  
    test_size = 0.3
)<jupyter_output><empty_output><jupyter_text>LinearRegression<jupyter_code>linear_regressor = linear_model.LinearRegression()
linear_regressor.fit(train_data, train_labels)
predictions = linear_regressor.predict(test_data)
print(test_labels)
print(predictions)
metrics.mean_absolute_error(test_labels, predictions)
linear_scoring = mod_sel.cross_val_score(
    linear_regressor, data, target, scoring = 'neg_mean_absolute_error', 
    cv = 10
)
print('mean: {}, std: {}'.format(linear_scoring.mean(), linear_scoring.std()))
scorer = metrics.make_scorer(metrics.mean_absolute_error, greater_is_better = True)
linear_scoring = mod_sel.cross_val_score(
    linear_regressor, data, target, scoring=scorer, 
    cv = 10
)
print('mean: {}, std: {}'.format(linear_scoring.mean(), linear_scoring.std()))
coef
linear_regressor.coef_
# в лекции не указано, что в уравнении обученной модели также участвует свободный член
linear_regressor.intercept_
print("y = {:.2f}*x1 + {:.2f}*x2".format(coef[0], coef[1]))
print("y = {:.2f}*x1 + {:.2f}*x2 + {:.2f}".format(
    linear_regressor.coef_[0],
    linear_regressor.coef_[1], 
    linear_regressor.intercept_
))<jupyter_output>y = 38.01*x1 + 0.37*x2 + -0.25<jupyter_text>Lasso<jupyter_code>lasso_regressor = linear_model.Lasso(random_state = 3)
lasso_regressor.fit(train_data, train_labels)
lasso_predictions = lasso_regressor.predict(test_data)
lasso_scoring = mod_sel.cross_val_score(lasso_regressor, data, target, scoring = scorer, cv = 10)
print('mean: {}, std: {}'.format(lasso_scoring.mean(), lasso_scoring.std()))
print(lasso_regressor.coef_)
print("y = {:.2f}*x1 + {:.2f}*x2".format(coef[0], coef[1]))
print("y = {:.2f}*x1 + {:.2f}*x2".format(lasso_regressor.coef_[0], lasso_regressor.coef_[1]))<jupyter_output>y = 37.22*x1 + 0.00*x2
{'fraction_non_alphanumeric': 0.13592946362968406, 'fraction_numerical': 0.022777369581190303, 'mean_word_length': 6.146981627296588, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '477270', 'license': 'MIT', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1046, 'n_words': 182, 'path': '02_supervised_learning/06_w2p_sklearn.linear_model_part2.ipynb', 'repo_name': 'pkuderov/mlda-course', 'size': '2722'}