<jupyter_start><jupyter_text>Линейная модель<jupyter_code>import pandas as pd 
import statsmodels.api as sm
import numpy as np
data = pd.read_csv('./AG.txt', sep = '	')
data
data.dtypes
data.describe()
import statsmodels.formula.api as sm
model = sm.ols(formula='y~x', data=data)
fitted3 = model.fit()
fitted3.summary()
import statsmodels.formula.api as smf
model1= smf.ols(formula='y~x', data=data).fit()
print(model1.summary())
# Residual standard error:
np.sqrt(model1.scale)<jupyter_output><empty_output><jupyter_text>Показательная модель<jupyter_code>y = data['y']
x = data['x']
# показательная
y1 = np.log10(y)

model_1 = sm.ols('y1 ~ x', data).fit()
model_1.summary()<jupyter_output><empty_output><jupyter_text>Степенная модель<jupyter_code>#степенная

x2 = np.log10(x)
y2 = np.log10(y)
model_2 = sm.ols('y2 ~ x2', data).fit()
model_2.summary()<jupyter_output><empty_output><jupyter_text>Гиперболическая модель<jupyter_code># гиперболическая
x3 = 1/x
y3 = y
model_3 = sm.ols('y3 ~ x3', data).fit()
model_3.summary()
pred_val0 = model1.fittedvalues.copy()
true_val0 = y
e0 = true_val0 - pred_val0

pred_val1 = model_1.fittedvalues.copy()
true_val1 = y1
e1 = true_val1 - pred_val1

pred_val2 = model_2.fittedvalues.copy()
true_val2 = y2
e2 = true_val2 - pred_val2

pred_val3 = model_3.fittedvalues.copy()
true_val3 = y3
e3 = true_val3 - pred_val3<jupyter_output><empty_output><jupyter_text>Аппроксимация<jupyter_code>apr0 = (1/len(x))*sum(abs(e0/y))*100
apr1 = (1/len(x))*sum(abs(e1/y1))*100
apr2 = (1/len(x))*sum(abs(e2/y2))*100
apr3 = (1/len(x))*sum(abs(e3/y3))*100
print(apr0)
print(apr1)
print(apr2)
print(apr3)<jupyter_output>8.396351371527702
1.3732006163028185
1.4113671313722802
9.195530780478185<jupyter_text>Коэффициенты детерминации (R^2)<jupyter_code># r2 чем ближе к 1 тем лучше
from sklearn.metrics import r2_score
print(r2_score(true_val0,pred_val0))
print(r2_score(true_val1,pred_val1))
print(r2_score(true_val2,pred_val2))
print(r2_score(true_val3,pred_val3))<jupyter_output>0.6727603268707893
0.687879106951971
0.6629699689794939
0.5928939814032377<jupyter_text>Cтандартная ошибка модели<jupyter_code>print(np.sqrt(model1.scale))
print(np.sqrt(model_1.scale))
print(np.sqrt(model_2.scale))
print(np.sqrt(model_3.scale))<jupyter_output>40.49900301912734
0.04357728324784551
0.0452827747807321
45.171563052765045<jupyter_text>Построение графиков<jupyter_code>import seaborn as sns
pred0 =pd.DataFrame({
    'real': true_val0,
    'pred': pred_val0
})
sns.regplot(x='real',y='pred',data=pred0)
pred1 =pd.DataFrame({
    'real': true_val1,
    'pred': pred_val1
})
sns.regplot(x='real',y='pred',data=pred1)
pred2 =pd.DataFrame({
    'real': true_val2,
    'pred': pred_val2
})
sns.regplot(x='real',y='pred',data=pred2)
pred3 =pd.DataFrame({
    'real': true_val3,
    'pred': pred_val3
})
sns.regplot(x='real',y='pred',data=pred3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1549149010072942, 'fraction_numerical': 0.11219173324070858, 'mean_word_length': 7.470588235294118, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '322392', 'license': 'WTFPL', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1336, 'n_words': 176, 'path': 'Course III/ECONOMETRICS/task4/python.ipynb', 'repo_name': 'GeorgiyDemo/FA', 'size': '2879'}