pandas教程:Data Transformation 数据变换、删除和替换

文章目录

  • 7.2 Data Transformation(数据变换)
  • 1 删除重复值
  • 2 Transforming Data Using a Function or Mapping(用函数和映射来转换数据)
  • 3 Replacing Values(替换值)
  • 4 Renaming Axis Indexes(重命名Axis Indexes)
  • 5 Discretization and Binning(离散化和装箱)
  • 6 Detecting and Filtering Outliers(检测和过滤异常值)
  • 7 Permutation and Random Sampling(排列和随机采样)
  • 8 Computing Indicator/Dummy Variables(计算指示器/假变量)

7.2 Data Transformation(数据变换)

1 删除重复值

import pandas as pd
import numpy as np
data = pd.DataFrame({'k1': ['one', 'two'] * 3 + ['two'],'k2': [1, 1, 2, 3, 3, 4, 4]})
data
k1k2
0one1
1two1
2one2
3two3
4one3
5two4
6two4

DataFrame方法duplicated返回的是一个boolean Series,表示一个row是否是重复的(根据前一行来判断):

data.duplicated()
0    False
1    False
2    False
3    False
4    False
5    False
6     True
dtype: bool

drop_duplicateds返回一个DataFrame,会删除重复的部分:

data.drop_duplicates()
k1k2
0one1
1two1
2one2
3two3
4one3
5two4

上面两种方法都默认考虑所有列;另外,我们可以指定一部分来检测重复值。假设我们只想检测’k1’列的重复值:

data['v1'] = range(7)
data
k1k2v1
0one10
1two11
2one22
3two33
4one34
5two45
6two46
data.drop_duplicates(['k1'])
k1k2v1
0one10
1two11

duplicateddrop_duplicated默认保留第一次观测到的数值组合。设置keep='last'能返回最后一个:

data.drop_duplicates(['k1', 'k2'], keep='last')
k1k2v1
0one10
1two11
2one22
3two33
4one34
6two46

2 Transforming Data Using a Function or Mapping(用函数和映射来转换数据)

有时候我们可能希望做一些数据转换。比如下面一个例子,有不同种类的肉:

data = pd.DataFrame({'food': ['bacon', 'pulled pork', 'bacon','Pastrami', 'corned beef', 'Bacon','pastrami', 'honey ham', 'nova lox'],'ounces': [4, 3, 12, 6, 7.5, 8, 3, 5, 6]})
data
foodounces
0bacon4.0
1pulled pork3.0
2bacon12.0
3Pastrami6.0
4corned beef7.5
5Bacon8.0
6pastrami3.0
7honey ham5.0
8nova lox6.0

假设你想加一列,表明每种肉来源的动物是什么。我们可以写一个映射:

meat_to_animal = {'bacon': 'pig','pulled pork': 'pig','pastrami': 'cow','corned beef': 'cow','honey ham': 'pig','nova lox': 'salmon'
}

用于seriesmap方法接受一个函数,或是一个字典,包含着映射关系,但这里有一个小问题,有些肉是大写,有些是小写。因此,我们先用str.lower把所有的值变为小写:

lowercased = data['food'].str.lower()
lowercased
0          bacon
1    pulled pork
2          bacon
3       pastrami
4    corned beef
5          bacon
6       pastrami
7      honey ham
8       nova lox
Name: food, dtype: object
data['animal'] = lowercased.map(meat_to_animal)
data
foodouncesanimal
0bacon4.0pig
1pulled pork3.0pig
2bacon12.0pig
3Pastrami6.0cow
4corned beef7.5cow
5Bacon8.0pig
6pastrami3.0cow
7honey ham5.0pig
8nova lox6.0salmon

我们也可以用一个函数解决上面的问题:

data['food'].map(lambda x: meat_to_animal[x.lower()])
0       pig
1       pig
2       pig
3       cow
4       cow
5       pig
6       cow
7       pig
8    salmon
Name: food, dtype: object

使用map是一个很简便的方法,用于element-wise转换和其他一些数据清洗操作。

3 Replacing Values(替换值)

其实fillna是一个特殊换的替换操作。map可以用于修改一个object里的部分值,但是replace能提供一个更简单和更灵活的方法做到这点。下面是一个series

data = pd.Series([1., -999., 2., -999., -1000., 3.])
data
0       1.0
1    -999.0
2       2.0
3    -999.0
4   -1000.0
5       3.0
dtype: float64

