目录
简单的数组属性
基础的查看数据集
使用Matplotlib绘图:
sklearn模型评估常用指标函数
numpy.random.randint
numpy.linspace
numpy.ones
numpy.sum
numpy.random.randn
numpy.random.rand
shape()
sklearn.linear_model.LinearRegression
# 这个求numpy的版本
numpy.__version__
# 这个是求变量的类型
a = 5
type(a)
# list()命令创建一个整数的列表,range(x)函数将会拼出0到x-1之间的元素
int_list = list(range(10)) #输出[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 迭代上述list,将其int型改变成为string型的list
str_list = [str(i) for i in int_list] #输出['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
# 将int_list的值进行复制x次
double_list = int_list * 2 #输出[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 将list转化为数组的操作
int_arr = np.array(int_list) #输出array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# numpy的数组还有几个常用的属性
int_arr.ndim #维度的数量,输出1,代表一维数组
int_arr.shape #每个维度的大小,输出(10,),代表每个维度有10个元素,因为只有一维,所以是(10,)
int_arr.size #数组的元素个数,输出10,总的元素总数
int_arr.dtype #数组的数据类型,输出int64
# numpy的数组的索引(头索引,尾索引)、如果没填值,默认是(x[start=0:stop=维度: step=1])
int_arr[0] #输出0
int_arr[3] #输出3
int_arr[-1] #输出9
int_arr[-2] #输出8
int_arr[2:5] #输出array([2, 3, 4])
int_arr[:5] #输出array([0, 1, 2, 3, 4])
int_arr[5:] #输出array([5, 6, 7, 8, 9])
int_arr[::1] #输出[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
int_arr[::2] #隔x位输出,所以输出array([0, 2, 4, 6, 8])
int_arr[::-1] #返回数组的所有元素,但顺序相反,输出array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
int_arr[::-3] #隔x位逆[9, 6, 3, 0]输出,所以输出array([0, 2, 4, 6, 8])
# 创建一个N维M列数组
arr_2d = np.zeros((3, 5))
# 2维数组,输出array([[ 0., 0., 0., 0., 0.],[ 0., 0., 0., 0., 0.],[ 0., 0., 0., 0., 0.]])
arr_float_3d = np.ones((3, 2, 4))
#输出array([[[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]],
[[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]],
[[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]]])
# 创建一个RGB图像矩阵,这个图像是纯白色的。所以是(R、G、B)3*x*y,元素值h是255
arr_uint_3d = np.ones((3, 2, 4), dtype=np.uint8) * 255
#输出array([[[255, 255, 255, 255],
[255, 255, 255, 255]],
[[255, 255, 255, 255],
[255, 255, 255, 255]],
[[255, 255, 255, 255],
[255, 255, 255, 255]]], dtype=uint8)
# 拿手写识别MNIST来记录下数据的导入
from sklearn import datasets
mnist = datasets.fetch_mldata('MNIST original')
#求这个数据集的样本数和每个样本的大小,输出(70000, 784),即70000个样本,每个图片是28*28的大小
mnist.data.shape
#求这个数据集的样本数,输出70000,即70000个样本
mnist.data.shape
#求这个数据集的标签数,即查看分几类,输出array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
import numpy as np
np.unique(mnist.target)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
# 在X轴0-10之间,绘制100个点
x = np.linspace(0, 10, 100)
# 可以使用sin(x)得到所有的x值点,并且用plt中的plot函数绘制出来
plt.plot(x, np.sin(x))
# 如果没图出来,我们可以加上
plt.show()
# 如果没出来,并有选项,我们可以这样,以后就默认出图了
%matplotlib inline
plt.plot(x, np.sin(x))
# 可以保存绘制出来的图
plt.savefig('figures/02.03-sine.png')
# 建立一个10*6大小的图像
plt.figure(figsize=(10, 6))
# 绘制散点图
plt.scatter(all_blue[:, 0], all_blue[:, 1], c='b', marker='s', s=180)
# 函数主要的作用就是给图加上图例
plt.legend()
#函数用于设置文字说明。
plt.text(x,y,string,fontsize=15,verticalalignment="top",horizontalalignment="right")
from sklearn import metrics
# 每个数据点样本实际的分类
y_true = [0,4,1,3,5,....]
#每个数据点预测的分类
y_pred = [1,4,1,3,6,9,8,.....]
# 求准确率
metrics.accuracy_score(y_true, y_pred)
# 求精确率
metrics.precision_score(y_true, y_pred)
# 求召回率
metrics.recall_score(y_true, y_pred)
# 求均值方差
metrics.mean_squared_error(y_true, y_pred)
# 求可释方差
metrics.explained_variance_score(y_true, y_pred)
# 求决定系数
metrics.r2_score(y_true, y_pred)
①numpy.random.randint(low, high=None, size=None, dtype='l') #在半开半闭区间[low,high)上离散均匀分布的整数值
low
从分布中提取的最小的整数
high
分布中提取的最大值[low, high)
size:int or tuple of ints, optional
输出形态,例如(m,n,k),默认为None,表示给出单值
dtype:dtype, optional
获得的数据类型
# 在0-2的距离上生成数据5个点带类型
np.random.randint(0,2,5)
②numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None) #在指定的间隔内返回等距离的数字。
1、start: scalar(标量), 序列的起始点
2、stop: scalar, 依据endpoint会有变化, endpoint为True, 则包含显示, 当为False, 不包含(生成序列相当于原始num上加1按 endpoint = True生成, 结果只显示第一个到倒数第二个)
3、num: int, optional(可选), 生成样本数量, 必须是非负数
4、endpoint: bool, optional, 如果是真,则包括stop,如果为False,则没有stop
5、retstep: bool, optional
6、dtype: dtype, optional
③numpy.ones(shape, dtype=None, order='C')[source] #默认值都是1
1、shape : int或int的序列,新数组的形状,例如,(2, 3)或2
2、dtype : 数据类型,可选,数组的所需数据类型,例如numpy.int8。默认是 numpy.float64。
3、order : {'C','F'},可选,默认值:C,是否在内存中以行主(C-风格)或列主(Fortran-风格)顺序存储多维数据。
④numpy.sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue)
1、a是要进行加法运算的向量/数组/矩阵
2、axis的值可以为None,也可以为整数和元组,不可以超过最大维度
如2维时:
当axis为0时,是压缩行,即将每一列的元素相加,将矩阵压缩为一行
当axis为1时,是压缩列,即将每一行的元素相加,将矩阵压缩为一列
⑤numpy.random.randn(d0, d1, …, dn) 是从标准正态分布中返回一个或多个样本值。
arr1 = np.random.randn(2,4)
#输出[[-1.03021018 0.5197033 0.52117459 -0.70102661][ 0.98268569 1.21940697 -1.095241 -0.38161758]]
⑥numpy.random.rand(d0, d1, …, dn) 的随机样本位于[0, 1)中。
arr2 = np.random.rand(2,4)
#输出[[ 0.19947349 0.05282713 0.56704222 0.45479972][ 0.28827103 0.1643551 0.30486786 0.56386943]]
⑦shape()一般的数组如:【22,33】 shape是(2,):他表示他是一个一维数组,数组中有两个元素;注意他和shape(2,1)的区别,他两个不一样。
如[[22],[33]] 他的shape是(2,1),表示二维数组,每行有一个元素
⑧sklearn.linear_model.LinearRegression(fit_intercept=True, normalize=False, copy_X=True, n_jobs=1) #先定义一个线性回归对象
1、fit_intercept : 默认为True,是否计算该模型的截距。如果使用中心化的数据,可以考虑设置为False,不考虑截距。注意这里是考虑,一般还是要考虑截距
2、normalize: 默认为false. 当fit_intercept设置为false的时候,这个参数会被自动忽略。如果为True,回归器会标准化输入参数:减去平均值,并且除以相应的二范数。当然啦,在这里还是建议将标准化的工作放在训练模型之前。通过设置sklearn.preprocessing.StandardScaler来实现,而在此处设置为false
3、copy_X : 默认为True, 否则X会被改写
4、n_jobs: int 默认为1. 当-1时默认使用全部CPUs