python learning note

python基本概念

python是一种解释型,面向对象,动态数据类型的高级程序设计语言.
(search for python extension packages to download extra packages)

python包

Pandas

In particular, pandas offers data structures and operations for manipulating numerical tables and time series:

  1. pd.Series
// create a series
In [3]: s = pd.Series([1, 1, 12, 6, np.nan])

In [4]: s
Out[4]: 
0    1.0
1    1.0
2    12.0
3    6.0
5    NaN
dtype: float64

Slice operation in series:
1\ To print elements from beginning to a range use [:Index] (does not include Index)
2\ To print elements till end-use [:-Index] (does not include Index)
3\ To print elements from specific Index till the end use [Index:] (does not include Index)
4\ To print elements within a range, use [Start Index:End Index]
5\ To print whole Series with the use of slicing operation, use [:]
6\ To print the whole Series in reverse order, use [::-1]

  1. pd.DataFrame:
// create a series
In [1]: df = pd.Series.to_frame(s)
In [2]: df.column = ['Rate']


//selecting columns:
1\ use .loc to do conditional selection.

data['Cost'].loc[data['DMA']=='Other'].sum()/data['Cost'].sum()
  1. dictionary
//create a dictionary
dict = {'Pheobe':[95,93,11,65],'luis':[99,97,66,70] }
  1. how to join two tables
df_off = df_off.set_index('trans_dt').join(total_gmb.set_index('trans_dt'),
                                          how='left', lsuffix='',
                                          rsuffix='_total', sort=False).reset_index()

Encountered errors and solutions:

1. datetime package:

AttributeError: 'datetime' module has no attribute 'strptime'

Solutions:

from datetime import datetime

or

#module  class    method
datetime.datetime.strptime(date, "%Y-%m-%d")

2. space in the string

In[5]: validation_data1['VERTICAL'].unique()
Out[5]: array(['Home\xa0&\xa0Garden', 'Other', 'Electronics'], dtype=object)

\xa0 is a non-breaking space in Latin1 (ISO 8859-1), also chr(160). Here is how to replace it with normal space:

string = string.replace(u'\xa0', u' ')

3. ‘pandas’ has no attribute ‘ewma’

    # 对size个数进行加权移动平均
    rol_weighted_mean = pd.ewma(timeSeries, span=size)
    改为
    rol_weighted_mean = pd.DataFrame.ewm(timeSeries, span=size).mean()

4. 解决plt图像交叠问题:
在这里插入图片描述

fig = plt.figure()
fig.tight_layout()
or
plt.tight_layout()

在这里插入图片描述
5. 怎么从string中选取特定字符

original array:
array(['Evergreen, Core, GEO1',
       'PL_TS_Control, Evergreen, Promoted Listings',
       'Evergreen, GEO4, Core', 'Evergreen, GEO2, Core',
       'Evergreen, Core, GEO 5', 'Evergreen, GEO3, Core',
       'Evergreen, PL_TS_Treatment, Promoted Listings',
       'Trading Cards, Strategic', 'CR, Strategic',
       'Evergreen, FBK, Core', 'Evergreen, Core, CTRL',
       'Watches, Strategic', 'Watches_SSC_Treatment_GEOB, Strategic',
       'Sneakers, Strategic', 'Watches_SSC_Control_GEOA, Strategic',
       'Evergreen, Core', 'Evergreen, Core, C2C',
       'Strategic, Sneaker_Showcase'], dtype=object)
       
data_test=data.loc[data['Labels on Campaign'].apply(lambda x: (x.split(',',1)[1] == ' Strategic') or (x.split(',',1)[0] == 'Strategic'))]

array(['Trading Cards, Strategic', 'CR, Strategic', 'Watches, Strategic',
       'Watches_SSC_Treatment_GEOB, Strategic', 'Sneakers, Strategic',
       'Watches_SSC_Control_GEOA, Strategic',
       'Strategic, Sneaker_Showcase'], dtype=object)
  1. 浅拷贝和深拷贝
dict2 = dict1          # 浅拷贝: 引用对象
dict3 = dict1.copy()   # 浅拷贝:深拷贝父对象(一级目录),子对象(二级目录)不拷贝,还是引用
  1. is 和 == 的区别
    ‘b is a’ returns ‘True’ when a and b point to the same object
    ‘b == a’ returns ‘True’ when a and b have the same variables
a = [1,2,3]
b = a                      #copy a
c = a[:]                   #copy a using slice operator
if b == a:
    print('True1')
if b is a:
    print('True2')
if c == a:
    print('True3')
if c is a:
    print('True4')

[out]: True1
       True2
       True3
Q-learning is a popular reinforcement learning algorithm used to solve Markov Decision Processes (MDPs). In Python, you can implement Q-learning using various libraries such as NumPy and TensorFlow. Here's a basic implementation of Q-learning in Python: ```python import numpy as np # Define the Q-learning function def q_learning(env, num_episodes, learning_rate, discount_factor, epsilon): # Initialize the Q-table num_states = env.observation_space.n num_actions = env.action_space.n Q = np.zeros((num_states, num_actions)) # Q-learning loop for episode in range(num_episodes): state = env.reset() done = False while not done: # Choose an action using epsilon-greedy policy if np.random.uniform() < epsilon: action = env.action_space.sample() else: action = np.argmax(Q[state]) # Perform the action and observe the next state and reward next_state, reward, done, _ = env.step(action) # Update the Q-table Q[state, action] += learning_rate * (reward + discount_factor * np.max(Q[next_state]) - Q[state, action]) state = next_state return Q # Example usage env = gym.make('your_environment') # Replace 'your_environment' with the name of your environment num_episodes = 1000 learning_rate = 0.1 discount_factor = 0.9 epsilon = 0.1 Q_table = q_learning(env, num_episodes, learning_rate, discount_factor, epsilon) ``` In this example, `env` represents the environment you want to train your agent on (e.g., a grid world). `num_episodes` is the number of episodes the agent will play to learn the optimal policy. `learning_rate` controls the weight given to the new information compared to the old information, while `discount_factor` determines the importance of future rewards. `epsilon` is the exploration rate that balances exploration and exploitation. Note that you need to install the required libraries (e.g., NumPy and gym) before running the code.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值