这里-999可能是用来表示缺失值的标识符。用NA来替代的话,用replace,会产生一个新series(除非使用inplace=True):

data.replace(-999, np.nan)
0       1.0
1       NaN
2       2.0
3       NaN
4   -1000.0
5       3.0
dtype: float64

如果想要一次替换多个值,直接用一个list即可:

data.replace([-999, -1000], np.nan)
0    1.0
1    NaN
2    2.0
3    NaN
4    NaN
5    3.0
dtype: float64

对于不同的值用不同的替换值,也是导入一个list

data.replace([-999, -1000], [np.nan, 0])
0    1.0
1    NaN
2    2.0
3    NaN
4    0.0
5    3.0
dtype: float64

参数也可以是一个dict:

data.replace({-999: np.nan, -1000: 0})
0    1.0
1    NaN
2    2.0
3    NaN
4    0.0
5    3.0
dtype: float64

注意:data.replace方法和data.str.replace方法是不同的,后者会对string进行element-wise替换。

4 Renaming Axis Indexes(重命名Axis Indexes)

像是series里的value一样,axis label也能类似地是函数或映射来转换,产生一个新的object。当然也可以设置in-place不产生新的数据:

data = pd.DataFrame(np.arange(12).reshape((3, 4)),index=['Ohio', 'Colorado', 'New York'],columns=['one', 'two', 'three', 'four'])
data
onetwothreefour
Ohio0123
Colorado4567
New York891011

series相同,axis index有一个map方法:

transform = lambda x: x[:4].upper()
transform
<function __main__.<lambda>>
data.index
Index(['Ohio', 'Colorado', 'New York'], dtype='object')
data.index.map(transform)
array(['OHIO', 'COLO', 'NEW '], dtype=object)

可以赋值给index,以in-place的方式修改DataFrame

data.index = data.index.map(transform)
data
onetwothreefour
OHIO0123
COLO4567
NEW891011

如果你想要创建一个转换后的版本,而且不用修改原始的数据,可以用rename:

data.rename(index=str.title, columns=str.upper)
ONETWOTHREEFOUR
Ohio0123
Colo4567
New891011

注意,rename能用于dict一样的object

data.rename(index={'OHIO': 'INDIANA'},columns={'three': 'pekaboo'})
onetwopekaboofour
INDIANA0123
COLO4567
NEW891011

rename能让你避免陷入手动赋值给indexcolumns的杂务中。可以用inplace直接修改原始数据:

data.rename(index={'OHIO': 'INDIANA'}, inplace=True)
data
onetwothreefour
INDIANA0123
COLO4567
NEW891011

5 Discretization and Binning(离散化和装箱)

连续型数据经常被离散化或分散成bins(分箱)来分析。假设你有一组数据,你想把人分到不同的年龄组里:

ages = [20, 22, 25, 27, 21, 23, 37, 31, 61, 45, 41, 32]

我们把这些分到四个bin里,18~25, 26~35, 36~60, >60。可以用pandas里的cut

bins = [18, 25, 35, 60, 100]cats = pd.cut(ages, bins)cats
[(18, 25], (18, 25], (18, 25], (25, 35], (18, 25], ..., (25, 35], (60, 100], (35, 60], (35, 60], (25, 35]]
Length: 12
Categories (4, object): [(18, 25] < (25, 35] < (35, 60] < (60, 100]]

返回的是一个特殊的Categorical object。我们看到的结果描述了pandas.cut如何得到bins。可以看作是一个string数组用来表示bin的名字,它内部包含了一个categories数组,用来记录不同类别的名字,并伴有表示ageslabel(可以通过codes属性查看):

cats.codes
array([0, 0, 0, 1, 0, 0, 2, 1, 3, 2, 2, 1], dtype=int8)
cats.categories
Index(['(18, 25]', '(25, 35]', '(35, 60]', '(60, 100]'], dtype='object')
pd.value_counts(cats)
(18, 25]     5
(35, 60]     3
(25, 35]     3
(60, 100]    1
dtype: int64

这里pd.value_counts(cats)pandas.cutbin的数量。

这里我们注意一下区间。括号表示不包含,方括号表示包含。你可以自己设定哪一边关闭(right=False):

pd.cut(ages, [18, 26, 36, 61, 100], right=False)
[[18, 26), [18, 26), [18, 26), [26, 36), [18, 26), ..., [26, 36), [61, 100), [36, 61), [36, 61), [26, 36)]
Length: 12
Categories (4, object): [[18, 26) < [26, 36) < [36, 61) < [61, 100)]

