列表切片
# 列表切片 字符串/元组/ndarray都支持切片
# 切片:截取一部分的操作
li = []
for i in range(1, 10):
li.append(i)
print(li)
# 左闭右开
print(li[0:5])
print(li[:5])
print(li[5:])
print(li[5:9])
print(li[-3:])
print(li[:])
copy = li[:]
copy[0] = 99
print(copy)
# print(li[::step]) #= ==13
print(li[::]) #= ==13
print(li[::2])
print(li[::-1]) # inverse
print(li[1:5])
print(li[-5:-9:-1][::-1])
print(li[-8:-4: 1])
numpy
import numpy as np
print([10, 20] + [20, 30]) # [30, 50]
# 运行效率低, 不支持矢量化操作
# 如何创建numpy 类型呢
# 1 传入列表
arr1 = np.array([10, 20])
arr2 = np.array([20, 30])
print(arr1 + arr2)
# 2
print(np.arange(10))
print(type(np.arange(10)))
# 成为numpy 类型 有什么属性
x = np.arange(1, 16) # 向量
print("x = ", x)
X = np.arange(1, 16).reshape(5, -1) # 自动推导
print("X = ")
print(X)
print(x.ndim) # 维度
print(X.ndim)
print(x.shape)
print(X.shape)
print(x.size)
print(X.size) # 元素个数
# numpy的切片
print(x[:5])
print(X[:3, :2]) # 名字[行切片, 列切片]
from sklearn import datasets
boston = datasets.load_boston()
X = boston.data
y = boston.target
print(X.shape)
print(y.shape)
print("+++++++++++++++++++++++++++++")
print(X[0])
print("+++++++++++++++++++++++++++++")
print(X[0, :3])
print(X[:20].shape)
元组
import numpy as np
print([10, 20] + [20, 30]) # [30, 50]
# 运行效率低, 不支持矢量化操作
# 如何创建numpy 类型呢
# 1 传入列表
arr1 = np.array([10, 20])
arr2 = np.array([20, 30])
print(arr1 + arr2)
# 2
print(np.arange(10))
print(type(np.arange(10)))
# 成为numpy 类型 有什么属性
x = np.arange(1, 16) # 向量
print("x = ", x)
X = np.arange(1, 16).reshape(5, -1) # 自动推导
print("X = ")
print(X)
print(x.ndim) # 维度
print(X.ndim)
print(x.shape)
print(X.shape)
print(x.size)
print(X.size) # 元素个数
# numpy的切片
print(x[:5])
print(X[:3, :2]) # 名字[行切片, 列切片]
from sklearn import datasets
boston = datasets.load_boston()
X = boston.data
y = boston.target
print(X.shape)
print(y.shape)
print("+++++++++++++++++++++++++++++")
print(X[0])
print("+++++++++++++++++++++++++++++")
print(X[0, :3])
print(X[:20].shape)
网友评论