机器学习笔记

大数据

  • 机器学习

机器学习

回归问题和分类问题

  • 导入数学函数库import numpy as np
  • 导入绘图模块import matplotlib.pyplot as plt
  • 生成回归样本数据from sklearn.datasets import make_regression
  • X,y=make_regression(n_samples=100,n_features=1,n_informative=1,noise=50,random_state=8)
英文 翻译
n_samples 样本数
n_features 特征数(自变量个数)
n_informative 参与建模特征数
n_targets 因变量个数
noise 噪音
bias 偏差(截距)
coef 是否输出coef标识
random_state 随机状态若为固定值则每次产生的数据都一样
  • 读取矩阵的长度X.shapey.shape
  • 构建画布fig=plt.figure()
  • 引入坐标系ax=fig.add_subplot(111)
  • 带入点ax. Scatter(X,y,c='b',s=60)
  • 保存画布plt.savefig('lr.pdf')
  • 导入y=ax+b的模型from sklearn.linear_model import LinearRegression
  • 建立模型对象,构造函数lr=LinearRegression()
  • 训练数据lr.fit(X,y)
  • 求斜率lr.coef_
  • 求截距lr.intercept_
  • XX=np.linspace(-3,3,200)
    array = numpy.linspace(start, end, num=num_points) 将在 start 和 end 之间生成
    一个统一的序列,共有 num_points 个元素。
  • XX=XX.reshape(-1,1)
  • XX.shape
  • yy=lr.predict(XX)
  • ax.plot(XX,yy,c=‘r’)
  • plt.savefig(‘test.pdf’)
  • lr.score(X,y)
  • lr.score(XX,yy)
  • del X,XX,y,yy,fig,ax
  • from sklearn.datasets import load_diabetes
  • diabetes=load_diabetes()
    diabetes
  • print(diabetes[‘DESCR’])
  • from sklearn.model_selection import train_test_split
  • X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=8)
  • X_train.shape
  • X_test.shape
  • lr=LinearRegression()
  • lr.fit(X_train,y_train)
  • lr.coef_
  • lr.intercept_
  • lr.score(X_train,y_train)
  • lr.score(X_test,y_test)
    下节课:训练的分数跟测试分数差别很大,产生过拟合问题(岭回归和套索回归)
import numpy as np
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import Ridge

diabetes=load_diabetes()
X=diabetes['data']
y=diabetes['target']
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=8)
lr=LinearRegression()
lr.fit(X_train,y_train)
print(lr.score(X_train,y_train))
print(lr.score(X_test,y_test))
rd=Ridge()
rd.fit(X_train,y_train)
print(rd.score(X_train,y_train))
print(rd.score(X_test,y_test))
la=Lasso()
la.fit(X_train,y_train)
print(la.score(X_train,y_train))
print(la.score(X_test,y_test))

你可能感兴趣的:(python,Anaconda,笔记,机器学习,python,人工智能)