你也可以用一个list或数组给labels选项来设定bin的名字:

group_names = ['Youth', 'YoungAdult', 'MiddleAged', 'Senior']
pd.cut(ages, bins, labels=group_names)
[Youth, Youth, Youth, YoungAdult, Youth, ..., YoungAdult, Senior, MiddleAged, MiddleAged, YoungAdult]
Length: 12
Categories (4, object): [Youth < YoungAdult < MiddleAged < Senior]

如果你只是给一个bins的数量来cut,而不是自己设定每个bind的范围,cut会根据最大值和最小值来计算等长的bins。比如下面我们想要做一个均匀分布的四个bins

data = np.random.rand(20)
pd.cut(data, 4, precision=2)
[(0.77, 0.98], (0.33, 0.55], (0.77, 0.98], (0.55, 0.77], (0.55, 0.77], ..., (0.77, 0.98], (0.11, 0.33], (0.11, 0.33], (0.33, 0.55], (0.11, 0.33]]
Length: 20
Categories (4, object): [(0.11, 0.33] < (0.33, 0.55] < (0.55, 0.77] < (0.77, 0.98]]

precision=2选项表示精确到小数点后两位。

一个近似的函数,qcut,会按照数据的分位数来分箱。取决于数据的分布,用cut通常不能保证每一个bin有一个相同数量的数据点。而qcut是按百分比来切的,所以可以得到等数量的bins

data = np.random.randn(1000) # Normally distributed
cats = pd.qcut(data, 4) # Cut into quartiles
cats
[(-0.717, -0.0981], (-0.717, -0.0981], (-0.0981, 0.639], (0.639, 3.434], [-2.86, -0.717], ..., (-0.0981, 0.639], (-0.717, -0.0981], (-0.0981, 0.639], (0.639, 3.434], (-0.0981, 0.639]]
Length: 1000
Categories (4, object): [[-2.86, -0.717] < (-0.717, -0.0981] < (-0.0981, 0.639] < (0.639, 3.434]]
pd.value_counts(cats)
(0.639, 3.434]       250
(-0.0981, 0.639]     250
(-0.717, -0.0981]    250
[-2.86, -0.717]      250
dtype: int64

类似的,在cut中我们可以自己指定百分比:

cats2 = pd.cut(data, [0, 0.1, 0.5, 0.9, 1.]) # 累进的百分比
cats2
[NaN, NaN, (0.1, 0.5], NaN, NaN, ..., (0.1, 0.5], NaN, (0.5, 0.9], NaN, (0.5, 0.9]]
Length: 1000
Categories (4, object): [(0, 0.1] < (0.1, 0.5] < (0.5, 0.9] < (0.9, 1]]
pd.value_counts(cats2)
(0.1, 0.5]    135
(0.5, 0.9]    124
(0, 0.1]       40
(0.9, 1]       21
dtype: int64

在之后的章节我们还会用到cutqcut,这些离散函数对于量化和群聚分析很有用。

6 Detecting and Filtering Outliers(检测和过滤异常值)

过滤或转换异常值是数组操作的一个重头戏。下面的DataFrame有正态分布的数据:

data = pd.DataFrame(np.random.randn(1000, 4))
data.describe()
0123
count1000.0000001000.0000001000.0000001000.000000
mean0.0109530.0129280.033165-0.031257
std1.0116211.0133411.0043560.996333
min-2.994342-4.328036-3.303616-3.133495
25%-0.654483-0.662177-0.644982-0.670813
50%-0.000637-0.0332410.050481-0.074641
75%0.7231000.7258390.7084520.643418
max3.3184993.3530013.0028533.002868

假设我们想要找一个列中,绝对值大于3的数字:

data.head()
0123
01.1237660.9339200.4947550.690507
12.5136360.575393-0.3235900.586833
2-0.335958-0.8437350.302201-0.490675
3-1.307658-0.4856701.6127870.210169
4-0.793757-0.693757-1.7183670.515088
col = data[2]
col.head()
0    0.494755
1   -0.323590
2    0.302201
3    1.612787
4   -1.718367
Name: 2, dtype: float64
col[np.abs(col) > 3]
339   -3.303616
932    3.002853
Name: 2, dtype: float64

