SWERC 2022-2023 - Online Mirror (Unrated, ICPC Rules, Teams Preferred)
时间: 2024-06-12 19:09:18 浏览: 5
SWERC 2022-2023 - Online Mirror (Unrated, ICPC Rules, Teams Preferred) 是一个编程竞赛的镜像赛事。它是针对SWERC(南欧洲区域赛)的一个在线版本,也可以理解为在SWERC结束后进行的一个模仿赛。此次镜像赛的规则是不评分的(即Unrated),采用国际大学生程序设计竞赛(ICPC)的规则,并且更加偏向于团队形式的比赛。
相关问题
这个问题的意思是给定电影评分数据集(MovieLens),写一段Python代码,为用户-电影评分矩阵进行余弦相似度计算,并找出用户ID为17的用户,推荐给他没看过的电影(即该用户没有评分的电影)。
好的,我明白了您的问题。为了计算用户-电影评分矩阵的余弦相似度,您可以使用scikit-learn库中的cosine_similarity函数。具体的步骤如下:
1. 导入需要的库和数据集
```python
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
# 读取数据集
ratings = pd.read_csv('ratings.csv')
```
2. 对评分数据进行预处理,生成用户-电影评分矩阵
```python
# 生成用户-电影评分矩阵
user_movie_matrix = ratings.pivot(index='userId', columns='movieId', values='rating').fillna(0)
```
3. 计算余弦相似度矩阵
```python
# 计算余弦相似度矩阵
cosine_sim_matrix = cosine_similarity(user_movie_matrix)
```
4. 找到用户ID为17的用户,并根据余弦相似度值排序推荐电影
```python
# 找到用户ID为17的用户
user_id = 17
# 获取用户17的电影评分向量
user_ratings = user_movie_matrix.loc[user_id].values.reshape(1, -1)
# 计算用户17与其他用户的余弦相似度值
user_similarities = cosine_sim_matrix[user_id-1].reshape(-1, 1)
# 计算用户17对电影的评分预测值
prediction = user_similarities.T.dot(user_movie_matrix.values) / user_similarities.sum()
# 将预测值转换为Series对象
prediction_series = pd.Series(prediction.reshape(-1), index=user_movie_matrix.columns)
# 找到用户17未评分的电影
unrated_movies = user_movie_matrix.loc[user_id][user_movie_matrix.loc[user_id] == 0].index
# 对未评分的电影进行推荐,按照预测评分值从高到低排序
recommendations = prediction_series[unrated_movies].sort_values(ascending=False)
```
以上代码可以为用户ID为17的用户推荐未评分的电影。如果您想要更深入地学习推荐算法,可以参考一些经典的推荐算法,比如基于协同过滤的算法、基于内容的推荐算法和基于深度学习的推荐算法等。
读取"ramen-rating.csv"文件,并进行数据预处理; 统计各个国家拉面品牌数量,并绘制排名前10位国家的直方图; 找出各个国家最受欢迎的拉面品牌和包装类型; 统计各个国家各个品牌的stars平均值; 给出一些交叉列联表,行列自己选。
1. 数据预处理:
```python
import pandas as pd
# 读取数据
df = pd.read_csv("ramen-rating.csv")
# 删除无用列
df.drop(['Review #', 'Top Ten'], axis=1, inplace=True)
# 处理包装列
df.loc[df['Style'].str.contains('cup', case=False), 'Packaging'] = 'Cup'
df.loc[df['Style'].str.contains('bowl', case=False), 'Packaging'] = 'Bowl'
df.loc[df['Style'].str.contains('box', case=False), 'Packaging'] = 'Box'
df.loc[df['Style'].str.contains('tray', case=False), 'Packaging'] = 'Tray'
# 处理品牌列
df.loc[df['Brand'].str.contains('nissin', case=False), 'Brand'] = 'Nissin'
df.loc[df['Brand'].str.contains('maruchan', case=False), 'Brand'] = 'Maruchan'
df.loc[df['Brand'].str.contains('samyang', case=False), 'Brand'] = 'Samyang'
df.loc[df['Brand'].str.contains('sapporo', case=False), 'Brand'] = 'Sapporo Ichiban'
# 处理国家列
df.loc[df['Country'].str.contains('usa', case=False), 'Country'] = 'United States'
df.loc[df['Country'].str.contains('south korea', case=False), 'Country'] = 'South Korea'
df.loc[df['Country'].str.contains('hong kong', case=False), 'Country'] = 'Hong Kong'
df.loc[df['Country'].str.contains('taiwan', case=False), 'Country'] = 'Taiwan'
df.loc[df['Country'].str.contains('singapore', case=False), 'Country'] = 'Singapore'
df.loc[df['Country'].str.contains('japan', case=False), 'Country'] = 'Japan'
df.loc[df['Country'].str.contains('thailand', case=False), 'Country'] = 'Thailand'
df.loc[df['Country'].str.contains('china', case=False), 'Country'] = 'China'
df.loc[df['Country'].str.contains('malaysia', case=False), 'Country'] = 'Malaysia'
df.loc[df['Country'].str.contains('indonesia', case=False), 'Country'] = 'Indonesia'
# 处理stars列
df.loc[df['Stars'] == 'Unrated', 'Stars'] = '0'
df['Stars'] = df['Stars'].astype(float)
# 保存清洗过的数据
df.to_csv("ramen_cleaned.csv", index=False)
```
2. 统计各个国家拉面品牌数量,并绘制排名前10位国家的直方图:
```python
import matplotlib.pyplot as plt
# 统计各个国家的品牌数量
country_counts = df.groupby('Country')['Brand'].nunique().sort_values(ascending=False)
# 绘制前10位国家的直方图
plt.bar(country_counts.index[:10], country_counts[:10])
plt.xticks(rotation=45)
plt.xlabel('Country')
plt.ylabel('Number of brands')
plt.title('Top 10 countries with most ramen brands')
plt.show()
```
3. 找出各个国家最受欢迎的拉面品牌和包装类型:
```python
# 找出各个国家最受欢迎的品牌和包装类型
popular_brand = df.groupby('Country')['Brand'].apply(lambda x: x.value_counts().index[0])
popular_packaging = df.groupby('Country')['Packaging'].apply(lambda x: x.value_counts().index[0])
# 输出结果
print("Most popular brand by country:\n", popular_brand)
print("\nMost popular packaging by country:\n", popular_packaging)
```
4. 统计各个国家各个品牌的stars平均值:
```python
# 统计各个国家各个品牌的stars平均值
country_brand_stars = df.groupby(['Country', 'Brand'])['Stars'].mean()
# 输出结果
print(country_brand_stars)
```
5. 交叉列联表:
```python
# 统计不同包装类型的品牌数量
packaging_brand_count = pd.crosstab(df['Packaging'], df['Brand'])
print("Packaging vs Brand:\n", packaging_brand_count)
# 统计不同国家的包装类型数量
country_packaging_count = pd.crosstab(df['Country'], df['Packaging'])
print("\nCountry vs Packaging:\n", country_packaging_count)
# 统计不同星级的品牌数量
stars_brand_count = pd.crosstab(df['Stars'], df['Brand'])
print("\nStars vs Brand:\n", stars_brand_count)
```
相关推荐
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)