numpy数据分析练习

  1. 查看版本

import numpy as np

print(np.__version__)

  1. 创建一维数组

arr = np.arange(10)

3.创建一个布尔数组


np.full((3, 3), True, dtype=bool)

np.ones((3,3), dtype=bool)

4.从一维数组中提取满足指定条件的元素?


arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

arr[arr % 2 == 1]

5.用numpy数组中的另一个值替换满足条件的元素项?


arr[arr % 2 == 1] = -1

6.在不影响原始数组的情况下替换满足条件的元素项?


arr = np.arange(10)

out = np.where(arr % 2 == 1, -1, arr)

7.改变数组的形状?


arr = np.arange(10)

arr.reshape(2, -1)  # Setting to -1 automatically decides the number of cols

  1. 垂直叠加两个数组?

a = np.arange(10).reshape(2,-1)

b = np.repeat(1, 10).reshape(2,-1)

# Answers# Method 1:

np.concatenate([a, b], axis=0)

# Method 2:

np.vstack([a, b])

# Method 3:

np.r_[a, b]

  1. 水平叠加两个数组?

a = np.arange(10).reshape(2,-1)

b = np.repeat(1, 10).reshape(2,-1)

# Answers# Method 1:

np.concatenate([a, b], axis=1)

# Method 2:

np.hstack([a, b])

# Method 3:

np.c_[a, b]

  1. 在无硬编码的情况下生成numpy中的自定义序列?

a = np.array([1,2,3])

np.r_[np.repeat(a, 3), np.tile(a, 3)]

  1. 获取两个numpy数组之间的公共项?

a = np.array([1,2,3,2,3,4,3,4,5,6])

b = np.array([7,2,10,2,7,4,9,4,9,8])

np.intersect1d(a,b)

  1. 从一个数组中删除存在于另一个数组中的项?

a = np.array([1,2,3,4,5])

b = np.array([5,6,7,8,9])

# From 'a' remove all of 'b'

np.setdiff1d(a,b)

13.得到两个数组元素匹配的位置?


a = np.array([1,2,3,2,3,4,3,4,5,6])

b = np.array([7,2,10,2,7,4,9,4,9,8])

np.where(a == b)

14.从numpy数组中提取给定范围内的所有数字?


a = np.arange(15)

# Method 1

index = np.where((a >= 5) & (a <= 10))

a[index]

# Method 2:

index = np.where(np.logical_and(a>=5, a<=10))

a[index]# > (array([6, 9, 10]),)

# Method 3: (thanks loganzk!)

a[(a >= 5) & (a <= 10)]

15.创建一个python函数来处理scalars并在numpy数组上工作?


def maxx(x, y):

    """Get the maximum of two items"""

    if x >= y:

        return x

    else:

        return y

pair_max = np.vectorize(maxx, otypes=[float])

a = np.array([5, 7, 9, 8, 6, 4, 5])

b = np.array([6, 3, 4, 8, 9, 7, 1])

pair_max(a, b)

16.交换二维numpy数组中的两列?


# Input

arr = np.arange(9).reshape(3,3)

arr

# Solution

arr[:, [1,0,2]]

17.交换二维numpy数组中的两行?


arr = np.arange(9).reshape(3,3)

# Solution

arr[[1,0,2], :]

18.反转二维数组的行?


# Input

arr = np.arange(9).reshape(3,3)

arr[::-1]

array([[6, 7, 8],

      [3, 4, 5],

      [0, 1, 2]])

19.反转二维数组的列?


arr = np.arange(9).reshape(3,3)

# Solution

arr[:, ::-1]

20.创建包含5到10之间随机浮动的二维数组?


# Input

arr = np.arange(9).reshape(3,3)

# Solution Method 1:

rand_arr = np.random.randint(low=5, high=10, size=(5,3)) + np.random.random((5,3))

# print(rand_arr)

# Solution Method 2:

rand_arr = np.random.uniform(5,10, size=(5,3))

print(rand_arr)

21.在numpy数组中只打印小数点后三位?


# Input

rand_arr = np.random.random((5,3))

# Create the random array

rand_arr = np.random.random([5,3])

# Limit to 3 decimal places

np.set_printoptions(precision=3)

rand_arr[:4]

  1. 通过e式科学记数法(如1e10)来打印一个numpy数组?

# Reset printoptions to default