选中所有绝对值大于3的行,可以用any方法在一个boolean DataFrame上:

data[(np.abs(data) > 3)].head()
0123
0NaNNaNNaNNaN
1NaNNaNNaNNaN
2NaNNaNNaNNaN
3NaNNaNNaNNaN
4NaNNaNNaNNaN
data[(np.abs(data) > 3).any(1)] # any中axis=1表示column
0123
221.0757280.2500000.951303-3.133495
1550.064837-4.3280361.121061-0.574203
224-0.289148-2.9121160.332218-3.129604
339-0.098352-0.610929-3.303616-2.072304
6090.9832401.3726330.0181723.002868
7353.318499-2.573122-1.515901-1.204596
8221.8103963.353001-1.283856-1.166749
856-0.7950703.204789-0.2116421.278828
9320.898147-0.9618503.002853-0.128494

下面是把绝对值大于3的数字直接变成-3或3:

data[np.abs(data) > 3] = np.sign(data) * 3
data[21:23]
0123
21-0.066111-1.1590640.518720-0.284596
221.0757280.2500000.951303-3.000000
data.describe()
0123
count1000.0000001000.0000001000.0000001000.000000
mean0.0106340.0136980.033466-0.030997
std1.0106291.0067681.0033830.995521
min-2.994342-3.000000-3.000000-3.000000
25%-0.654483-0.662177-0.644982-0.670813
50%-0.000637-0.0332410.050481-0.074641
75%0.7231000.7258390.7084520.643418
max3.0000003.0000003.0000003.000000

np.sign(data)会根据值的正负号来得到1或-1:

np.sign(data).head()
0123
01.01.01.01.0
11.01.0-1.01.0
2-1.0-1.01.0-1.0
3-1.0-1.01.01.0
4-1.0-1.0-1.01.0

7 Permutation and Random Sampling(排列和随机采样)

排列(随机排序)一个seriesDataFrame中的row,用numpy.random.permutation函数很容易就能做到。调用permutation的时候设定好你想要进行排列的axis,会产生一个整数数组表示新的顺序:

df = pd.DataFrame(np.arange(5 * 4).reshape((5, 4)))
df
0123
00123
14567
2891011
312131415
416171819
sampler = np.random.permutation(5)
sampler
array([4, 3, 2, 1, 0])

这个数组能被用在基于iloc上的indexingtake函数:

df.take(sampler)
0123
416171819
312131415
2891011
14567
00123

为了选中一个随机的子集,而且没有代替功能(既不影响原来的值,返回一个新的seriesDataFrame),可以用sample方法:

df.sample(n=3)
0123
00123
312131415
416171819

如果想要生成的样本带有替代功能(即允许重复),给sample中设定replace=True:

choices = pd.Series([5, 7, -1, 6, 4])draws = choices.sample(n=10, replace=True)draws
4    4
4    4
1    7
1    7
1    7
1    7
4    4
3    6
1    7
1    7
dtype: int64

8 Computing Indicator/Dummy Variables(计算指示器/假变量)

另一种在统计模型上的转换或机器学习应用是把一个categorical variable(类别变量)变为一个dummy or indicator matrix(假或指示器矩阵)。如果DataFrame中的一列有k个不同的值,我们可以用一个矩阵或DataFrame用k列来表示,1或0。pandas有一个get_dummies函数实现这个工作,当然,你自己设计一个其实也不难。这里举个例子:

df = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'b'],'data1': range(6)})
df
data1key
00b
11b
22a
33c
44a
55b
pd.get_dummies(df['key'])
abc
00.01.00.0
10.01.00.0
21.00.00.0
30.00.01.0
41.00.00.0
50.01.00.0

在一些情况里,如果我们想要给column加一个prefix, 可以用data.get_dummies里的prefix参数来实现:

dummies = pd.get_dummies(df['key'], prefix='key')
df_with_dummy = df[['data1']].join(dummies)
df_with_dummy
data1key_akey_bkey_c
000.01.00.0
110.01.00.0
221.00.00.0
330.00.01.0
441.00.00.0
550.01.00.0

如果DataFrame中的a row属于多个类别,事情会变得复杂一些。我们来看一下MoviesLens 1M 数据集:

