C enhance Day(四)

本文详细介绍了二级指针的内存模型,二维数组的定义与使用,包括数组类型的定义,指针数组与数组指针的概念及应用,二维数组的指针表示与函数参数传递,以及常见陷阱和注意事项。

一. 回顾

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
4)三种二级指针内存模型图

在这里插入图片描述

二. 二维数组

2.1 数组类型的定义

  • 数组类型:由元素个数 和 元素类型对应 int [8]
  • 通过typedef定义一个数组类型
  • 有typedef是类型,没有typedef是变量
    在这里插入图片描述

2.2 指针数组

  • 指针数组:它是数组,每个元素都是指针
    在这里插入图片描述
  • main函数的参数:
    argc:传参数的个数(包含可执行程序)
    argv:指针数组,指向输入的参数

2.3 数组指针

< 1 > 定义数组指针的第一种方式

  • 数组指针:它是指针,指向一个数组的指针
  • 数组指针,指向一维数组的整个数组,而不是首元素地址

在这里插入图片描述

  • 这里需要注意的是p = a是会警告的,因为p应该是指向一整个一维数组的,而a是首元素的地址,不报错是因为首元素地址和整个数组的首地址是相同的。
  • 此时打印p+1的,相比于p加的步长是一整个一维数组的长度,也就是40:
    在这里插入图片描述
  • 如果要给数组指针所指向的数组赋值,操作如下:
    在这里插入图片描述
    打印方式如下:
    在这里插入图片描述

< 2 > 定义数组指针的第二种方式

  • 先定义数组指针类型,再根据类型定义变量
  • 第二种定义方式和指针数组写法很类似,多了一个( )
  • ( )和[ ]的优先级是一样的,从左往右
  • ( )里面有指针,它是一个指针
  • 指向数组的指针,但是有了typedef,所以它是一个数组指针类型

定义了一个数组指针类型,然后根据这个类型定义了一个变量q:
在这里插入图片描述

现在给这个数组指针一个指向:

int a[10] = {0};

typedef int (*P)[10];
P q;
q = &a;

< 3 > 定义数组指针的第三种方式

在这里插入图片描述

< 4 > 总结

在这里插入图片描述

2.4 二维数组的使用

定义数组 int a3[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}

  • 二维数组数组名代表第0行的首地址
  • 区别于第0行首元素地址,它们的值是一样的,但是步长不一样
  • 二维数组的数组名,可以理解是一维数组的数组名加上取地址&,这两个代表的意思是一样的
  • *(a3 + 0)给二维数组的数组名加 * 号,此时表示的是第0行首元素的地址,咱也不知道为啥,记着吧。所以第0行第二个元素的地址是*(a3+0)+1 。第1行首元素的地址为*(a3+1),相比于*(a3+0)偏移的是16个字节
  • 因为 *[ ]是等价的,所以*(a3+0)a3[0]是等价的,a3[1]*(a3+1) 是等价的,a3[0]a3[1]跨越16个字节。a3[0]表示的是第0行的首元素的地址,a3[0]+1表示的是第0行第二个元素的地址,a3[0]a3[0]+1跨越4个字节
    在这里插入图片描述

总结一下:

在这里插入图片描述
在这里插入图片描述

2.5 数组指针和二维数组相结合

注意:这里 p = &a 又会报警告,因为这里的a变成二维数组了,这里的&a表示的是整个二维数组的首地址,上面定义的指向一维数组的数组指针时这样使用就是对的,具体的为什么自己理解一下。

在这里插入图片描述
打印二维数组的值:

在这里插入图片描述

2.6 首行首元素地址和首行首地址的区别

  • 测量一维数组的长度时,sizeof输入的是首行首元素的地址,如:sizeof(t)
    在这里插入图片描述
  • 二维数组的a[0],表示的是第0行首元素的地址,所以等于sizeof测出来是40个字节。&a[0]表示的是第0行整个一维数组的地址,它是一个指针类型,所以是4个字节
    在这里插入图片描述

2.7 二维数组做函数参数

在这里插入图片描述

< 1 > 5K写法

