Home >Backend Development >Python Tutorial >How to do data analysis and mining in Python
How to perform data analysis and mining in Python
Data analysis and mining are indispensable key skills in today's information age. As a high-level programming language, Python has rich data processing and analysis libraries, making data analysis and mining easier and more efficient. This article will introduce how to perform data analysis and mining in Python, with specific code examples.
Sample code:
# 使用requests库获取网络上的数据 import requests url = "http://example.com/data.csv" response = requests.get(url) data = response.content # 使用pandas库读取本地的数据文件 import pandas as pd data = pd.read_csv("data.csv") # 使用MySQLdb库连接数据库并获取数据 import MySQLdb # 连接数据库 conn = MySQLdb.connect(host="localhost", user="root", passwd="password", db="database") cursor = conn.cursor() # 执行查询语句 cursor.execute("SELECT * FROM table") # 获取查询结果 data = cursor.fetchall() # 关闭数据库连接 conn.close()
Sample code:
import pandas as pd # 去除重复数据 data = data.drop_duplicates() # 处理缺失值 data = data.dropna() # 标准化数据 data['column'] = (data['column'] - data['column'].mean()) / data['column'].std() # 数据类型转换 data['column'] = data['column'].astype(int) # 去除异常值 q1 = data['column'].quantile(0.25) q3 = data['column'].quantile(0.75) iqr = q3 - q1 data = data[(data['column'] > q1 - 1.5*iqr) & (data['column'] < q3 + 1.5*iqr)]
Sample code:
import pandas as pd import numpy as np from sklearn.cluster import KMeans import matplotlib.pyplot as plt # 描述性统计分析 data.describe() # 数据关联分析 data.corr() # 数据聚类分析 kmeans = KMeans(n_clusters=3).fit(data) labels = kmeans.labels_ centroids = kmeans.cluster_centers_ # 数据预测和分类 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) model = LogisticRegression() model.fit(X_train, y_train) y_pred = model.predict(X_test) # 数据可视化 data.plot(kind='scatter', x='column1', y='column2') plt.show()
In summary, through the support of Python's rich libraries and modules, data analysis and mining become simpler and more efficient. I hope the above content can help you better perform data analysis and mining in Python.
The above is the detailed content of How to do data analysis and mining in Python. For more information, please follow other related articles on the PHP Chinese website!