优化以下代码,# 构建特征矩阵和标签向量 X = [] y = data['Rating'] for index, row in data.iterrows(): features = [] # 添加运行时长区间评分 if pd.notna(row['RunTime']): category1 = pd.cut([row['RunTime']], bins=bins1, labels=labels1)[0] if category1 in avg_runtime_ratings: features.append(avg_runtime_ratings[category1]) else: features.append(0) else: features.append(0) # 添加年份区间评分 if pd.notna(row['year']): category2 = pd.cut([row['year']], bins=bins2, labels=labels2)[0] if category2 in avg_year_ratings: features.append(avg_year_ratings[category2]) else: features.append(0) else: features.append(0) # 添加导演评分 if row.Director in avg_director_ratings: features.append(avg_director_ratings[row.Director]) else: features.append(0) # 添加编剧评分 if row.Writer in avg_writer_ratings: features.append(avg_writer_ratings[row.Writer]) else: features.append(0) # 添加主演评分 casts = row.TopTwoCasts.split(',') if len(casts) == 1: cast = casts[0] if cast in avg_casts_ratings: features.append(avg_casts_ratings[cast]) else: features.append(0) features.extend([0, 0]) else: cast_1, cast_2 = casts if cast_1 in avg_casts_ratings: features.append(avg_casts_ratings[cast_1] * 0.6) else: features.append(0) if cast_2 in avg_casts_ratings: features.append(avg_casts_ratings[cast_2] * 0.4) else: features.append(0) # 添加类型评分 genres = row.Genres.split(',') if len(genres) == 1: genre = genres[0] if genre in avg_genres_ratings: features.append(avg_genres_ratings[genre]) else: features.append(0) features.extend([0, 0]) elif len(genres) == 2: genre_1, genre_2 = genres if genre_1 in avg_genres_ratings: features.append(avg_genres_ratings[genre_1] * 0.6) else: features.append(0) if genre_2 in avg_genres_ratings: features.append(avg_genres_ratings[genre_2] * 0.4) else: features.append(0) features.append(0) else: genre_1, genre_2, genre_3 = genres if genre_1 in avg_genres_ratings: features.append(avg_genres_ratings[genre_1] * 0.4) else: features.append(0) if genre_2 in avg_genres_ratings: features.append(avg_genres_ratings[genre_2] * 0.3) else: features.append(0) if genre_3 in avg_genres_ratings: features.append(avg_genres_ratings[genre_3] * 0.3) else: features.append(0) X.append(features) X = pd.DataFrame(X)
时间: 2024-02-14 21:05:16 浏览: 133
可以优化的地方有:
1. 使用向量化操作代替迭代操作,可以显著提高代码效率;
2. 缩短代码长度和提高可读性,可以通过使用函数和列表推导式等技巧实现。
以下是优化后的代码:
```python
import numpy as np
def get_feature(row, avg_runtime_ratings, avg_year_ratings, avg_director_ratings, avg_writer_ratings, avg_casts_ratings, avg_genres_ratings):
features = []
# 添加运行时长区间评分
runtime = row.get('RunTime', np.nan)
category1 = pd.cut([runtime], bins=bins1, labels=labels1)[0] if pd.notna(runtime) else None
if category1 in avg_runtime_ratings:
features.append(avg_runtime_ratings[category1])
else:
features.append(0)
# 添加年份区间评分
year = row.get('year', np.nan)
category2 = pd.cut([year], bins=bins2, labels=labels2)[0] if pd.notna(year) else None
if category2 in avg_year_ratings:
features.append(avg_year_ratings[category2])
else:
features.append(0)
# 添加导演评分
director = row.get('Director', None)
features.append(avg_director_ratings.get(director, 0))
# 添加编剧评分
writer = row.get('Writer', None)
features.append(avg_writer_ratings.get(writer, 0))
# 添加主演评分
casts = row.get('TopTwoCasts', '').split(',')
if len(casts) == 1:
cast = casts[0]
features.append(avg_casts_ratings.get(cast, 0))
features.extend([0, 0])
else:
cast_1, cast_2 = casts
features.append(avg_casts_ratings.get(cast_1, 0) * 0.6)
features.append(avg_casts_ratings.get(cast_2, 0) * 0.4)
# 添加类型评分
genres = row.get('Genres', '').split(',')
if len(genres) == 1:
genre = genres[0]
features.append(avg_genres_ratings.get(genre, 0))
features.extend([0, 0])
elif len(genres) == 2:
genre_1, genre_2 = genres
features.append(avg_genres_ratings.get(genre_1, 0) * 0.6)
features.append(avg_genres_ratings.get(genre_2, 0) * 0.4)
features.append(0)
else:
genre_1, genre_2, genre_3 = genres
features.append(avg_genres_ratings.get(genre_1, 0) * 0.4)
features.append(avg_genres_ratings.get(genre_2, 0) * 0.3)
features.append(avg_genres_ratings.get(genre_3, 0) * 0.3)
return features
X = data.apply(lambda row: get_feature(row, avg_runtime_ratings, avg_year_ratings, avg_director_ratings, avg_writer_ratings, avg_casts_ratings, avg_genres_ratings), axis=1)
X = pd.DataFrame(X.tolist())
```
阅读全文