mnames = ['movie_id', 'title', 'genres']
movies = pd.read_table('../datasets/movielens/movies.dat', sep='::',header=None, names=mnames, engine='python')
movies[:10]
movie_idtitlegenres
01Toy Story (1995)Animation|Children's|Comedy
12Jumanji (1995)Adventure|Children's|Fantasy
23Grumpier Old Men (1995)Comedy|Romance
34Waiting to Exhale (1995)Comedy|Drama
45Father of the Bride Part II (1995)Comedy
56Heat (1995)Action|Crime|Thriller
67Sabrina (1995)Comedy|Romance
78Tom and Huck (1995)Adventure|Children's
89Sudden Death (1995)Action
910GoldenEye (1995)Action|Adventure|Thriller

给每个genre添加一个指示变量比较麻烦。首先我们先取出所有不同的类别:

all_genres = []for x in movies.genres:all_genres.extend(x.split('|'))genres = pd.unique(all_genres)
genres
array(['Animation', "Children's", 'Comedy', 'Adventure', 'Fantasy','Romance', 'Drama', 'Action', 'Crime', 'Thriller', 'Horror','Sci-Fi', 'Documentary', 'War', 'Musical', 'Mystery', 'Film-Noir','Western'], dtype=object)

一种构建indicator dataframe的方法是先构建一个全是0的DataFrame

zero_matrix = np.zeros((len(movies), len(genres)))zero_matrix.shape
(3883, 18)
dummies = pd.DataFrame(zero_matrix, columns=genres)
dummies.head()
AnimationChildren'sComedyAdventureFantasyRomanceDramaActionCrimeThrillerHorrorSci-FiDocumentaryWarMusicalMysteryFilm-NoirWestern
00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0
10.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0
20.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0
30.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0
40.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0

然后迭代每一部movie,并设置每一行中的dummies为1。使用dummies.columns来计算每一列的genre的指示器:

gen = movies.genres[0]
gen.split('|')
['Animation', "Children's", 'Comedy']
dummies.columns.get_indexer(gen.split('|'))
array([0, 1, 2])

然后,使用.iloc,根据索引来设定值:

for i, gen in enumerate(movies.genres):indices = dummies.columns.get_indexer(gen.split('|'))dummies.iloc[i, indices] = 1
dummies.head()
AnimationChildren'sComedyAdventureFantasyRomanceDramaActionCrimeThrillerHorrorSci-FiDocumentaryWarMusicalMysteryFilm-NoirWestern
01.01.01.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0
10.01.00.01.01.00.00.00.00.00.00.00.00.00.00.00.00.00.0
20.00.01.00.00.01.00.00.00.00.00.00.00.00.00.00.00.00.0
30.00.01.00.00.00.01.00.00.00.00.00.00.00.00.00.00.00.0
40.00.01.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0

然后,我们可以结合这个和movies:

movies_windic = movies.join(dummies.add_prefix('Genre_'))
movies_windic.iloc[0]
movie_id                                       1
title                           Toy Story (1995)
genres               Animation|Children's|Comedy
Genre_Animation                                1
Genre_Children's                               1
Genre_Comedy                                   1
Genre_Adventure                                0
Genre_Fantasy                                  0
Genre_Romance                                  0
Genre_Drama                                    0
Genre_Action                                   0
Genre_Crime                                    0
Genre_Thriller                                 0
Genre_Horror                                   0
Genre_Sci-Fi                                   0
Genre_Documentary                              0
Genre_War                                      0
Genre_Musical                                  0
Genre_Mystery                                  0
Genre_Film-Noir                                0
Genre_Western                                  0
Name: 0, dtype: object

对于一个很大的数据集,这种构建多个成员指示变量的方法并不会加快速度。写一个低层级的函数来直接写一个numpy array,并把写过整合到DataFrame会更快一些。

一个有用的recipe诀窍是把get_dummies和离散函数(比如cut)结合起来:

np.random.seed(12345)
values = np.random.rand(10)
values
array([ 0.92961609,  0.31637555,  0.18391881,  0.20456028,  0.56772503,0.5955447 ,  0.96451452,  0.6531771 ,  0.74890664,  0.65356987])
bins = [0, 0.2, 0.4, 0.6, 0.8, 1.]
pd.cut(values, bins)
[(0.8, 1], (0.2, 0.4], (0, 0.2], (0.2, 0.4], (0.4, 0.6], (0.4, 0.6], (0.8, 1], (0.6, 0.8], (0.6, 0.8], (0.6, 0.8]]
Categories (5, object): [(0, 0.2] < (0.2, 0.4] < (0.4, 0.6] < (0.6, 0.8] < (0.8, 1]]
pd.get_dummies(pd.cut(values, bins))
(0, 0.2](0.2, 0.4](0.4, 0.6](0.6, 0.8](0.8, 1]
00.00.00.00.01.0
10.01.00.00.00.0
21.00.00.00.00.0
30.01.00.00.00.0
40.00.01.00.00.0
50.00.01.00.00.0
60.00.00.00.01.0
70.00.00.01.00.0
80.00.00.01.00.0
90.00.00.01.00.0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/133931.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

