1.30每日总结
今天我计划学习一些基本函数的功能及它们的使用方法,由于网上大多没有汇总,碰巧又赶上最近学的数据处理,所以我根据需要自己整理了几个可能会用到的关于数据的函数。
- 
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]
- 
第三方库常见函数: - 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()
- NumPy库中的
 
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号