np.set_printoptions(suppress=False)

# Create the random array

np.random.seed(100)

rand_arr = np.random.random([3,3])/1e3

rand_arr

np.set_printoptions(suppress=True, precision=6)  # precision is ptional

rand_arr

23.限制numpy数组输出中打印的项目数?


np.set_printoptions(threshold=6)

a = np.arange(15)

24.打印完整的numpy数组而不截断


# Input

np.set_printoptions(threshold=6)

a = np.arange(15)

# Solution

np.set_printoptions(threshold=np.nan)

a  # 有报错

25.导入数字和文本的数据集保持文本在numpy数组中完好无损?


# Solution

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris = np.genfromtxt(url, delimiter=',', dtype='object')

names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')

# Print the first 3 rows

iris[:3]

  1. 从1维元组数组中提取特定列?

# **给定:**

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris_1d = np.genfromtxt(url, delimiter=',', dtype=None)print(iris_1d.shape)

# Solution:

species = np.array([row[4] for row in iris_1d])

species[:5]

27.将1维元组数组转换为2维numpy数组?


# **给定:**

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris_1d = np.genfromtxt(url, delimiter=',', dtype=None)

# Solution:# Method 1: Convert each row to a list and get the first 4 items

iris_2d = np.array([row.tolist()[:4] for row in iris_1d])

iris_2d[:4]

# Alt Method 2: Import only the first 4 columns from source url

iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])

iris_2d[:4]

28.计算numpy数组的均值,中位数,标准差?


# Input

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris = np.genfromtxt(url, delimiter=',', dtype='object')

sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])

# Solution

mu, med, sd = np.mean(sepallength), np.median(sepallength), np.std(sepallength)print(mu, med, sd)

29.规范化数组,使数组的值正好介于0和1之间?


url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])

# Solution

Smax, Smin = sepallength.max(), sepallength.min()

S = (sepallength - Smin)/(Smax - Smin)# or

S = (sepallength - Smin)/sepallength.ptp()  # Thanks, David jeda!

print(S)

31.找到numpy数组的百分位数?


# Input

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])

# Solution

np.percentile(sepallength, q=[5, 95])

32.在数组中的随机位置插入值?


# Input

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')

# Method 1

i, j = np.where(iris_2d)

# i, j contain the row numbers and column numbers of 600 elements of iris_x

np.random.seed(100)

iris_2d[np.random.choice((i), 20), np.random.choice((j), 20)] = np.nan

# Method 2

np.random.seed(100)

iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan

# Print first 10 rows

print(iris_2d[:10])

33.在numpy数组中找到缺失值的位置?


# Input

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])

iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan

# Solution

print("Number of missing values: \n", np.isnan(iris_2d[:, 0]).sum())print("Position of missing values: \n", np.where(np.isnan(iris_2d[:, 0])))

34.根据两个或多个条件过滤numpy数组?


# Input

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])

# Solution

condition = (iris_2d[:, 2] > 1.5) & (iris_2d[:, 0] < 5.0)

iris_2d[condition]

35.从numpy数组中删除包含缺失值的行?


# Input

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])

iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan

# Solution# No direct numpy function for this.# Method 1:

any_nan_in_row = np.array([~np.any(np.isnan(row)) for row in iris_2d])

iris_2d[any_nan_in_row][:5]

# Method 2: (By Rong)

iris_2d[np.sum(np.isnan(iris_2d), axis = 1) == 0][:5]

36.找到numpy数组的两列之间的相关性?


# Input

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])

# Solution 1

np.corrcoef(iris[:, 0], iris[:, 2])[0, 1]

# Solution 2

from scipy.stats.stats import pearsonr 

corr, p_value = pearsonr(iris[:, 0], iris[:, 2])

print(corr)

37.查找给定数组是否具有任何空值?


url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])

np.isnan(iris_2d).any()

38.在numpy数组中用0替换所有缺失值?


# Input

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])

iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan

# Solution

iris_2d[np.isnan(iris_2d)] = 0

iris_2d[:4]

39.在numpy数组中查找唯一值的计数?


# Import iris keeping the text column intact

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris = np.genfromtxt(url, delimiter=',', dtype='object')

names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')

# Solution# Extract the species column as an array

species = np.array([row.tolist()[4] for row in iris])

# Get the unique values and the counts

np.unique(species, return_counts=True)

