1.30每日总结

今天我计划学习一些基本函数的功能及它们的使用方法,由于网上大多没有汇总,碰巧又赶上最近学的数据处理,所以我根据需要自己整理了几个可能会用到的关于数据的函数。

  1. Python内置函数:

    • len():用于获取对象的长度或元素个数。
    string = "hello"
    length = len(string)
    print(length)  # 输出:5
    
    • print():用于打印输出信息到控制台。
    print("Hello, world!")
    
    • range():用于生成一个整数序列。
    numbers = range(1, 11)
    for num in numbers:
        print(num)
    
    • zip():用于将多个序列压缩成一个元组。
    names = ["Alice", "Bob", "Charlie"]
    ages = [25, 30, 35]
    zipped = zip(names, ages)
    for name, age in zipped:
        print(f"{name} is {age} years old.")
    
    • sorted():用于对可迭代对象进行排序。
    numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
    sorted_numbers = sorted(numbers)
    print(sorted_numbers)  # 输出:[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
    
    • map():用于将函数应用于序列的每个元素,并返回结果列表。
    numbers = [1, 2, 3, 4, 5]
    squared_numbers = list(map(lambda x: x**2, numbers))
    print(squared_numbers)  # 输出:[1, 4, 9, 16, 25]
    
  2. 第三方库常见函数:

    • NumPy库中的numpy.sum()函数:用于计算数组元素之和。
    import numpy as np
    
    numbers = np.array([1, 2, 3, 4, 5])
    sum_of_numbers = np.sum(numbers)
    print(sum_of_numbers)  # 输出:15
    
    • Pandas库中的pandas.DataFrame()函数:用于创建一个数据框。
    import pandas as pd
    
    data = {"name": ["Alice", "Bob", "Charlie"],
            "age": [25, 30, 35],
            "gender": ["F", "M", "M"]}
    df = pd.DataFrame(data)
    print(df)
    
    • Matplotlib库中的matplotlib.pyplot.plot()函数:用于绘制折线图。
    import matplotlib.pyplot as plt
    
    x = [1, 2, 3, 4, 5]
    y = [1, 4, 9, 16, 25]
    plt.plot(x, y)
    plt.show()
    
    • Scikit-learn库中的sklearn.linear_model.LinearRegression()函数:用于拟合线性回归模型。
    from sklearn.linear_model import LinearRegression
    
    X = [[1], [2], [3], [4], [5]]
    y = [1, 4, 9, 16, 25]
    model = LinearRegression()
    model.fit(X, y)
    print(model.coef_)  # 输出:[9.]
    
    • TensorFlow库中的tf.keras.layers.Dense()函数:用于创建一个全连接层。
    import tensorflow as tf
    
    inputs = tf.keras.Input(shape=(10,))
    dense_layer = tf.keras.layers.Dense(units=64, activation="relu")(inputs)
    outputs = tf.keras.layers.Dense(units=1, activation="sigmoid")(dense_layer)
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    model.summary()
posted @   漏网鲨鱼  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示