The second day of python learning - Numpy data statistics and other uses

Numpy application

NumPy is usually used together with SciPy (Scientific Python) and Matplotlib (drawing library). It is a powerful scientific computing environment that helps us learn data science or machine learning through Python. Data analysis of bar graphs, pie charts, multiple subgraphs, and linear regression graphs can be made through NumPy.

In addition, you can import the Pandas library, which is a software library for data manipulation and analysis in the Python programming language. In particular, it provides data structures and operations for manipulating numerical tables and time series. Pandas allows drawing from various file formats such as CSV, JSON, a> to import data. Microsoft Excel, SQL

cmd import module:

pip install scipy -i https://pypi.tuna.tsinghua.edu.cn/simple  //运用清华源安装scipy模块

1. Simple prediction

from scipy import stats 

x = [5,7,8,7,2,17,2,9,4,11,12,9,6] 
y = [99,86,87,88,111,86,103,87,94,78,77,85,86] 

slope, intercept, r, p, std_err = stats.linregress(x, y) 

def myfunc(x): 
  return slope * x + intercept 

speed = myfunc(10) 
print(speed) 

2. Multiple regression prediction data

import pandas
from sklearn import linear_model

df = pandas.read_csv("cars.csv")    //导入cars.csv文件

x = df[['Weight', 'Volume']]
y = df['CO2']

regr = linear_model.LinearRegression()
regr.fit(x.values, y.values)

# 预测重量为 2300kg、排量为 1300ccm 的汽车的二氧化碳排放量:
predictedCO2 = regr.predict([[2300, 1300]])

print(predictedCO2)
print('---------------------')

# 描述与未知变量的关系因子
regr = linear_model.LinearRegression() 
regr.fit(x.values, y.values)

print(regr.coef_) 

The results can be predicted from the data on the relevant factors.

3. Pie chart

import matplotlib.pyplot as plt
import numpy as np

x = np.array(["Runoob-1", "Runoob-2", "Runoob-3", "C-RUNOOB"])
y = np.array([12,22,6,18])

plt.bar(x,y)
plt.show()

4. Bar chart

import matplotlib.pyplot as plt
import numpy as np

x = np.array(["Runoob-1", "Runoob-2", "Runoob-3", "C-RUNOOB"])
y = np.array([12,22,6,18])

plt.bar(x,y)
plt.show()

Guess you like

Origin blog.csdn.net/m0_57069925/article/details/134037021