40.将数字转换为分类(文本)数组?


# Input

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris = np.genfromtxt(url, delimiter=',', dtype='object')

names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')

# Bin petallength

petal_length_bin = np.digitize(iris[:, 2].astype('float'), [0, 3, 5, 10])

# Map it to respective category

label_map = {1: 'small', 2: 'medium', 3: 'large', 4: np.nan}

petal_length_cat = [label_map[x] for x in petal_length_bin]

# View

petal_length_cat[:4]

41.从numpy数组的现有列创建新列?


# Input

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')

# Solution# Compute volume

sepallength = iris_2d[:, 0].astype('float')

petallength = iris_2d[:, 2].astype('float')

volume = (np.pi * petallength * (sepallength**2))/3

# Introduce new dimension to match iris_2d's

volume = volume[:, np.newaxis]

# Add the new column

out = np.hstack([iris_2d, volume])

# View

out[:4]

42.在numpy中进行概率抽样?


# Import iris keeping the text column intact

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris = np.genfromtxt(url, delimiter=',', dtype='object')

# Solution# Get the species column

species = iris[:, 4]

# Approach 1: Generate Probablistically

np.random.seed(100)

a = np.array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'])

species_out = np.random.choice(a, 150, p=[0.5, 0.25, 0.25])

# Approach 2: Probablistic Sampling (preferred)

np.random.seed(100)

probs = np.r_[np.linspace(0, 0.500, num=50), np.linspace(0.501, .750, num=50), np.linspace(.751, 1.0, num=50)]

index = np.searchsorted(probs, np.random.random(150))

species_out = species[index]

print(np.unique(species_out, return_counts=True))

方法2是首选方法,因为它创建了一个索引变量,该变量可用于取样2维表格数据。

43.按另一个数组分组时获取数组的第二大值?


# Import iris keeping the text column intact

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris = np.genfromtxt(url, delimiter=',', dtype='object')

# Solution# Get the species and petal length columns

petal_len_setosa = iris[iris[:, 4] == b'Iris-setosa', [2]].astype('float')

# Get the second last value

np.unique(np.sort(petal_len_setosa))[-2]

44.按列对2D数组进行排序


url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris = np.genfromtxt(url, delimiter=',', dtype='object')

names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')

# Sort by column position 0: SepalLength

print(iris[iris[:,0].argsort()][:20])

45.在numpy数组中找到最常见的值?


# **给定:**

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris = np.genfromtxt(url, delimiter=',', dtype='object')

# Solution:

vals, counts = np.unique(iris[:, 2], return_counts=True)print(vals[np.argmax(counts)])

46.找到第一次出现的值大于给定值的位置?


# **给定:**

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris = np.genfromtxt(url, delimiter=',', dtype='object')

# Solution: (edit: changed argmax to argwhere. Thanks Rong!)

np.argwhere(iris[:, 3].astype(float) > 1.0)[0]

47.将大于给定值的所有值替换为给定的截止值?


# Input

np.set_printoptions(precision=2)

np.random.seed(100)

a = np.random.uniform(1,50, 20)

# Solution 1: Using np.clip

np.clip(a, a_min=10, a_max=30)

# Solution 2: Using np.where

print(np.where(a < 10, 10, np.where(a > 30, 30, a)))

48.从numpy数组中获取最大n值的位置?


# Input

np.random.seed(100)

a = np.random.uniform(1,50, 20)

# Solution:

print(a.argsort())# > [18 7 3 10 15]

# Solution 2:

np.argpartition(-a, 5)[:5]# > [15 10  3  7 18]

# Below methods will get you the values.# Method 1:

a[a.argsort()][-5:]

# Method 2:

np.sort(a)[-5:]

# Method 3:

np.partition(a, kth=-5)[-5:]

# Method 4:

a[np.argpartition(-a, 5)][:5]

  1. 计算数组中所有可能值的行数?

# 输出包含10列,表示从1到10的数字。这些值是各行中数字的计数。 例如,cell(0,2)的值为2,这意味着数字3在第一行中恰好出现了2次。

np.random.seed(100)

arr = np.random.randint(1,11,size=(6, 10))

# Solution

def counts_of_all_values_rowwise(arr2d):

    # Unique values and its counts row wise

    num_counts_array = [np.unique(row, return_counts=True) for row in arr2d]

    # Counts of all values row wise

    return([[int(b[a==i]) if i in a else 0 for i in np.unique(arr2d)] for a, b in num_counts_array])

