<大数据处理与智能分析入门>试卷_编程题

2021-06-18  本文已影响0人  Tony_Pm

第一章第2题:计算圆柱的表面积与体积

import math
def comput(r,h):
    # 计算表面积 S=2πrh+2πr^2
    S = 2*math.pi*r*h + 2*math.pi*r**2
    # 计算体积 V=πr^2*h
    V = math.pi*r**2*h
    return S,
S,V = comput(10,11)
image.png

第二章第2题:

import numpy as np
def cap_2_2(N1):
    # 1.
    N4 = np.load("N4.npy")
    # print(N4)

    # 2.
    res = N4[0,[1,3]]
    res2 = N4[2,[0,4]]
    N5 = np.array([res,res2])
    # print(N5)

    # 3.
    N6 = np.hstack((N1,N5.flatten()))
    print(N6)

N1 = cap_2_1()
cap_2_2(N1)
image.png

第三章第2题

import pandas as pd
def cap_3_2():
    df = pd.read_excel("1(1).xls")
    # print(df)

    df1 = df.iloc[:,2:]
    Nt = df1.values
    # print(type(Nt))

    TF = (df["交易日期"]<="2017-01-16") & (df["交易日期"]>="2017-01-05")
    # print(TF)

    S = Nt[TF==True][:,1].sum()
    print(S)
cap_3_2()
image.png

第四章练习题

import pandas as pd
from matplotlib import pyplot as plt

def cap_4_1():
    # 1.
    df = pd.read_excel("4.xls",index_col=0)
    # print(df)

    # 2.
    df1 = df.loc["2018/1/1":"2018/1/10",:]
    plt.plot(df1.index,df1["猪肉价格"],label="pork")
    plt.plot(df1.index,df1["牛肉价格"],label="beef")
    plt.legend()
    plt.xticks(rotation=45)
    plt.show()

    # 3.
    fig,ax = plt.subplots(2,1)
    ax[0].plot(df.index,df["猪肉价格"])
    ax[1].plot(df.index,df["牛肉价格"])
    plt.show()

cap_4_1()
image.png image.png

第五章第2题

# 导入逻辑回归模型
from sklearn.linear_model import LogisticRegression
# 导入支持向量机
from sklearn.svm import SVC
# 导入深度学习
from sklearn.neural_network import MLPClassifier

def cap_5_2():
    df = pd.read_excel("3(1).xls",index_col=0)

    # 构建训练集的 特征值 与 标签值
    train_df = df.loc[1:20,:]
    # print(train_df)
    train_features = train_df.loc[:,"XI":"X3"].values
    train_label = train_df.loc[:,"Y"].values
    # print(train_features.dtype,train_label.dtype)

    # 构建测试集的 特征值
    test_df = df.loc[21:,"XI":"X3"].values
    # print(test_df)

    # 使用逻辑回归训练
    lgr_model = LogisticRegression()
    lgr_model.fit(train_features,train_label.astype("int"))
    lgr_pred = lgr_model.predict(test_df)
    print(f"逻辑回归算法预测为:{lgr_pred}")

    # 使用支持向量机模型
    svm_model = SVC()
    svm_model.fit(train_features, train_label.astype("int"))
    svm_pred = svm_model.predict(test_df)
    print(f"支持向量机算法预测为:{svm_pred}")

    # 神经网络算法
    mlp_model = MLPClassifier()
    mlp_model.fit(train_features, train_label.astype("int"))
    mlp_pred = mlp_model.predict(test_df)
    print(f"神经网络算法预测为:{mlp_pred}")

cap_5_2()
image.png
上一篇 下一篇

猜你喜欢

热点阅读