线性/非线性回归--计算社会科学作业

<1>简单一元线性回归

编了一份一元数据,反映成绩和学习时间之间的正相关关系

用Python进行拟合并绘制图像

from collections import OrderedDict
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

lib = {'学习时间': [0.50, 0.75, 1.00, 1.25, 1.50, 1.75, 1.75, 2.00, 2.25,
                2.50, 2.75, 3.00, 3.25, 3.50, 4.00, 4.25, 4.50, 4.75, 5.00, 5.50],
       '分数': [10,  22,  13,  43,  20,  22,  33,  50,
              62,  48,  55,  75,  62,  73,  81,  76,  64,  82,  90,  93]}

order_lib = OrderedDict(lib)
df_lib = pd.DataFrame(order_lib)

x = df_lib.loc[:, '学习时间']
y = df_lib.loc[:, '分数']

plt.scatter(x, y)
plt.xlabel('Hours')
plt.ylabel('Score')

x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.8)
x_train = x_train.values.reshape(-1, 1)
x_test = x_test.values.reshape(-1, 1)

model = LinearRegression()
model.fit(x_train, y_train)

a = model.intercept_
b = model.coef_

plt.scatter(x_train, y_train, color='orange')
y_train_predict = model.predict(x_train)
plt.plot(x_train, y_train_predict, color='black')

plt.title('y = {:.4f}+{:.4f}x'.format(*b, a))
plt.grid()
plt.show()

<2>多项式非线性回归

用一元二次方程对正态分布进行拟合

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LinearRegression, SGDClassifier
from sklearn.preprocessing import PolynomialFeatures, StandardScaler


x = np.arange(0, 1, 0.002)
y = norm.rvs(0, size=500, scale=0.1)  # 高斯分布数据
y = y + x**2

plt.scatter(x, y, s=5, color='orange')
y_test = []
y_test = np.array(y_test)

# clf = LinearRegression(fit_intercept=False)
clf = Pipeline([('poly', PolynomialFeatures(degree=100)),
                ('linear', LinearRegression(fit_intercept=False))])
clf.fit(x[:, np.newaxis], y)
y_test = clf.predict(x[:, np.newaxis])

plt.plot(x, y_test, linewidth=2)
plt.grid()  # 显示网格
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值