# Printprint(np.arange(1,11))

counts_of_all_values_rowwise(arr)

# Example 2:

arr = np.array([np.array(list('bill clinton')), np.array(list('narendramodi')), np.array(list('jjayalalitha'))])print(np.unique(arr))

counts_of_all_values_rowwise(arr)

50.将数组转换为平面一维数组?


# **给定:**

arr1 = np.arange(3)

arr2 = np.arange(3,7)

arr3 = np.arange(7,10)

array_of_arrays = np.array([arr1, arr2, arr3])print('array_of_arrays: ', array_of_arrays)

# Solution 1

arr_2d = np.array([a for arr in array_of_arrays for a in arr])

# Solution 2:

arr_2d = np.concatenate(array_of_arrays)print(arr_2d)

  1. 在numpy中为数组生成单热编码?

# **给定:**

np.random.seed(101)

arr = np.random.randint(1,4, size=6)

arr

# > array([2, 3, 2, 2, 2, 1])

# Solution:def one_hot_encodings(arr):

    uniqs = np.unique(arr)

    out = np.zeros((arr.shape[0], uniqs.shape[0]))

    for i, k in enumerate(arr):

        out[i, k-1] = 1

    return out

one_hot_encodings(arr)

# Method 2:

(arr[:, None] == np.unique(arr)).view(np.int8)

52.创建按分类变量分组的行号?


# **给定:**

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

species = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4)

np.random.seed(100)

species_small = np.sort(np.random.choice(species, size=20))

species_small

print([i for val in np.unique(species_small) for i, grp in enumerate(species_small[species_small==val])])

  1. 根据给定的分类变量创建组ID?

# **给定:**

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

species = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4)

np.random.seed(100)

species_small = np.sort(np.random.choice(species, size=20))

species_small

# Solution:

output = [np.argwhere(np.unique(species_small) == s).tolist()[0][0] for val in np.unique(species_small) for s in species_small[species_small==val]]

# Solution: For Loop version

output = []

uniqs = np.unique(species_small)

for val in uniqs:  # uniq values in group

    for s in species_small[species_small==val]:  # each element in group

        groupid = np.argwhere(uniqs == s).tolist()[0][0]  # groupid

        output.append(groupid)

print(output)

  1. 使用numpy对数组中的项进行排名?

np.random.seed(10)

a = np.random.randint(20, size=10)print('Array: ', a)

# Solutionprint(a.argsort().argsort())print('Array: ', a)

55.使用numpy对多维数组中的项进行排名?


# **给定:**

np.random.seed(10)

a = np.random.randint(20, size=[2,5])print(a)

# Solutionprint(a.ravel().argsort().argsort().reshape(a.shape))

56.在二维numpy数组的每一行中找到最大值?


# Input

np.random.seed(100)

a = np.random.randint(1,10, [5,3])

a

# Solution 1

np.amax(a, axis=1)

# Solution 2

np.apply_along_axis(np.max, arr=a, axis=1)

57.计算二维numpy数组每行的最小值?


# Input

np.random.seed(100)

a = np.random.randint(1,10, [5,3])

a

# Solution

np.apply_along_axis(lambda x: np.min(x)/np.max(x), arr=a, axis=1)

58.在numpy数组中找到重复的记录?


# Input

np.random.seed(100)

a = np.random.randint(0, 5, 10)

## Solution# There is no direct function to do this as of 1.13.3

# Create an all True array

out = np.full(a.shape[0], True)

# Find the index positions of unique elements

unique_positions = np.unique(a, return_index=True)[1]

# Mark those positions as False

out[unique_positions] = False

print(out)

59.找出数字的分组均值?


# Input

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris = np.genfromtxt(url, delimiter=',', dtype='object')

names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')

# Solution# No direct way to implement this. Just a version of a workaround.

numeric_column = iris[:, 1].astype('float')  # sepalwidth

grouping_column = iris[:, 4]  # species

# List comprehension version[[group_val, numeric_column[grouping_column==group_val].mean()] for group_val in np.unique(grouping_column)]

# For Loop version

output = []for group_val in np.unique(grouping_column):

    output.append([group_val, numeric_column[grouping_column==group_val].mean()])

output

