Python数据分析基础教程课后习题:(1)按手机价格统计评分最大值、最小值、算术平均值和中位数。(2)按手机品牌统计评分最大值、最小值、算术平均值和中位数。

Python数据分析基础教程课后习题:(1)按手机价格统计评分最大值、最小值、算术平均值和中位数。(2)按手机品牌统计评分最大值、最小值、算术平均值和中位数。,第1张

(1)按手机价格统计评分最大值、最小值、算术平均值和中位数。
import numpy as np

price, rating = np.loadtxt('Mobile.csv', delimiter=',', usecols=(1, 2), dtype=float, unpack=True)
print(price, rating)
u_price = np.unique(price)
print(u_price)
p_size = u_price.size
print(p_size)
price_rating = np.zeros((p_size, 5))
j = 0
for i in u_price:
    price_rating[j, 0] = i
    price_code = np.where(price == i)
    print('满足某一价格条件的元素索引值:', price_code)
    print("使用元素索引值提取评分元素")
    m_rating = rating[price_code].astype(np.int)
    print('评分数组', m_rating)

    price_rating[j,1] = np.max(m_rating)
    price_rating[j,2] = np.min(m_rating)
    price_rating[j,3] = np.mean(m_rating)
    price_rating[j,4] = np.median(m_rating)

    j += 1

print('输出价格分类统计分析结果', price_rating)
(2)按手机品牌统计评分最大值、最小值、算术平均值和中位数

import numpy as np

# brand ,rating= np.loadtxt('Mobile.csv', delimiter=',', usecols=(0,2), dtype=str,float)
brand = np.loadtxt('Mobile.csv', delimiter=',', usecols=(0), dtype=str)
# print(brand)
# print(np.size(brand))
u_brand = np.unique(brand)
# print(np.size(u_brand))
b_size = u_brand.size
print(b_size)

rating = np.loadtxt('Mobile.csv', delimiter=',', usecols=(2), dtype=float)

brand_rating = np.zeros((b_size,5)).astype(str) #

j = 0
for i in u_brand:
    brand_rating[j, 0] = i
    brand_code = np.where(brand == i)
    print('满足某一价格条件的元素索引值:', brand_code)
    print("使用元素索引值提取评分元素")
    m_rating = rating[brand_code].astype(np.int)
    print('评分数组', m_rating)

    brand_rating[j, 1] = np.max(m_rating)
    brand_rating[j, 2] = np.min(m_rating)
    brand_rating[j, 3] = np.mean(m_rating)
    brand_rating[j, 4] = np.median(m_rating)

    j += 1
print(brand_rating)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/738273.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存