<jupyter_start><jupyter_text>statistics - line regression  Simple Linear Regression With scikit-learn<jupyter_code>import numpy as np
from sklearn.linear_model import LinearRegression

x = np.array([5, 15, 25, 35, 45, 55]).reshape((-1, 1))
y = np.array([5, 20, 14, 32, 22, 38])
x,y
# Create a regression model and fit it with existing data
model = LinearRegression().fit(x, y)
# get the results to check whether the model works satisfactorily and interpret it
>>> r_sq = model.score(x, y)
>>> print('coefficient of determination:', r_sq)
>>> print('intercept:', model.intercept_)
# intercept: 5.633333333333329  截距
>>> print('slope:', model.coef_)
# Once there is a satisfactory model, you can use it for predictions with either existing or new data
>>> y_pred = model.predict(x)
>>> print('predicted response:', y_pred, sep='\n')
# draw the line regression fitline and provide data and predict reponse 
import matplotlib.pyplot as plt
x_new=[5, 15, 25, 35, 45, 55]
ya = [5, 20, 14, 32, 22, 38]
xb = np.arange(5).reshape((-1,1))
coef = np.polyfit(xa,ya,1)
poly1d_fn = np.poly1d(coef) 
plt.plot(xa,ya, 'yo', xa, poly1d_fn(xa), '--k')
plt.plot(x_new,poly1d_fn(x_new), 'yo')
plt.xlim(0, 60)
plt.ylim(0, 60)
plt.show()<jupyter_output><empty_output><jupyter_text>Multiple Linear Regression With scikit-learn<jupyter_code>x = [[0, 1], [5, 1], [15, 2], [25, 5], [35, 11], [45, 15], [55, 34], [60, 35]]
y = [4, 5, 20, 14, 32, 22, 38, 43]
x, y = np.array(x), np.array(y)
model = LinearRegression().fit(x, y)
>>> r_sq = model.score(x, y)
>>> print('coefficient of determination:', r_sq)
# coefficient of determination: 0.8615939258756776
>>> print('intercept:', model.intercept_)
# intercept: 5.52257927519819
>>> print('slope:', model.coef_)
# slope: [0.44706965 0.25502548]
>>> y_pred = model.predict(x)
>>> print('predicted response:', y_pred, sep='\n')
# predicted response:
# [ 5.77760476  8.012953   12.73867497 17.9744479  23.97529728 29.4660957
#  38.78227633 41.27265006]
>>> x_new = np.arange(10).reshape((-1, 2))
>>> print(x_new)
>>> y_new = model.predict(x_new)
>>> print(y_new)<jupyter_output>[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
[ 5.77760476  7.18179502  8.58598528  9.99017554 11.3943658 ]<jupyter_text>Polynomial Regression with numpy<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

X = [1, 5, 8, 10, 14, 18]
Y = [1, 1, 10, 20, 45, 75]
# Train Algorithm (Polynomial)
degree = 2
poly_fit = np.poly1d(np.polyfit(X,Y, degree))


# Plot data
xx = np.linspace(0, 26, 100)
print(xx)
plt.plot(xx, poly_fit(xx), c='r',linestyle='-')
plt.title('Polynomial')
plt.xlabel('X')
plt.ylabel('Y')
plt.axis([0, 25, 0, 100])
plt.grid(True)
plt.scatter(X, Y)
plt.show()
# Predict price
print( poly_fit(12) )<jupyter_output>29.997335810423564<jupyter_text>example  animal body and brain<jupyter_code>import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt

#read data
dataframe = pd.read_fwf('brain_body.txt')
x_values = dataframe[['Brain']]#[0:4]
y_values = dataframe[['Body']]#[0:4]

#train model on data
body_reg = linear_model.LinearRegression()
body_reg.fit(x_values, y_values)
print(x_values)
#visualize results
plt.scatter(x_values, y_values)
# plt.plot(x_values, body_reg.predict(x_values))
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17003058103975535, 'fraction_numerical': 0.10886850152905199, 'mean_word_length': 5.608080808080808, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '361454', 'license': 'MIT', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1330, 'n_words': 332, 'path': 'basic/chb15.ipynb', 'repo_name': 'creasy-pita/pythondata-book-note', 'size': '3270'}