在这里插入图片描述

< 2 > 7K写法

在这里插入图片描述

< 3 > 错误写法

这里二级指针,a+1跳的是4个字节,其它三种写法步长是 a+1 一次跳一行,16个字节

在这里插入图片描述

< 4 > 9K写法

在这里插入图片描述

【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
Group Assignment - Case Study BACKGROUND BeachBoys BikeShare is a bike share service provider where users can take and return bikes at any of the 70 sta�ons on their network. The company wants to leverage their data to beter understand and, hopefully, op�mize their opera�ons. BikeShare has decided to start by harnessing analy�cs to enhance opera�ons in the logis�cs department, by improving the redistribu�on of bikes between sta�ons to meet demand, and ensuring that there are bikes and return docks available when and where users need them. As a key step towards tackling this challenge, management has tasked you to develop a model capable of predic�ng the net rate of bike ren�ng for a given sta�on, which is defined as the number of bikes returned to, minus the number of bikes taken from, the given sta�on in a given hour. In other words, your model should enable BikeShare's logis�cs team to make the statement - "In the next hour, the quan�ty of bikes at sta�on A will change by X”. In addi�on, management would also like you to: • Help them understand the factors that affect bike rental, which could inform future decisions on where to locate BikeShare's sta�ons. • Help them conceptualize how your predic�on may be used to improve the redistribu�on of bikes within the network. • Highlight any assump�ons or drawbacks of the analysis, if any, and suggest how they may be verified or addressed in the future. ASSIGNMENT Explore, transform, and visualize the given data as appropriate, before using it to train and evaluate an appropriate ML model for the problem. Address the issues highlighted by management as described above, albeit in a less in-depth manner. Finally, ar�culate your findings and recommenda�ons in a concise, coherent, and professional manner, making reference to any earlier results or diagrams as appropriate to support your conclusions. Please use Python to complete this task, using any libraries you might deem necessary for your analysis, e.g., pandas, sklearn, etc. Detail your code, analysis findings, and recommenda�ons clearly in a reproducible Jupyter notebook with appropriate comments and documenta�on, so that an individual viewing the notebook will be able to follow through your steps and understand the reasoning involved and inferences made. DELIVERABLES You should upload the following deliverables in a .zip file: • A Jupyter notebook detailing your analysis and findings for this project, • A PDF-ed copy of the Jupyter notebook above, which should not exceed 30 pages, • The datasets used in your analysis, which should be loaded into your notebook, • Addi�onal files relevant to your analysis, which should be described in your notebook. Finally, please prepare presenta�on slides for the group presenta�on (10-15 mins). All team members need to present in English. THE DATA The company has collected informa�on on the sta�ons, trips taken, and on weather condi�ons in each of the ci�es from September 2014 to August 2015. You can find the data here - bikes_data.zip (3.1 MB). Below, you will also find detailed informa�on on all the fields available in the dataset. The way you include this informa�on in your model is up to you and should be clearly jus�fied and documented in your report. You are free to use any other data sources provided you specify a link to this informa�on in your report. Sta�on Data • ld: sta�on ID number • Name: name of sta�on • Lat: la�tude • Long: longitude • Dock Count: number of total docks at sta�on • City: one of San Francisco, Redwood City, Palo Alto, Mountain View, or San Jose Please note that during the period covered by the dataset, several sta�ons were moved. Sta�ons 23, 25, 49, 69, and 72 became respec�vely sta�ons 85, 86, 87, 88, 89 (which in turn became 90 a�er a second move). Trip Data • Trip ld: numeric ID of bike trip • Dura�on: �me of trip in seconds • Start Date: start date of trip with date and �me, in Pacific Standard Time • Start Sta�on: sta�on id of start sta�on • Start Terminal: numeric reference for start sta�on • End Date: end date of trip with date and �me, in Pacific Standard Time • End Sta�on: sta�on id for end sta�on • Subscrip�on Type: Subscriber (annual or 30-day member) or Customer (24hour or 3-day member) Weather Data • Date: day for which the weather is being reported • Temperature (day min, mean and max): in F • Dew point (day min, mean and max): Temperature in F below which dew can form • Humidity (day min, mean and max): in % • Pressure (day min, mean and max): Atmospheric pressure at sea level in inches of mercury • Visibility (day min, mean and max): distance in miles • Wind Speed (day max and mean): in mph • Max Gust Speed: in mph • Precipita�on: total amount of precipita�ons in inches • Cloud Cover: scale of 0 (clear) tons (totally covered) • Events: Special meteorological events • Wind Direc�on: in degrees • Zip: area code for San Francisco (94107), Redwood City (94063), Palo Alto (94301), Mountain View (94041), and San Jose (95113)
08-07
这是一个非常典型的机器学习项目,目标是预测自行车租赁的净变化,并分析影响因素。以下是该项目的步骤概述和一个基本的代码框架。 ### 步骤概述 1. **数据加载**:加载并合并三个数据集(站点、骑行、天气)。 2. **数据预处理**:清理缺失值、转换时间字段、特征工程等。 3. **可视化**:探索数据分布和关系。 4. **模型训练**:选择适当的回归模型(如线性回归、随机森林等)进行训练。 5. **结果分析**:解释模型结果,识别影响因素。 6. **报告撰写**:总结发现和建议。 ### 代码框架 ```python import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error, r2_score # 加载数据 stations = pd.read_csv('stations.csv') trips = pd.read_csv('trips.csv') weather = pd.read_csv('weather.csv') # 数据预处理 # 转换时间字段为datetime格式 trips['Start Date'] = pd.to_datetime(trips['Start Date']) trips['End Date'] = pd.to_datetime(trips['End Date']) # 提取小时信息 trips['Start Hour'] = trips['Start Date'].dt.hour trips['End Hour'] = trips['End Date'].dt.hour # 计算每个站点每小时的净变化 hourly_changes = trips.groupby(['Start Station', 'Start Hour']).size().reset_index(name='Outflows') hourly_returns = trips.groupby(['End Station', 'End Hour']).size().reset_index(name='Inflows') # 合并出库和入库数据 net_changes = hourly_changes.merge(hourly_returns, how='outer', left_on=['Start Station', 'Start Hour'], right_on=['End Station', 'End Hour']) net_changes['Net Change'] = net_changes['Inflows'] - net_changes['Outflows'] net_changes.dropna(subset=['Net Change'], inplace=True) # 特征工程 # 将站点ID与站点数据合并 net_changes = net_changes.merge(stations[['id', 'Lat', 'Long', 'Dock Count', 'City']], how='left', left_on='Start Station', right_on='id') # 将天气数据按日期和城市合并 weather['Date'] = pd.to_datetime(weather['Date']) weather.set_index('Date', inplace=True) net_changes['Start Date'] = net_changes['Start Hour'].apply(lambda x: pd.Timestamp(f'2014-09-01 {x}:00')) # 假设日期固定 net_changes.set_index('Start Date', inplace=True) net_changes = net_changes.join(weather, on='Date') # 模型训练 X = net_changes[['Lat', 'Long', 'Dock Count', 'Temperature (mean)', 'Humidity (mean)', 'Wind Speed (mean)']] y = net_changes['Net Change'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) model = LinearRegression() model.fit(X_train, y_train) # 模型评估 y_pred = model.predict(X_test) mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred) print(f'MSE: {mse}') print(f'R^2: {r2}') # 可视化 plt.figure(figsize=(10, 6)) sns.scatterplot(x=y_test, y=y_pred) plt.xlabel('Actual Net Change') plt.ylabel('Predicted Net Change') plt.title('Actual vs Predicted Net Change') plt.show() # 结果分析 coefficients = pd.DataFrame({'Feature': X.columns, 'Coefficient': model.coef_}) print(coefficients) ``` ### 分析和建议 1. **特征重要性**:通过模型系数可以确定哪些特征对净变化影响最大。 2. **运营优化**:根据预测结果,可以在需求高峰期前调整自行车分配。 3. **局限性和改进**:考虑更多外部因素(如节假日、特殊事件),并验证模型在不同时间段的表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值