API接口安全设计

简介 HTTP接口是互联网各系统之间对接的重要方式之一&#xff0c;使用HTTP接口开发和调用都很方便&#xff0c;也是被大量采用的方式&#xff0c;它可以让不同系统之间实现数据的交换和共享。 由于HTTP接口开放在互联网上&#xff0c;所以我们就需要有一定的安全措施来保证接口…

#龙迅视频转换IC LT7911D是一款高性能Type-C/DP/EDP 转MIPI®DSI/CSI/LVDS 芯片,适用于VR/显示应用。

1.说明 应用功能&#xff1a;LT7911D适用于DP1.2转MIPIDSI/MIPICSI/LVDS&#xff0c;EDP转MIPIDSI/MIPICSI/LVDS&#xff0c;TYPE-C转MIPIDSI/MIPICSI/LVDS应用方案 分辨率&#xff1a;单PORT高达4K30HZ&#xff0c;双PORT高达4K 60HZ 工作温度范围&#xff1a;−40C to 85C 产…

多比特杯武汉工程大学第六届ACM新生赛 A,L

为什么要演奏春日影&#xff01;&#xff01;&#xff01; 看到题目所说&#xff0c;若 b i b_i bi​在 i i i 之前&#xff0c;则…,那么很容易联想到拓扑排序&#xff0c;再仔细看题&#xff0c;对于每个 b i b_i bi​,我们都想其对应的 i 进行连边&#xff0c;那么我们很容…

webgoat-client side客户端问题

client side Bypass front-end restrictions 用户对 Web 应用程序的前端有很大程度的控制权。 它们可以更改 HTML 代码&#xff0c;有时也可以更改脚本。这就是为什么 需要特定输入格式的应用也应在服务器端进行验证&#xff0c;而不是只在前端做限制。 0x02 先提交请求&am…

win10虚机扩容C盘

需求&#xff1a; 在虚机管理平台上&#xff0c;将win10虚机的C盘空间扩容至200G&#xff0c;当前空间为100G 操作步骤 1.在虚机平台上&#xff0c;将硬盘1的大小增加至200G 如下图 点击保存&#xff1b; 查看win10虚机&#xff0c;发现C盘空间还是100G&#xff0c;如下图…

使用Redis实现缓存及对应问题解决

一、为什么需要Redis作缓存&#xff1f; 在业务场景中&#xff0c;如果有些数据需要极高频的存取&#xff0c;每次都要在mysql中查询的话代价太大&#xff0c;假如有一个存在于客户端和mysql之间的存储空间&#xff0c;每次可以在这空间中进行存取操作&#xff0c;就会减轻mys…

go程序获取工作目录及可执行程序存放目录的方法-linux

简介 工作目录 通常就是指用户启动应用程序时&#xff0c;用户当时所在的文件夹的绝对路径。 如&#xff1a;root用户登录到linux系统后&#xff0c;一顿cd&#xff08;change directory&#xff09;后, 到了/tmp文件夹下。此时&#xff0c;用户要启动某个应用程序&#xff0…

mediapipe流水线分析 二

目标检测 Graph 一 流水线上游输入处理 1 TfLiteConverterCalculator 将输入的数据转换成tensorflow api 支持的Tensor TfLiteTensor 并初始化相关输入输出节点 &#xff0c;该类的业务主要通过 interpreter std::unique_ptrtflite::Interpreter interpreter_ nullptr; 实现…

Python教程---Python基础语法2

1.变量和字面量(常量) 字面量就是一个一个的值&#xff0c;比如&#xff1a;1&#xff0c;2&#xff0c;3&#xff0c;4&#xff0c;5&#xff0c;6&#xff0c;‘HELLO’ 字面量所表示的意思就是它的字面的值&#xff0c;在程序中可以直接使用字面量 变量&#xff08;varia…

利用大语言模型(LLM )提高工作效率