60.将PIL图像转换为numpy数组?


from io import BytesIO

from PIL import Image

import PIL, requests

# Import image from URL

URL = 'https://img2.baidu.com/it/u=982549611,3731122317&fm=26&fmt=auto&gp=0.jpg'

response = requests.get(URL)

# Read it as Image

I = Image.open(BytesIO(response.content))

# Optionally resize

I = I.resize([150,150])

# Convert to numpy array

arr = np.asarray(I)

# Optionaly Convert it back to an image and show

im = PIL.Image.fromarray(np.uint8(arr))

Image.Image.show(im)

61.删除numpy数组中所有缺少的值?


a = np.array([1,2,3,np.nan,5,6,7,np.nan])

a[~np.isnan(a)]

62.计算两个数组之间的欧氏距离?


# Input

a = np.array([1,2,3,4,5])

b = np.array([4,5,6,7,8])

# Solution

dist = np.linalg.norm(a-b)

dist

63.在一维数组中找到所有的局部极大值(或峰值)?


a = np.array([1, 3, 7, 1, 2, 6, 0, 1])

doublediff = np.diff(np.sign(np.diff(a)))

peak_locations = np.where(doublediff == -2)[0] + 1

peak_locations

64.从二维数组中减去一维数组,其中一维数组的每一项从各自的行中减去?


# Input

a_2d = np.array([[3,3,3],[4,4,4],[5,5,5]])

b_1d = np.array([1,2,3])

# Solution

print(a_2d - b_1d[:,None])

65.查找数组中项的第n次重复索引?


x = np.array([1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1, 2])

n = 5

# Solution 1: List comprehension[i for i, v in enumerate(x) if v == 1][n-1]

# Solution 2: Numpy version

np.where(x == 1)[0][n-1]

66.将numpy的datetime 64对象转换为datetime的datetime对象?


# **给定:** a numpy datetime64 object

dt64 = np.datetime64('2018-02-25 22:10:10')

# Solutionfrom datetime import datetime

dt64.tolist()

# or

dt64.astype(datetime)

67.计算numpy数组的移动平均值?


# Solution# Source: https://stackoverflow.com/questions/14313510/how-to-calculate-moving-average-using-numpydef moving_average(a, n=3) :

    ret = np.cumsum(a, dtype=float)

    ret[n:] = ret[n:] - ret[:-n]

    return ret[n - 1:] / n

np.random.seed(100)

Z = np.random.randint(10, size=10)print('array: ', Z)# Method 1

moving_average(Z, n=3).round(2)

# Method 2:  # Thanks AlanLRH!# np.ones(3)/3 gives equal weights. Use np.ones(4)/4 for window size 4.

np.convolve(Z, np.ones(3)/3, mode='valid') .

68.在给定起始点、长度和步骤的情况下创建一个numpy数组序列?


length = 10

start = 5

step = 3

def seq(start, length, step):

    end = start + (step*length)

    return np.arange(start, end, step)

seq(start, length, step)

69.填写不规则系列的numpy日期中的缺失日期?


# Input

dates = np.arange(np.datetime64('2018-02-01'), np.datetime64('2018-02-25'), 2)print(dates)

# Solution ---------------

filled_in = np.array([np.arange(date, (date+d)) for date, d in zip(dates, np.diff(dates))]).reshape(-1)

# add the last day

output = np.hstack([filled_in, dates[-1]])

output

# For loop version -------

out = []for date, d in zip(dates, np.diff(dates)):

    out.append(np.arange(date, (date+d)))

filled_in = np.array(out).reshape(-1)

# add the last day

output = np.hstack([filled_in, dates[-1]])

output

70.从给定的一维数组创建步长?


def gen_strides(a, stride_len=5, window_len=5):

    n_strides = ((a.size-window_len)//stride_len) + 1

    # return np.array([a[s:(s+window_len)] for s in np.arange(0, a.size, stride_len)[:n_strides]])

    return np.array([a[s:(s+window_len)] for s in np.arange(0, n_strides*stride_len, stride_len)])

print(gen_strides(np.arange(15), stride_len=2, window_len=4))

文章链接:

101 NumPy Exercises for Data Analysis (Python)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,014评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,796评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,484评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,830评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,946评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,114评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,182评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,927评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,369评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,678评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,832评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,533评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,166评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,885评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,128评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,659评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,738评论 2 351