python手感练习
小心情
疫情在家待了好久,所在地湖北黄冈。每天沉迷与论文、欢乐斗地主和东华帝君之间,我可真花心,哈哈哈哈。
python工具类软件还是要多敲代码,不然眼睛说我会了,手又给你Pia!(o ‵-′)ノ”(ノ﹏<。)Pia!(o ‵-′)ノ”(ノ﹏<。)打脸。
BTW:喜欢Rstudio的原因是因为,输入函数的前面几个单词就可以提示补全,目前仍在jupyter notebook的摸索当中。
只是numpy、scipy、pandas、matplotlib的小例子
#numpy小例子
import numpy
i = numpy.array([[520,13,14],[25,9,178]])
print("i:\n{}".format(i))
i:
[[520 13 14]
[ 25 9 178]]
#scipy小例子
import numpy as np
from scipy import sparse #sparse用来生成稀疏矩阵
matrix = np.eye(6) #6行6列稀疏矩阵
sparse_matrix = sparse.csr_matrix(matrix) #转换格式sparse函数只存储非零元素
print('对角矩阵:\n{}'.format(matrix))
print('\nsparse存储的矩阵:\n{}'.format(sparse_matrix))
对角矩阵:
[[1. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 1.]]
sparse存储的矩阵:
(0, 0) 1.0
(1, 1) 1.0
(2, 2) 1.0
(3, 3) 1.0
(4, 4) 1.0
(5, 5) 1.0
#pandas小例子
import pandas as pd
data = {
"Name":['小芋','小菡','小榆','小寒'],
"City":['北京','上海','广州','深圳'],
"Age":['18','20','22','24'],
"Height":['162','161','165','166'],
} #data是字典的形式
data_frame = pd.DataFrame(data) #通过DataFrame函数将字典转为数据框格式。
display(data_frame)
display(data_frame[data_frame.City != '北京']) #对数据框进行筛选
Name City Age Height
0 小芋 北京 18 162
1 小菡 上海 20 161
2 小榆 广州 22 165
3 小寒 深圳 24 166
Name City Age Height
1 小菡 上海 20 161
2 小榆 广州 22 165
3 小寒 深圳 24 166
#matplotlib小例子
%matplotlib inline
#运行jupyter进行内置画图,%同一行不能有井号注释
import matplotlib.pyplot as plt
x = np.linspace(-20,20,10) #生成间隔为10的等差数列
y = x**3 + 2*x**2 +6*x +5
plt.plot(x,y,marker = 'o')
scikit_learn 很重要的机器学习库,建立在scipy之上