日常工作就是面向 google/ 百度编程&#xff0c;除了给变量命名是手动输入&#xff0c;大多时候就是通过搜索引擎拷贝别人的代码&#xff0c;或者找到旧项目一段代码拷贝过来使用。这无疑是开发人员的真实写照&#xff1b;然而&#xff0c;通过搜索引擎搜索答案&#xff0c;无疑…

Go 面向对象,多态

面向对象 工程结构 新建一个oop.go package _oop // Package _oop 引用名称import ("fmt""strconv" )// GIRL 常量 const (// GIRL 自增GIRL Gender iotaFIRSTSECONDTHIRD )type Gender uint8 // 无符号的8位整数类型// User 结构体 type User struct…

Vue 跨域的两种解决方式

一、通过 proxy 解决跨域 1.1 baseURL 配置 对 axios 二次封装时&#xff0c;baseURL 设置为 /api。 const serviceAxios axios.create({baseURL: /api,timeout: 10000, // 请求超时设置withCredentials: false, // 跨域请求是否需要携带 cookie });1.2 vue.config.js 配置…

SQLite 3.44.0 发布!

SQLite 开发团队于 2023 年 11 月 01 日发布了 SQLite 3.44.0 版本&#xff0c;带来了一些 SQL 和优化器增强&#xff0c;本文给大家做一个简要分分析。 新增 concat() 函数 新版本增加了两个连接字符串的函数&#xff1a;concat() 以及 concat_ws()。它们可以兼容 PostgreSQ…

代码冲突解决

远程仓库修改 本地代码修改 接下来我们push一下 如果使用IDE 冲突内容如下&#xff1a; 我们可以使用自带的工具进行修改 我们选择接受自己改动的即可 如果使用git工具怎么去处理呢 远程分支是这样 本地是这样的 add和commit之后&#xff0c;再pull&#xff0c;最后pus…

若依:用sqlite3随便掰饬掰饬

“若依”这个开源项目&#xff0c;感觉是外包公司标配了啊&#xff0c;都在用。从README感觉像是某位阿里员工的工作之余的小整理。对于SprintBoot&#xff0c;个人感觉太重型&#xff0c;不过人家生态起来了&#xff0c;不是那么容易玩完。但是随着VMware被博通收购&#xff0…

关键词搜索亚马逊商品数据接口(标题|主图|SKU|价格|优惠价|掌柜昵称|店铺链接|店铺所在地)

亚马逊提供了API接口来获取商品数据。其中&#xff0c;关键词搜索亚马逊商品接口&#xff08;item_search-按关键字搜索亚马逊商品接口&#xff09;可以用于获取按关键字搜索到的商品数据。 通过该接口&#xff0c;您可以使用API Key和API Secret来认证身份&#xff0c;并使用…

BP神经网络的数据分类——语音特征信号分类

大家好&#xff0c;我是带我去滑雪&#xff01; BP神经网络&#xff0c;也称为反向传播神经网络&#xff0c;是一种常用于分类和回归任务的人工神经网络&#xff08;ANN&#xff09;类型。它是一种前馈神经网络&#xff0c;通常包括输入层、一个或多个隐藏层和输出层。BP神经网…

关于iOS:如何使用SwiftUI调整图片大小?

How to resize Image with SwiftUI? 我在Assets.xcassets中拥有很大的形象。 如何使用SwiftUI调整图像大小以缩小图像&#xff1f; 我试图设置框架&#xff0c;但不起作用&#xff1a; 1 2 Image(room.thumbnailImage) .frame(width: 32.0, height: 32.0) 在Image上应用…

浅析刚入门Python初学者的注意事项

文章目录 一、注意你的Python版本1.print()函数2.raw_input()与input()3.比较符号&#xff0c;使用!替换<>4.repr函数5.exec()函数 二、新手常遇到的问题1、如何写多行程序&#xff1f;2、如何执行.py文件&#xff1f;3、and&#xff0c;or&#xff0c;not4、True和False…

Unity项目转微信小游戏保姆教程,繁杂问题解决,及微信小游戏平台简单性能测试

前言 借着某人需求&#xff0c;做了一波简单的技术调研&#xff1a;将Unity项目转换为微信小游戏。 本文主要内容&#xff1a;Unity转换小游戏的步骤&#xff0c;遇到问题的解决方法&#xff0c;以及简单的性能测试对比 微信小游戏的限制 微信小游戏对程序包体大小有严格限制…