栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

<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()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/423600.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号