0


【Numpy-矩阵库~python】

目录


个人昵称:lxw-pro
个人主页:欢迎关注 我的主页
个人感悟: “失败乃成功之母”,这是不变的道理,在失败中总结,在失败中成长,才能成为IT界的一代宗师。

# -*- coding = utf-8 -*-# @Time : 2022/8/7 14:30# @Author : lxw_pro# @File : NumPy 矩阵库.py# @Software : PyCharm

numpy学习(续)

NumPy 中包含了一个矩阵库 numpy.matlib,该模块中的函数返回的是一个矩阵,而不是 ndarray 对象。

转置矩阵

import numpy as np

lxw = np.arange(16).reshape(4,4)print("原数组为:\n", lxw)print("转置过的数组为:\n", lxw.T)

matlib.empty()

matlib.empty()

函数返回一个新的矩阵

import numpy.matlib

kk = np.matlib.empty((3,3))# 填充为随机数据print(kk)

numpy.matlib.zeros()

numpy.matlib.zeros()

函数创建一个以 0 填充的矩阵

ll = np.matlib.zeros((3,3))print(ll)

numpy.matlib.ones()

numpy.matlib.ones()

函数创建一个以 1 填充的矩阵

yy = np.matlib.ones((3,3))print(yy)

numpy.matlib.eye()

numpy.matlib.eye() 函数返回一个矩阵,对角线元素为 1,其他位置为零

dd = np.matlib.eye(n=3, M=4, k=0, dtype=float)print(dd)

numpy.matlib.rand()

numpy.matlib.rand()

函数创建一个给定大小的矩阵,数据是随机填充的

sj = np.matlib.rand((3,3))print(sj)

二维矩阵

e = np.matrix('1, 2;3, 4')print(e)

n维数组

r = np.asarray(e)print(r)

以上所有程序对应运行结果如下:

原数组为:
 [[0123][4567][891011][12131415]]
转置过的数组为:
 [[04812][15913][261014][371115]][[           nan 0.0000000e+0001.1581509e-311][2.0236929e-3200.0000000e+0000.0000000e+000][0.0000000e+0000.0000000e+0000.0000000e+000]][[0.0.0.][0.0.0.][0.0.0.]][[1.1.1.][1.1.1.][1.1.1.]][[1.0.0.0.][0.1.0.0.][0.0.1.0.]][[0.398367270.169833880.91118039][0.772830470.246087130.72451454][0.324477120.215230510.4374776]][[12][34]][[12][34]]

—————————————————————————————————

pandas 每日一练:

# -*- coding = utf-8 -*-# @Time : 2022/8/7 19:48# @Author : lxw_pro# @File : pandas-12 练习.py# @Software : PyCharmimport pandas as pd
import matplotlib.pyplot as plt

lxw = pd.read_excel("site.xlsx")print(lxw)

运行结果如下:

     Unnamed:0  Unnamed:0.1   create_dt  ...   yye  sku_cost_prc     lrl
0012016-11-30...8.86.7730.00%1122016-11-30...7.55.7730.00%2232016-11-30...5.03.8530.00%3342016-11-30...19.67.5430.00%4452016-12-02...13.510.3830.00%.......................7517517522016-12-31...1.00.7730.00%7527527532016-12-31...2.01.5430.00%7537537542016-12-31...1.00.7730.00%7547547552016-12-31...7.62.9230.00%7557557562016-12-31...3.32.5430.00%[756 rows x 8 columns]

71、以5个数据作为一个数据滑动窗口,计算这五个数据总和(sku_cost_prc)

zh = lxw['sku_cost_prc'].rolling(5).sum()print(zh)

运行结果如下:

0        NaN
1        NaN
2        NaN
3        NaN
434.31...75110.8975210.517538.367549.907558.54
Name: sku_cost_prc, Length:756, dtype: float64

72、将sku_cost_prc 5日均线、20日均线与原始数据绘制在同一个圈上

lxw['sku_cost_prc'].plot()
lxw['sku_cost_prc'].rolling(5).mean().plot()
lxw['sku_cost_prc'].rolling(20).mean().plot()

plt.show()

运行效果如下:

在这里插入图片描述


73、将数据往后移动5天

wh = lxw.shift(5)
print(wh)

运行结果如下:

     Unnamed:0  Unnamed:0.1   create_dt  ...   yye  sku_cost_prc     lrl
0           NaN           NaN         NaN  ...   NaN           NaN     NaN
1           NaN           NaN         NaN  ...   NaN           NaN     NaN
2           NaN           NaN         NaN  ...   NaN           NaN     NaN
3           NaN           NaN         NaN  ...   NaN           NaN     NaN
4           NaN           NaN         NaN  ...   NaN           NaN     NaN
.......................751746.0747.02016-12-31...20.02.0040.00%752747.0748.02016-12-31...5.01.9230.00%753748.0749.02016-12-31...3.82.9230.00%754749.0750.02016-12-31...1.81.3830.00%755750.0751.02016-12-31...3.93.902.56%[756 rows x 8 columns]

74、将数据往前移动5天

wq = lxw.shift(-5)print(wq)

运行结果如下:

     Unnamed:0  Unnamed:0.1   create_dt  ...   yye  sku_cost_prc     lrl
05.06.02016-12-02...3.93.0030.00%16.07.0         NaN  ...10.88.3130.00%27.08.02016-12-02...15.511.9230.00%38.09.02016-12-02...3.52.6930.00%49.010.02016-12-02...   NaN          7.3130.00%.......................751         NaN           NaN         NaN  ...   NaN           NaN     NaN
752         NaN           NaN         NaN  ...   NaN           NaN     NaN
753         NaN           NaN         NaN  ...   NaN           NaN     NaN
754         NaN           NaN         NaN  ...   NaN           NaN     NaN
755         NaN           NaN         NaN  ...   NaN           NaN     NaN
[756 rows x 8 columns]

75、使用expanding函数计算sku_cost_prc的移动窗口均值

yj = lxw['sku_cost_prc'].expanding(min_periods=1).mean()print(yj)

运行结果如下:

06.77000016.27000025.46333335.98250046.862000...7519.5490937529.5384297539.5267697549.5179957559.508740
Name: sku_cost_prc, Length:756, dtype: float64

每日一言:

沉缅于虚幻的梦想,而忘记现实的生活,这是毫无意义的,千万记住。

人往往需要说很多话,然后才能够归至潜默。


持续更新中…

点赞,你的认可是我创作的

动力


收藏,你的青睐是我努力的

方向


评论,你的意见是我进步的

财富


关注,你的喜欢是我长久的

坚持


在这里插入图片描述

欢迎关注微信公众号【程序人生6】,一起探讨学习哦!!!

标签: python numpy 矩阵

本文转载自: https://blog.csdn.net/m0_66318554/article/details/126213992
版权归原作者 lxw-pro 所有, 如有侵权,请联系我们删除。

“【Numpy-矩阵库~python】”的评论:

还没有评论