0
点赞
收藏
分享

微信扫一扫

scikit-learn学习基础知识一


scikit-learn学习基础知识一

文章目录

  • ​​scikit-learn学习基础知识一​​
  • ​​一、一个线性回归的案例​​
  • ​​二、线性回归​​
  • ​​三、岭回归与分类​​
  • ​​四、总结​​

一、一个线性回归的案例

"""
scikit-learn learning1
一个简单的线性回归的案例。
"""


import sklearn
import numpy as np
import matplotlib.pyplot as plt


from sklearn.linear_model import LinearRegression


# 调用线性回归的现有的函数
# 直接调用现有的模块中的现成的线性回归函数 LinearRegression
# LinearRegression


"""
简单的线性回归案例
"""
"""
pip install sklearn
"""


def test():

"""
test the environment.
"""

print(sklearn.__version__)


def linear():

"""
linear data showing.
"""

X = np.array([[6], [8], [10], [14], [18]]).reshape(-1, 1)
# X
# 训练数据的特征
y = [7, 9, 13, 17.5, 18]
# y
plt.figure()
plt.title("simple linear") # title
plt.xlabel("X") # X
plt.ylabel("y") # y
plt.plot(X, y, 'k.')
# plot -> show.
plt.axis([0, 25, 0, 25])
# 0->25; 0->25.
plt.grid(True) # grid
plt.show() # show.


def training_linear():

"""
use LinearRegression to train.
"""

model = LinearRegression()
# 构建一个实例


X = np.array([[6], [8], [10], [14], [18]]).reshape(-1, 1)
# X
# 训练数据的特征

y = [7, 9, 13, 17.5, 18]
# y

model.fit(X, y)
# 进行训练 LinearRegression
# LinearRegression


test = np.array([[12]])
predict = model.predict(test)[0]
# 预测 LinearRegression

print(predict)


if __name__ == '__main__':
# 这是一个简单的案例,直接调用现有的函数来实现线性回归,
# 当然,也可以自己学会了那个线性回归的公式来自己计算线性回归的系数啦。

# test()

# linear()

training_linear()
# LinearRegression
# use LinearRegression yo train.
#

# pass

二、线性回归

"""
线性回归模型

最简单的线性回归。


"""


from numpy import mod
import sklearn
from sklearn.linear_model import LinearRegression as lr
# LinearRegression

model1 = lr()



model1.fit([[0, 0], [1, 1], [2, 2]], [0, 1, 2]) # fit method.


# parameter1: X; parameter2: y.

print(model1.coef_) # coef_ -> model1.coef_.
# model1.coef_ -> y=ax+b (a, b) (for example.)




# PS E:\1\Documents\PyTorch> & C:/Users/1/AppData/Local/Programs/Python/Python38/python.exe e:/1/Documents/PyTorch/pytorch/others/sklearn/sklearn_learning/sklearn_learning2.py
# [0.5 0.5]

三、岭回归与分类

"""


岭回归与分类


"""


from sklearn import linear_model

reg = linear_model.Ridge(alpha=.5)
reg.fit([[0, 0], [0, 0], [1, 1]], [0, .1, 1])
print(reg.coef_)
print(reg.intercept_)








"""
[0.34545455 0.34545455]
0.13636363636363638
"""

四、总结

以上就是使用Python语言来学习scikit-learn的基础知识的线性回归于岭回归的内容,希望对大家有一些帮助,最后,感谢大家的阅读与支持啦。


举报

相关推荐

0 条评论